JXMLElement/asFormattedXML
Aus Joomla! Dokumentation
< JXMLElement(Weitergeleitet von Joomla! Programmierung/Framework/JXMLElement/asFormattedXML)
Inhaltsverzeichnis |
[Bearbeiten] Beschreibung
JXMLElement->asFormattedXML() Gibt das Element formatiert zurück.
[Bearbeiten] Syntax
asFormattedXML( [$compressed], [$indent], [$level] )
| Parameter | Datentyp | Beschreibung | Standardwert |
|---|---|---|---|
| [$compressed] | boolean | Soll die Ausgabe komprimiert werden. | false |
| [$indent] | string | Welche(s) Zeichen soll zum Einrücken benutzt werden (Die Vorgabe ist ein Tabulator) | \t |
| [$level] | integer | Bei welchem Level soll mit dem Einrücken begonnen werden. | 0 |
[Bearbeiten] Beispiele
<nafu> <doku kategorie="Info">Wiki</doku> </nafu>
Das gesamte XML Dokument oder einen Teil davon formatiert ausgeben. (Falls Sie dieses Beispiel nicht gerade im CLI ausprobieren haben wir noch <pre>'s und htmlentities() hinzugefügt).
<?php $xml = JFactory::getXML('demo.xml'); echo '<pre>'; echo htmlentities($xml->asFormattedXML()); echo '</pre>';
Ausgabe:
<nafu> <doku kategorie="Info">Wiki</doku> </nafu>
Die "komprimierte" Ausgabe, z.B. zur Verwendung in Streams:
$xml->asFormattedXML(true)
Ausgabe:
<nafu><doku kategorie="Info">Wiki</doku></nafu>
Nur einen Teil des Dokuments ausgeben:
$xml->doku->asFormattedXML()
Ausgabe:
<doku kategorie="Info">Wiki</doku>
[Bearbeiten] Quellcode
public function asFormattedXML($compressed = false, $indent = "\t", $level = 0) { $out = ''; // Start a new line, indent by the number indicated in $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 /> { $out .= " />"; } else { // If there are 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 } elseif ((string) $this) { // If there is data, close off the start tag and add the data } // Add the end tag $out .= '</' . $this->getName() . '>'; } return $out; }
[Bearbeiten] Siehe auch
- JXMLElement->asFormattedXML() auf api.joomla.org