SimpleXMLElement::asXML

(PHP 5, PHP 7, PHP 8)

SimpleXMLElement::asXML Return a well-formed XML string based on SimpleXML element

Опис

public SimpleXMLElement::asXML(?string $filename = null): string|bool

The asXML method formats the parent object's data in XML version 1.0.

Параметри

filename

If a string value is provided, the function writes the data to the file rather than returning it.

Значення, що повертаються

If the filename isn't specified, this function returns a string on success and false on error. If the parameter is specified, it returns true if the file was written successfully and false otherwise.

Журнал змін

Версія Опис
8.0.0 filename is nullable now.

Приклади

Приклад #1 Get XML

<?php
$string
= <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;

$xml = new SimpleXMLElement($string);

echo
$xml->asXML();

?>

Поданий вище приклад виведе:

<?xml version="1.0"?>
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>

asXML also works on Xpath results:

Приклад #2 Using asXML() on SimpleXMLElement::xpath() results

<?php
$string
= <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;

$xml = new SimpleXMLElement($string);

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

foreach (
$result as $node) {
echo
$node->asXML();
}
?>

Поданий вище приклад виведе:

<c>text</c><c>stuff</c>

Прогляньте також