php PHP5:在 DOMDocument 中查找根节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1205751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP5: Find Root Node in DOMDocument
提问by BlaM
I have a PHP5 DOMDocument and I try to find the root node (not the root element).
我有一个 PHP5 DOMDocument,我试图找到根节点(不是根元素)。
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?
我想获得“测试”的 DOMNode,以便我可以调用 - 例如 - hasChildNodes。我可以得到“documentElement”,但那是一个 DOMElement。也许我可以从那里去?
$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.)
(显然,我不仅要调用 hasChildNodes - 所以不,它无助于找到另一种方法来确定文档是否包含内容。这只是我的简单示例。最后我需要一个 DOMNode。)
采纳答案by PatrikAkerstrand
According to the PHP docs DOMElement is a subclass of DOMNode, so it should inherit the hasChildNodes()-method.
根据 PHP 文档DOMElement 是 DOMNode 的子类,因此它应该继承hasChildNodes()-method。
回答by Cristian Toma
DOMElementextends DOMNode.
DOMElement扩展了DOMNode。
You get the RootDOMElementby $d->documentElement.
您可以通过 $d->documentElement获得Root DOMElement。
回答by adatapost
DOM Model- The W3C has broken down the DOM into a tree structure of nodes of varying types. The Node interface is the base interface for all elements. All objects implementing this interface expose methods for dealing with children.
DOM 模型- W3C 已将 DOM 分解为不同类型节点的树结构。Node 接口是所有元素的基本接口。实现此接口的所有对象都公开了处理子项的方法。
$dom=new DomDocument;
$dom->Load("file.xml");
$root=$dom->documentElement; // Root node
回答by Cliffordlife
Prior to php 5.1.3 this guy has it licked
在 php 5.1.3 之前这家伙已经舔过了
https://macfoo.wordpress.com/2009/06/03/getting-the-root-node-from-an-xml-string
https://macfoo.wordpress.com/2009/06/03/getting-the-root-node-from-an-xml-string
/**
* function getXMLRootNode
* @param string An xml string
* @return string Return XML root node name
*/
function getXMLRootNode($xmlstr)
{
// Create DOM model
$doc = new DOMDocument();
// Load the XML string
if(!$doc->loadXML($xmlstr))
{
throw new Exception('Unable to parse XML string');
}
// Find the root tag name
$root = $doc->documentElement;
if(!isset($root))
{
throw new Exception('Unable to find XML root node');
}
if(!isset($root->nodeName))
{
throw new Exception('Unable to find XML root node name');
}
return $root->nodeName;
}
Cross Posted to SO Questions that I hit while trying to find how to do this pre 5.1.3
Cross Posted to SO Questions 我在 5.1.3 之前试图找到如何做到这一点时遇到的问题

