PHP 8.4.1 Released!

dom_import_simplexml

(PHP 5, PHP 7, PHP 8)

dom_import_simplexml Ermittelt ein DOMElement-Objekt aus einem SimpleXMLElement-Objekt

Beschreibung

dom_import_simplexml(object $node): DOMAttr|DOMElement

Diese Funktion verwendet das angegebene Attribut oder Element node (eine SimpleXMLElement-Instanz) und erstellt einen DOMAttr- bzw. DOMElement-Knoten. Der neue DOMNode verweist auf denselben zugrundeliegenden XML-Knoten wie das SimpleXMLElement.

Parameter-Liste

node

Der zu importierende Attribut- oder Elementknoten (eine Instanz von SimpleXMLElement).

Rückgabewerte

Das DOMAttr oder DOMElement.

Changelog

Version Beschreibung
8.0.0 Diese Funktion gibt im Fehlerfall nicht mehr null zurück.

Beispiele

Beispiel #1 SimpleXML mit der Funktion dom_import_simplexml() in DOM importieren

<?php

$sxe
= simplexml_load_string('<books><book><title>blah</title></book></books>');

if (
$sxe === false) {
echo
'Fehler beim Parsen des Dokumentes';
exit;
}

$dom_sxe = dom_import_simplexml($sxe);
if (!
$dom_sxe) {
echo
'Fehler bei der Konvertierung zu XML';
exit;
}

$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);

echo
$dom->saveXML();

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

<?xml version="1.0"?>
<books><book><title>blah</title></book></books>

Beispiel #2 SimpleXML in DOM importieren und SimpleXML über DOM modifizieren

Die Fehlerbehandlung wurde der Kürze halber weggelassen.

<?php

$sxe
= simplexml_load_string('<books><book><title>blah</title></book></books>');
$elt = dom_import_simplexml($sxe);
$elt->setAttribute("foo", "bar");
echo
$sxe->asXML();

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

<?xml version="1.0"?>
<books foo="bar"><book><title>blah</title></book></books>

Siehe auch

add a note

User Contributed Notes 5 notes

up
12
crescentfreshpot at yahoo dot com
16 years ago
justinpatrin at php dot net:
> To get a proper DOM document (which you need to do most things) you need...

No you don't. Just do:
<?php
$dom
= dom_import_simplexml($xml)->ownerDocument;
?>
up
8
Jeff M
15 years ago
SimpleXML is an 'Object Mapping XML API'. It is not DOM, per se. SimpleXML converts the XML elements into PHP's native data types.

The dom_import_simplexml and simplexml_import_dom functions do *not* create separate copies of the original object. You are free to use the methods of either or both interchangeably, since the underlying instance is the same.

<?php
// initialize a simplexml object
$sxe = simplexml_load_string('<root/>');

// get a dom interface on the simplexml object
$dom = dom_import_simplexml($sxe);

// dom adds a new element under the root
$element = $dom->appendChild(new DOMElement('dom_element'));

// dom adds an attribute on the new element
$element->setAttribute('creator', 'dom');

// simplexml adds an attribute on the dom element
$sxe->dom_element['sxe_attribute'] = 'added by simplexml';

// simplexml adds a new element under the root
$element = $sxe->addChild('sxe_element');

// simplexml adds an attribute on the new element
$element['creator'] = 'simplexml';

// dom finds the simplexml element (via DOMNodeList->index)
$element = $dom->getElementsByTagName('sxe_element')->item(0);

// dom adds an attribute on the simplexml element
$element->setAttribute('dom_attribute', 'added by dom');

echo (
'<pre>');
print_r($sxe);
echo (
'</pre>');
?>

Outputs:

SimpleXMLElement Object
(
[dom_element] => SimpleXMLElement Object
(
[@attributes] => Array
(
[creator] => dom
[sxe_attribute] => added by simplexml
)

)

[sxe_element] => SimpleXMLElement Object
(
[@attributes] => Array
(
[creator] => simplexml
[dom_attribute] => added by dom
)

)

)

What this illustrates is that both interfaces are operating on the same underlying object instance. Also, when you dom_import_simplexml, you can create and add new elements without reference to an ownerDocument (or documentElement).

So passing a SimpleXMLElement to another method does not mean the recipient is limited to using SimpleXML methods.

Hey Presto! Your telescope has become a pair of binoculars!
up
1
justinpatrin at php dot net
18 years ago
I've found that newer versions of PHP5 require some special syntax in order to properly convert between SimpleXML and DOM. It's not as easy as calling dom_import_simplexml() with a SimpleXML node. To get a proper DOM document (which you need to do most things) you need:
<?php
//$xml is a SimpleXML instance
$domnode = dom_import_simplexml($xml);
$dom = new DOMDocument();
$domnode = $dom->importNode($domnode, true);
$dom->appendChild($domnode);
?>

Switching back, though, is, well...simple.

<?php
//$dom is a DOMDocument instance
$xml = simplexml_import_dom($dom);
?>
up
1
h4ss4n3 at hyj4z1 dot me
4 years ago
//No need to initiate, import and append on example#1

(...)
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Erreur lors de la conversion du XML';
exit;
}

//$dom = new DOMDocument('1.0');
//$dom_sxe = $dom->importNode($dom_sxe, true);
//$dom_sxe = $dom->appendChild($dom_sxe);

//use ownerDocument propertie
echo $dom->ownerDocument->saveXML();

?>
up
-2
biniou at yopmail dot com
8 years ago
Very useful to add a CDATA node with SimpleXMLElement (use it like addChild) :

<?php
class My_SimpleXMLElement extends SimpleXMLElement {

public function
addChildWithCData($name, $value = NULL) {
$new_child = $this->addChild($name);

$node = dom_import_simplexml($new_child);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($value));

return
$new_child;
}
}
To Top