API16

JXMLElement/asFormattedXML

From Joomla! Documentation

< API16:JXMLElement
Revision as of 17:56, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Return a well-formed XML string based on SimpleXML element <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>[[Description:JXMLElement/asFormattedX...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

[Edit Descripton]

Template:Description:JXMLElement/asFormattedXML

Syntax[edit]

asFormattedXML($compressed=false, $indent="\t", $level=0)
Parameter Name Default Value Description
$compressed false $compressed Should we use indentation and newlines ?
$indent "\t" $level Indentaion level.
$level 0

Returns[edit]

string

Defined in[edit]

libraries/joomla/utilities/xmlelement.php

Importing[edit]

jimport( 'joomla.utilities.xmlelement' );

Source Body[edit]

public function asFormattedXML($compressed = false, $indent = "\t", $level = 0)
{
        $out = '';

        //-- Start a new line, indent by the number indicated in $level
        $out .=($compressed) ? '' : "\n".str_repeat($indent, $level);

        //-- Add a <, and add the name of the tag
        $out .= '<'.$this->getName();

        //-- For each attribute, add attr="value"
        foreach($this->attributes() as $attr) {
                $out .= ' '.$attr->getName().'="'.htmlspecialchars((string)$attr, ENT_COMPAT, 'UTF-8').'"';
        }

        //-- If there are no children and it contains no data, end it off with a />
        if( ! count($this->children())
        && !(string)$this)
        {
                $out .= " />";
        }
        else
        {
                //-- If there are children
                if(count($this->children()))
                {
                        //-- Close off the start tag
                        $out .= '>';

                        $level ++;

                        //-- For each child, call the asFormattedXML function (this will ensure that all children are added recursively)
                        foreach($this->children() as $child)
                        {
                                $out .= $child->asFormattedXML($compressed, $indent, $level);
                        }

                        $level --;

                        //-- Add the newline and indentation to go along with the close tag
                        $out .=($compressed) ? '' : "\n".str_repeat($indent, $level);
                }
                elseif((string)$this)
                {
                        //-- If there is data, close off the start tag and add the data
                        $out .= '>'.htmlspecialchars((string)$this, ENT_COMPAT, 'UTF-8');
                }

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

        return $out;
}

[Edit See Also] Template:SeeAlso:JXMLElement/asFormattedXML

Examples[edit]

<CodeExamplesForm />

Get the whole XML document or a part of it.

demo.xml

<jdoc>
	<doku kategory="Info">Wiki</doku>
</jdoc>

Get the whole XML document or a part of it.

<?php

$xml = JFactory::getXML('demo.xml');

echo '<pre>';
echo htmlentities($xml->asFormattedXML());
echo '</pre>';

Output:

<jdoc>
	<doku kategory="Info">Wiki</doku>
</jdoc>

Die "komprimierte" Ausgabe, z.B. zur Verwendung in Streams:

$xml->asFormattedXML(true)

Output:

<jdoc><doku kategory="Info">Wiki</doku></jdoc>

Output only a part of the document:

$xml->doku->asFormattedXML()

Output:

<doku kategory="Info">Wiki</doku>


Elkuku 11:26, 14 January 2011 (CST) Edit comment