PHP 7.4.0RC4 Released!


(PHP 5, PHP 7)

simplexml_import_domObtiene un objeto SimpleXMLElement de un nodo DOM


simplexml_import_dom ( DOMNode $node [, string $class_name = "SimpleXMLElement" ] ) : SimpleXMLElement

Esta función toma un nodo de un documento DOM y lo convierte en un nodo SimpleXML. Este nuevo objeto puede ser usado como un elemento SimpleXML nativo.



Un nodo del elemento DOM


Este parámetro opcional debe usarse cuando se necesite que simplexml_import_dom() retorne un objeto de la clase indicada. Esa clase debe extender de la clase SimpleXMLElement.

Valores devueltos

Retorna un SimpleXMLElement o FALSE en caso de error.


Ejemplo #1 Importando desde DOM

= new DOMDocument;
if (!
$dom) {
'Error analizando el documento';

$s simplexml_import_dom($dom);


El resultado del ejemplo sería:


Ver también

add a note add a note

User Contributed Notes 2 notes

shirkaen at gmail dot com
2 years ago
/*If tags in DOM have text and some part of this text is surrounded by other tag, simple_import_dom object doesn't contain position of child tag, so rendering it back to html/xml can be a problem. Printing such a node is also peculiar. Eg.*/

$text = "<body><p>Some text before <em>italic </em>and after text.</p></body>";
$dom = new DOMDocument;
$result = simplexml_import_dom($dom);
echo $result->p;

//Prints :
//Some text before and after text.

//SimpleXMLElement Object ( [em] => italic )

//does more or less the same:
//SimpleXMLElement Object ( [0] => italic )

//object(SimpleXMLElement)#8 (1) { ["em"]=> string(7) "italic " }

//object(SimpleXMLElement)#9 (1) { [0]=> string(7) "italic " }

echo $result->p->em;
brandonkirsch at perceptionilluminates dot com
6 years ago
simplexml_import_dom() will accept DOMNodes or other SimpleXMLElements.  This has a useful purpose if you are extending the SimpleXMLElement class and would prefer the xpath() method to return your class extension instead of basic SimpleXMLElements.  For example:

class MySimpleXML extends SimpleXMLElement{
    public function xpath($xpath){
        $return = array();
        $simpleXmls = parent::xpath($xpath); // run SimpleXMLElement xpath, returning an array of SimpleXMLElements
        foreach($simpleXmls as $xml){
            $return[] = simplexml_import_dom($xml,'MySimpleXML'); // copy SimpleXMLElement, returning as MySimpleXML instance
        return $return;

Now I can create an instance of MySimpleXML, run an xpath, and get my results back as an array of MySimpleXML elements instead of the native SimpleXMLElement:

$mySimpleXml = new mySimpleXml('<root><node/></root>');

$array = $mySimpleXml->xpath('/root/node'); // returns array with a single MySimpleXml object
To Top