HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

simplexml_import_dom

Description

The simplexml_import_dom of SimpleXML for PHP gets a SimpleXMLElement object from a DOM node.

Syntax

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

Parameters

node

A DOM Element node

class_name

An object of the specified class. The class should extend the SimpleXMLElement class.

Return

Returns a SimpleXMLElement or null on failure.

Examples

1 · node · SimpleXMLElement · simplexml_load_file

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/xml/namespace.xml";

$node = simplexml_load_file($filename);

$return = simplexml_import_dom($node);

print_r($return);
SimpleXMLElement Object
(
    [person] => Array
        (
            [0] => SimpleXMLElement Object
                (
                )

            [1] => SimpleXMLElement Object
                (
                )

            [2] => SimpleXMLElement Object
                (
                )

        )

)

2 · node · SimpleXMLElement · simplexml_load_string

<?

$data =
<<<XML
<?xml version="1.0"?>
<people>
    <person xmlns:a="https://osbo.com">
        <a:first a:class="myclass" a:id="myid" a:title="mytitle">first</a:first>
        <a:last>last</a:last>
        <a:age>age</a:age>
    </person>
    <person xmlns:b="https://osbo.com">
        <b:first b:class="myclass" b:id="myid" b:title="mytitle">first</b:first>
        <b:last>last</b:last>
        <b:age>age</b:age>
    </person>
    <person xmlns:c="https://osbo.com">
        <c:first c:class="myclass" c:id="myid" c:title="mytitle">first</c:first>
        <c:last>last</c:last>
        <c:age>age</c:age>
    </person>
</people>
XML;

$node = simplexml_load_string($data);

$return = simplexml_import_dom($node);

print_r($return);
SimpleXMLElement Object
(
    [person] => Array
        (
            [0] => SimpleXMLElement Object
                (
                )

            [1] => SimpleXMLElement Object
                (
                )

            [2] => SimpleXMLElement Object
                (
                )

        )

)

3 · node · DOMNode

<?

$source =
<<<XML
<?xml version="1.0"?>
<people>
    <person xmlns:a="https://osbo.com">
        <a:first a:class="myclass" a:id="myid" a:title="mytitle">first</a:first>
        <a:last>last</a:last>
        <a:age>age</a:age>
    </person>
    <person xmlns:b="https://osbo.com">
        <b:first b:class="myclass" b:id="myid" b:title="mytitle">first</b:first>
        <b:last>last</b:last>
        <b:age>age</b:age>
    </person>
    <person xmlns:c="https://osbo.com">
        <c:first c:class="myclass" c:id="myid" c:title="mytitle">first</c:first>
        <c:last>last</c:last>
        <c:age>age</c:age>
    </person>
</people>
XML;

$node = new DOMDocument();
$node->loadXML($source);

$return = simplexml_import_dom($node);

print_r($return);
SimpleXMLElement Object
(
    [person] => Array
        (
            [0] => SimpleXMLElement Object
                (
                )

            [1] => SimpleXMLElement Object
                (
                )

            [2] => SimpleXMLElement Object
                (
                )

        )

)

4 · class_name

<?

class myclass extends SimpleXMLElement
{
}

$source =
<<<XML
<?xml version="1.0"?>
<people>
    <person xmlns:a="https://osbo.com">
        <a:first a:class="myclass" a:id="myid" a:title="mytitle">first</a:first>
        <a:last>last</a:last>
        <a:age>age</a:age>
    </person>
    <person xmlns:b="https://osbo.com">
        <b:first b:class="myclass" b:id="myid" b:title="mytitle">first</b:first>
        <b:last>last</b:last>
        <b:age>age</b:age>
    </person>
    <person xmlns:c="https://osbo.com">
        <c:first c:class="myclass" c:id="myid" c:title="mytitle">first</c:first>
        <c:last>last</c:last>
        <c:age>age</c:age>
    </person>
</people>
XML;

$node = new DOMDocument();
$node->loadXML($source);

$class_name = "myclass";

$return = simplexml_import_dom($node, $class_name);

print_r($return);
myclass Object
(
    [person] => Array
        (
            [0] => myclass Object
                (
                )

            [1] => myclass Object
                (
                )

            [2] => myclass Object
                (
                )

        )

)