如何通过索引获取 PHP DOMDocument 的子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6041073/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:10:57  来源:igfitidea点击:

How to get a child of PHP DOMDocument by index

phpdom

提问by bingjie2680

I am trying to get a child of a PHP DOMDocument. Say I have a DOM document like this:

我正在尝试获取 PHP DOMDocument 的子项。假设我有一个这样的 DOM 文档:

<div>
   <h1 ></h1>
   <div id=2></div>
   <div class="test"></div>
...
</div>

I have a index number 3. Then I need to get the element <div class="test"></div>. In the DOMDocument API, there isn't a method like children(3). Is there? How can I get a child with an index?

我有一个索引号 3。然后我需要获取元素<div class="test"></div>。在 DOMDocument API 中,没有像children(3). 在那儿?我怎样才能得到一个有索引的孩子?

回答by lonesomeday

You canuse childNodes. This is a property of a DOM element that contains a NodeList containing all the element's children. Ideally you'd be able to do $el->childNodes->item(2)(note that it's 0-based, not 1-based, so 2 is the third item). However, this includes text nodes. So it's hard to predict what number your node will be. This probably isn't the best solution.

可以使用childNodes. 这是一个 DOM 元素的属性,该元素包含一个包含所有元素子元素的 NodeList。理想情况下,您可以这样做$el->childNodes->item(2)(请注意,它是基于 0 的,而不是基于 1 的,因此 2 是第三项)。但是,这包括文本节点。因此很难预测您的节点将是多少。这可能不是最好的解决方案。

You could go with alexn's solution(getElementsByTagName('*')->item(2)), but again this has its drawbacks. If your nodes have child nodes, they will also be included in the selection. This could throw your calculation off.

您可以使用alexn 的解决方案( getElementsByTagName('*')->item(2)),但这同样有其缺点。如果您的节点有子节点,它们也将包含在选择中。这可能会打乱你的计算。

My preferred solution would be to use XPath: it's probably the most stable solution, and not particularly hard.

我的首选解决方案是使用 XPath:它可能是最稳定的解决方案,而且不是特别难。

You'll need to have created an XPath object with $xpath = new DOMXPath($document)somewhere, where $documentis your DOMDocument instance. I'm going to assume that $elis the parent div node, the "context" that we're searching in.

您需要在$xpath = new DOMXPath($document)某处创建一个 XPath 对象,$document您的 DOMDocument 实例在哪里。我将假设这$el是父 div 节点,即我们正在搜索的“上下文”。

$node = $x->query('*', $el)->item(2);

Note that, again, we're using a 0-based index to find which element in the selection it is. Here, we're looking at child nodes of the top level divonly, and *selects only element nodes, so the calculations with text nodes are unnecessary.

请注意,我们再次使用基于 0 的索引来查找它是选择中的哪个元素。在这里,我们只查看顶层的子节点div,并且*只选择元素节点,因此不需要使用文本节点进行计算。

回答by alexn

If you use DOMDocument you can use getElementsByTagName('*')which returns a DomNodeList with all elements in your document. You can then invoke the itemfunction which takes an index as a parameter:

如果使用 DOMDocument,则可以使用getElementsByTagName('*')which 返回包含文档中所有元素的 DomNodeList。然后,您可以调用将item索引作为参数的函数:

$nodes = $dom->getElementsByTagName('*');
$targetNode = $nodes->item(3);

回答by Khurram Ijaz

try this

尝试这个

 foreach($dom->getElementsByTagName('div') as $div) { 
        $class = $div->getAttribute('class');
    } 

now you can match the class or id attribute of that particular div and do what ever. this is not the solution but helps you find the contents and attributes with all divs' . hope it helps.

现在您可以匹配该特定 div 的 class 或 id 属性并执行任何操作。这不是解决方案,而是帮助您找到所有 div' 的内容和属性。希望能帮助到你。

回答by Steven Vachon

Try this:

尝试这个:

$dom->childNodes->item(3)