Actions

API15

JSimpleXMLElement/toString

From Joomla! Documentation

< API15:JSimpleXMLElement
Revision as of 17:25, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Return a well-formed XML string based on SimpleXML element

[Edit Descripton]

Description:JSimpleXMLElement/toString

Syntax

toString($whitespace=true)
Parameter Name Default Value Description
$whitespace true

Returns

string

Defined in

libraries/joomla/utilities/simplexml.php

Importing

jimport( 'joomla.utilities.simplexml' );

Source Body

function toString($whitespace=true)
{
        //Start a new line, indent by the number indicated in $this->level, add a <, and add the name of the tag
        if ($whitespace) {
                $out = "\n".str_repeat("\t", $this->_level).'<'.$this->_name;
        } else {
                $out = '<'.$this->_name;
        }
 
        //For each attribute, add attr="value"
        foreach($this->_attributes as $attr => $value) {
                $out .= ' '.$attr.'="'.htmlspecialchars($value).'"';
        }
 
        //If there are no children and it contains no data, end it off with a />
        if (empty($this->_children) && empty($this->_data)) {
                $out .= " />";
        }
        else //Otherwise...
        {
                //If there are children
                if(!empty($this->_children))
                {
                        //Close off the start tag
                        $out .= '>';
 
                        //For each child, call the asXML function (this will ensure that all children are added recursively)
                        foreach($this->_children as $child)
                                $out .= $child->toString($whitespace);
 
                        //Add the newline and indentation to go along with the close tag
                        if ($whitespace) {
                                $out .= "\n".str_repeat("\t", $this->_level);
                        }
                }
 
                //If there is data, close off the start tag and add the data
                elseif(!empty($this->_data))
                        $out .= '>'.htmlspecialchars($this->_data);
 
                //Add the end tag
                $out .= '</'.$this->_name.'>';
        }
 
        //Return the final output
        return $out;
}

[Edit See Also] SeeAlso:JSimpleXMLElement/toString

Examples

<CodeExamplesForm />