simplexml_import_dom

(PHP 5, PHP 7, PHP 8)

simplexml_import_domRecebe um objeto SimpleXMLElement de um nó DOM

Descrição

simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement

Esta função recebe um nó de um documento DOM e o transforma em um nó SimpleXML. O novo objeto pode ser utilizado como um elemento SimpleXML nativo.

Parâmetros

node

Um nó DOM

class_name

Pode-se utilizar o parâmetro opcional simplexml_import_dom() para retornar um objeto da classe especificada. A classe deve estender a classe SimpleXMLElement.

Valor Retornado

Retorna um SimpleXMLElement ou null em caso de falha.

Exemplos

Exemplo #1 Importando o DOM

<?php
$dom
= new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!
$dom) {
echo
'Error while parsing the document';
exit;
}

$s = simplexml_import_dom($dom);

echo
$s->book[0]->title;
?>

O exemplo acima produzirá:

blah

Veja Também

add a note add a note

User Contributed Notes 2 notes

up
-1
shirkaen at gmail dot com
7 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;
$dom->loadHTML($text);
$result = simplexml_import_dom($dom);
echo $result->p;

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

print_r($result->p);
//Prints:
//SimpleXMLElement Object ( [em] => italic )

print_r($result->p->em)
//does more or less the same:
//SimpleXMLElement Object ( [0] => italic )

var_dump($result->p);
//Prints:
//object(SimpleXMLElement)#8 (1) { ["em"]=> string(7) "italic " }

var_dump($result->p->em)
//object(SimpleXMLElement)#9 (1) { [0]=> string(7) "italic " }

//but
echo $result->p->em;
//prints:
//italic
up
-6
brandonkirsch at perceptionilluminates dot com
10 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