Why not just:simplexml_import_dom(dom_import_simplexml($SimpleXmlNode))
(PHP 5, PHP 7, PHP 8)
simplexml_import_dom — Erzeugt ein SimpleXMLElement
-Objekt aus einem XML- oder HTML-Knoten
$node
, ?string $class_name
= SimpleXMLElement::class): ?SimpleXMLElementDie Funktion wandelt einen Knoten eines DOM-Dokuments in einen SimpleXML-Knoten um. Das neue Objekt kann wie ein normales SimpleXML-Element verwendet werden.
node
Ein DOM-Element-Knoten.
class_name
Der optionale Parameter kann verwendet werden, wenn simplexml_import_dom() ein Objekt der spezifischen Klasse zurückgeben soll. Die gewählte Klasse sollte von der Klasse SimpleXMLElement abgeleitet sein.
Gibt ein SimpleXMLElement-Objekt zurück oder null
, falls ein
Fehler auftrat.
Throws a TypeError when passed a
non-XML or non-HTML node
.
Version | Beschreibung |
---|---|
8.4.0 | Unterstützung für Dom\Document hinzugefügt. |
8.4.0 |
Diese Funktion wirft nun einen TypeError anstelle
eines ValueError, wenn der übergebene
node weder XML noch HTML ist.
|
Beispiel #1 Ein DOMDocument importieren
<?php
$dom = new DOMDocument;
$dom->loadXML('<buecher><buch><titel>blah</titel></buch></buecher>');
if (!$dom) {
echo 'Fehler beim Parsen des Dokuments';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->buch[0]->titel;
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
blah
Beispiel #2 Ein Dom\Document importieren
<?php
$dom = Dom\XMLDocument::createFromString('<buecher><buch><titel>blah</titel></buch></buecher>');
$s = simplexml_import_dom($dom);
echo $s->buch[0]->titel;
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
blah
Why not just:simplexml_import_dom(dom_import_simplexml($SimpleXmlNode))
/*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 " }//butecho $result->p->em;//prints://italic
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