dom_import_simplexml()
dom_import_simplexml() — Gets a DOMAttr or DOMElement object from a SimpleXMLElement object.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
This function takes the given attribute or element node (a SimpleXMLElement instance) and creates a DOMAttr or DOMElement node, respectively. The new DOMNode refers to the same underlying XML node as the SimpleXMLElement.
Signature
dom_import_simplexml(object $node): DOMAttr|DOMElement
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $node | object | The attribute or element node to import (a SimpleXMLElement instance). |
Return value
The DOMAttr or DOMElement.
Example
A runnable snippet:
$sxe = simplexml_load_string('<books><book><title>blah</title></book></books>'); if ($sxe === false) { echo 'Error while parsing the document'; exit; } $dom_sxe = dom_import_simplexml($sxe); if (!$dom_sxe) { echo 'Error while converting XML'; exit; } $dom = new DOMDocument('1.0'); $dom_sxe = $dom->importNode($dom_sxe, true); $dom_sxe = $dom->appendChild($dom_sxe); echo $dom->saveXML();
Output:
<?xml version="1.0"?> <books><book><title>blah</title></book></books>
Changelog
| Version | Change |
|---|---|
| 8.0.0 | This function no longer returns null on failure. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page dom_import_simplexml() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.