simplexml_import_dom

(PHP 5)

simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.

Описание

SimpleXMLElement simplexml_import_dom ( DOMNode $node [, string $class_name ] )

This function takes a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.

Список параметров

node

A DOM Element node

class_name

You may use this optional parameter so that simplexml_import_dom() will return an object of the specified class. That class should extend the SimpleXMLElement class.

Возвращаемые значения

Returns a SimpleXMLElement or FALSE on failure.

Примеры

Пример #1 Importing DOM

<?php
$dom 
= new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!
$dom) {
    echo 
'Error while parsing the document';
    exit;
}

$s simplexml_import_dom($dom);

echo 
$s->book[0]->title// blah
?>

Смотрите также

  • dom_import_simplexml()

Коментарии

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
2013-08-14 18:38:27
http://php5.kiev.ua/manual/ru/function.simplexml-import-dom.html
/*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
2016-11-10 20:58:15
http://php5.kiev.ua/manual/ru/function.simplexml-import-dom.html
Why not just:
simplexml_import_dom(dom_import_simplexml($SimpleXmlNode))
2023-06-06 03:07:37
http://php5.kiev.ua/manual/ru/function.simplexml-import-dom.html

    Поддержать сайт на родительском проекте КГБ