开发者

ignoring nested elements when parsing xml with php

probably a simple question to answer for someone:::

xml:

   <foobar>
     <foo>i am a foo</foo>
     <bar>i am a bar</bar>
     <foo>i am a <bar>bar</bar></foo>
   </foobar>

In the above, I want to display all elements that are <foo>. When the script gets to the line with the nested < bar > the result is "i am a bar" .. which isn't the result I had hoped for.

Is it not possible to print out the entire contents of that element as it is, so that i see: "i am a <bar>bar</bar>"

php:

$xml开发者_C百科 = file_get_contents('sample');
$dom = new DOMDocument;

@$dom->loadHTML($xml);

$resources= $dom->getElementsByTagName('foo');

foreach ($resources as $resource){
        echo $resource->nodeValue . "\n";
}


After some trolling and trying to do what I needed with SimpleXML, I arrived at the following conclusion. My issue with SimpleXML was where the elements are. If the xml is structured, and the hierarchy is standard ... I have no problem.

If the XML is a web page for example, and the <foo> element is anywhere, SimpleXML doesn't have a good facility like getElementsByTagName to pull out the element wherever it may be....

<?php
$doc = new DOMDocument();
$doc->load('sample');
$element_name = 'foo';
if ($doc->getElementsByTagName($element_name)->length > 0) {
        $resources = $doc->getElementsByTagName($element_name);

        foreach ($resources as $resource) {
                $id = null;

                if (!$resource->hasAttribute('id')) {
                        $resource->setAttribute('id', gen_uuid());
                }

                $innerHTML = null;
                $children = $resource->childNodes;
                foreach ($children as $child) {
                        $tmp_doc = new DOMDocument();
                        $tmp_doc->appendChild($tmp_doc->importNode($child,true));
                        $innerHTML .= rtrim($tmp_doc->saveHTML());
                }
                $resource->nodevalue = $innerHTML;
        }
} 
echo $doc->saveHTML();
?>


Rather than writing all that code, you might try XPath. That expression would be "//foo", which would get a list of all the elements in the document named "foo".

http://php.net/manual/en/simplexmlelement.xpath.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜