简体   繁体   English

使用PHP DOMXpath遍历子节点?

[英]Traversing child nodes with PHP DOMXpath?

I'm having some trouble understanding what exactly is stored in childNodes. 我在理解childNodes中存储的内容时遇到了一些麻烦。 Ideally I'd like to do another xquery on each of the child nodes, but can't seem to get it straight. 理想情况下,我想在每个子节点上做另一个xquery,但似乎无法直截了当。 Here's my scenario: Data: 这是我的方案:数据:

<div class="something">
    <h3>
        <a href="link1.html">Link text 1</a>
    </h3>
    <div class"somethingelse">Something else text 1</div>
</div>
<div class="something">
    <h3>
        <a href="link2.html">Link text 2</a>
    </h3>
    <div class"somethingelse">Something else text 2</div>
</div>
<div class="something">
    <h3>
        <a href="link3.html">Link text 3</a>
    </h3>
    <div class"somethingelse">Something else text 3</div>
</div>

And the code: 和代码:

$html = new DOMDocument();
$html->loadHtmlFile($local_file);
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='something']");
foreach ($nodelist as $n) {
    Can I run another query here? }

For each element of "something" (ie, $n) I want to access the values of the two pieces of text and the href. 对于“某事”的每个元素(即$ n),我想访问两段文本和href的值。 I tried using childNode and another xquery but couldn't get anything to work. 我尝试使用childNode和另一个xquery但无法获得任何工作。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Yes you can run another xpath query, something like that : 是的,您可以运行另一个xpath查询,类似于:

foreach ($nodelist as $n)
{
    $other_nodes = $xpath->query('div[@class="somethingelse"]', $n);

    echo $other_nodes->length;
}

This will get you the inner div with the class somethingelse, the second argument of the $xpath->query method tells to query to take this node as context, see more http://fr2.php.net/manual/en/domxpath.query.php 这将使用somethingelse类获取内部div,$ xpath-> query方法的第二个参数告诉查询将此节点作为上下文,请参阅http://fr2.php.net/manual/en/domxpath .query.php

Trexx had it but he missed the last sentence of the question: Trexx有,但他错过了问题的最后一句话:

foreach ($nodelist as $n){
    $href = $xpath->query('h3/a', $n)->item(0)->getAttribute('href');
    $a_text = $xpath->query('h3/a', $n)->item(0)->nodeValue;
    $div_text = $xpath->query('div', $n)->item(0)->nodeValue;
}

If I understand your question correctly, it worked when I used the descendant:: expression. 如果我正确理解你的问题,那么当我使用descendant :: expression时它就有用了。 Try this: 尝试这个:

foreach ($nodelist as $n) {
    $other_nodes = $xpath->query('descendant::div[@class="some-descendant"]', $n);

    echo $other_nodes->length;
    echo $other_nodes->item(0)->nodeValue;
}

Although sometimes it's just enough to combine queries using the // path expression for narrowing your search. 虽然有时它只是使用//路径表达式组合查询来缩小搜索范围。 The // path expression selects nodes in the document starting from the current node that match the selector. // path表达式从与选择器匹配的当前节点开始选择文档中的节点。

$nodes = $xpath->query('//div[@class="some-descendant"]//div[@class="some-descendant-of-that-descendant"]');

Then loop through those for the stuff you need. 然后循环遍历那些你需要的东西。 Hope this helps. 希望这可以帮助。

Here is a code snippet that allows you to access the information contained within each of the nodes with class attribute "something": 这是一个代码片段,允许您使用类属性“something”访问每个节点中包含的信息:

$nodes_tracker = 0;
$nodes_array = array();
foreach($nodelist as $n){
    $info = $xpath->query('//h3//a', $n)->item($nodes_tracker)->nodeValue;
    $extra_info = $xpath->query('//div[@class="somethingelse"', $n)->item($nodes_tracker)->nodeValue;
    array_push($nodes_array, $info. ' - '. $extra_info . '<br>'); //Add each info to array  
    $nodes_tracker++;
}
print_r($nodes_array);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM