PHP.nl

dom_import_simplexml

dom_import_simplexml

Gets a DOMAttr or DOMElement object from a SimpleXMLElement object

 **dom_import_simplexml** object $node

This function takes the given attribute or element (a instance) and creates a or node, repectively. The new refers to the same underlying XML node as the . node``SimpleXMLElement``DOMAttr``DOMElement``DOMNode``SimpleXMLElement

node The attribute or element node to import (a instance). SimpleXMLElement

The or . DOMAttr``DOMElement

**Voorbeeld: Import SimpleXML into DOM with **

<?php

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

if ($sxe === false) {
    echo 'Error while parsing the document';
    exit;
}

$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
    echo 'Error while converting XML';
    exit;
}

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

echo $dom->saveXML();

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

Voorbeeld: Import SimpleXML into DOM and modify SimpleXML through DOM

Error handling omitted for brevity.

<?php

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

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

simplexml_import_dom