Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

php - PHP5: Find Root Node in DOMDocument

I have a PHP5 DOMDocument and I try to find the root node (not the root element).

Example:

<test>
    <element>
        <bla1>x</bla1>
        <bla2>x</bla2>
    </element>
    <element>
        <bla1>y</bla1>
        <bla2>y</bla2>
    </element>
    <element>
        <bla1>z</bla1>
        <bla2>z</bla2>
    </element>
</test>

I want to get the DOMNode of "test" so that I can call - for example - hasChildNodes. I can get the "documentElement", but that's a DOMElement. Maybe I can go from there?

$d = DOMDocument::loadXML($xml);
// [... do some stuff here to find document's root node ...]
if ($rootnode->hasChildNodes()) echo 'yayy!'

Who can fill the gap? I seem to be blind.

(Obviously it's not only hasChildNodes I want to call - so NO, it doesn't help to find another method to find out if the document contains stuff. That's just for my simple example. I need a DOMNode at the end.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

DOMElement extends DOMNode.

You get the Root DOMElement by $d->documentElement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...