API16

JSimpleXMLElement/toString

From Joomla! Documentation

< API16:JSimpleXMLElement

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Return a well-formed XML string based on SimpleXML element



Syntax[edit]

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

Returns[edit]

string

Defined in[edit]

libraries/joomla/utilities/simplexml.php

Importing[edit]

jimport( 'joomla.utilities.simplexml' );

Source Body[edit]

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, ENT_COMPAT, 'UTF-8').'"';
        }

        //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, ENT_COMPAT, 'UTF-8');

                //Add the end tag
                $out .= '</'.$this->_name.'>';
        }

        //Return the final output
        return $out;
}



Examples[edit]

Code Examples[edit]