JSimpleXMLElement/toString: Difference between revisions
From Joomla! Documentation
New page: ===Description===
Return a well-formed XML string based on SimpleXML element
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JSimpleXMLElement/toStri... |
m clean up |
||
| Line 2: | Line 2: | ||
Return a well-formed XML string based on SimpleXML element | Return a well-formed XML string based on SimpleXML element | ||
{{Description:JSimpleXMLElement/toString}} | |||
{{subst:Description:JSimpleXMLElement/toString}} | |||
===Syntax=== | ===Syntax=== | ||
| Line 81: | Line 79: | ||
</source> | </source> | ||
{{subst:SeeAlso:JSimpleXMLElement/toString}} | |||
{{SeeAlso:JSimpleXMLElement/toString}} | |||
===Examples=== | ===Examples=== | ||
Revision as of 14:26, 24 March 2017
Description
Return a well-formed XML string based on SimpleXML element
{{subst: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, 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;
}
{{subst:SeeAlso:JSimpleXMLElement/toString}}
Examples
<CodeExamplesForm />