使用 PHP DOMXpath 遍历子节点?

发布于 2024-12-12 02:32:29 字数 1062 浏览 0 评论 0原文

我无法理解 childNode 中到底存储了什么。理想情况下,我想对每个子节点执行另一个 xquery,但似乎无法直接解决。这是我的场景: 数据:

<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>

和代码:

$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? }

对于“某事”的每个元素(即 $n),我想访问两段文本和 href 的值。我尝试使用 childNode 和另一个 xquery 但无法让任何东西工作。任何帮助将不胜感激!

I'm having some trouble understanding what exactly is stored in childNodes. Ideally I'd like to do another xquery on each of the child nodes, but can't seem to get it straight. 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" (i.e., $n) I want to access the values of the two pieces of text and the href. I tried using childNode and another xquery but couldn't get anything to work. Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆萌少年 2024-12-19 02:32:29

是的,您可以运行另一个 xpath 查询,类似这样:

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

    echo $other_nodes->length;
}

这将为您提供带有类 someelse 的内部 div,$xpath->query 方法的第二个参数告诉查询将此节点作为上下文,请参阅更多 https://www.php.net/manual/en/domxpath.query.php< /a>

Yes you can run another xpath query, something like that :

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 https://www.php.net/manual/en/domxpath.query.php

怼怹恏 2024-12-19 02:32:29

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;
}

Trexx had it but he missed the last sentence of the question:

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;
}
幸福还没到 2024-12-19 02:32:29

如果我正确理解你的问题,那么当我使用descendant:: 表达式时它就起作用了。尝试一下:

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

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

尽管有时使用 // 路径表达式组合查询就足以缩小搜索范围。 // 路径表达式从当前节点开始选择文档中与选择器匹配的节点。

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

然后循环遍历这些内容以找到您需要的内容。希望这有帮助。

If I understand your question correctly, it worked when I used the 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.

$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.

浅浅 2024-12-19 02:32:29

下面是一个代码片段,允许您访问每个具有类属性“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);`

Here is a code snippet that allows you to access the information contained within each of the nodes with class attribute "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);`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文