PHP 8.5.0 Alpha 1 available for testing

simplexml_import_dom

(PHP 5, PHP 7, PHP 8)

simplexml_import_domConstruye un objeto SimpleXMLElement a partir de un objeto XML o HTML

Descripción

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

simplexml_import_dom() toma un nodo de un documento DOM y lo transforma en nodo SimpleXML. Este nuevo objeto puede entonces ser utilizado como un objeto nativo SimpleXML.

Parámetros

node

Un elemento DOM

class_name

Este parámetro opcional permite que simplexml_load_string() retorne un objeto de la clase especificada. Esta clase debe extender la clase SimpleXMLElement.

Valores devueltos

Retorna un objeto SimpleXMLElement o null en caso de fallo.

Errores/Excepciones

Lanza una TypeError cuando un node no-XML o no-HTML es pasado.

Historial de cambios

Versión Descripción
8.4.0 Se añade soporte para Dom\Document.
8.4.0 Esta función ahora lanza una TypeError en lugar de una ValueError cuando un node no-XML o no-HTML es pasado.

Ejemplos

Ejemplo #1 Importar un DOMDocument

<?php
$dom
= new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!
$dom) {
echo
'Error durante el análisis del documento';
exit;
}

$s = simplexml_import_dom($dom);

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

El resultado del ejemplo sería:

blah

Ejemplo #2 Importar un Dom\Document

<?php
$dom
= Dom\XMLDocument::createFromString('<books><book><title>blah</title></book></books>');

$s = simplexml_import_dom($dom);

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

El resultado del ejemplo sería:

blah

Ver también

add a note

User Contributed Notes 3 notes

up
-1
paul dot allsopp at digital-pig dot com
2 years ago
Why not just:
simplexml_import_dom(dom_import_simplexml($SimpleXmlNode))
up
-2
shirkaen at gmail dot com
8 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
-4
brandonkirsch at perceptionilluminates dot com
11 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