simplexml_import_dom()
simplexml_import_dom() — Get a SimpleXMLElement object from an XML or HTML node.
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 a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.
Signature
simplexml_import_dom(object $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $node | object | A DOM Element node |
| $class_name | ?string | You may use this optional parameter so that simplexml_import_dom() will return an object of the specified class. That class must extend the SimpleXMLElement class. |
Return value
Returns a SimpleXMLElement or null on failure.
Example
A runnable snippet:
$dom = new DOMDocument; $dom->loadXML('<books><book><title>blah</title></book></books>'); $s = simplexml_import_dom($dom); echo $s->book[0]->title;
Output:
blah
Changelog
| Version | Change |
|---|---|
| 8.4.0 | Added support for Dom\Document. |
| 8.4.0 | This function now throws a TypeError instead of a ValueError when passed a non-XML or non-HTML node. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page simplexml_import_dom() (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.