API15:JSimpleXMLElement/toString
From Joomla! Documentation
Description
Return a well-formed XML string based on SimpleXML element
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
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;
}
[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >
Examples
Code Examples