JXMLElement/asFormattedXML

Aus Joomla! Dokumentation
Wechseln zu: Navigation, Suche

Inhaltsverzeichnis

[Bearbeiten] Beschreibung

JXMLElement->asFormattedXML() Gibt das Element formatiert zurück.

[Bearbeiten] Syntax

asFormattedXML( [$compressed], [$indent], [$level] )
  • @return string
  • @since Joomla 1.6
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

File xml.png demo.xml

<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

JXMLElement->asFormattedXML in Joomla! 2.5.4

Folder blue.png libraries

  • Folder red.png joomla
    • Folder green.png utilities
      • File php.png xmlelement.php
  1. public function asFormattedXML($compressed = false, $indent = "\t", $level = 0)
  2. {
  3. $out = '';
  4.  
  5. // Start a new line, indent by the number indicated in $level
  6. $out .= ($compressed) ? '' : "\n" . str_repeat($indent, $level);
  7.  
  8. // Add a <, and add the name of the tag
  9. $out .= '<' . $this->getName();
  10.  
  11. // For each attribute, add attr="value"
  12. foreach ($this->attributes() as $attr)
  13. {
  14. $out .= ' ' . $attr->getName() . '="' . htmlspecialchars((string) $attr, ENT_COMPAT, 'UTF-8') . '"';
  15. }
  16.  
  17. // If there are no children and it contains no data, end it off with a />
  18. if (!count($this->children()) && !(string) $this)
  19. {
  20. $out .= " />";
  21. }
  22. else
  23. {
  24. // If there are children
  25. if (count($this->children()))
  26. {
  27. // Close off the start tag
  28. $out .= '>';
  29.  
  30. $level++;
  31.  
  32. // For each child, call the asFormattedXML function (this will ensure that all children are added recursively)
  33. foreach ($this->children() as $child)
  34. {
  35. $out .= $child->asFormattedXML($compressed, $indent, $level);
  36. }
  37.  
  38. $level--;
  39.  
  40. // Add the newline and indentation to go along with the close tag
  41. $out .= ($compressed) ? '' : "\n" . str_repeat($indent, $level);
  42.  
  43. }
  44. elseif ((string) $this)
  45. {
  46. // If there is data, close off the start tag and add the data
  47. $out .= '>' . htmlspecialchars((string) $this, ENT_COMPAT, 'UTF-8');
  48. }
  49.  
  50. // Add the end tag
  51. $out .= '</' . $this->getName() . '>';
  52. }
  53.  
  54. return $out;
  55. }

[Bearbeiten] Siehe auch

Meine Werkzeuge
Namensräume
Varianten
Aktionen
Navigation
Sonstiges
Team Navigation
Werkzeuge