简体   繁体   English

使用php DOMDocument进入子节点

[英]Getting into child nodes with php DOMDocument

I know this has been asked a few times before but I really can't seem to figure it out from other examples on here. 我知道这已经被问过几次了,但是我似乎无法从这里的其他例子中弄清楚。

    $classname = "gallery2";
    $pageDOM = new DOMDocument();
    $pageDOM->loadHTML($kl);

    $xpath = new DOMXPath($pageDOM);
    $ul = $xpath->query("//ul[@class='$classname']");
    $ulNode = $ul->item(0);

    $ulHTML = new DOMDocument();

    $ulHTML->appendChild($ulHTML->importNode($ulNode, TRUE));
    $ulHTMLsaved = $ulHTML->saveHTML();

var_dump on $ulHTMLsaved returns all the markup that I want, but then how do I do a foreach and get into to a child node from the existing variable that I've set? $ ulHTMLsaved上的var_dump返回我想要的所有标记,但是该如何执行foreach并从我设置的现有变量进入子节点?

$ulHTMLsaved contains something like the following & I want to grab various attributes from other nodes nested within all the li nodes. $ulHTMLsaved包含以下内容,我想从嵌套在所有li节点内的其他节点获取各种属性。

<ul>
    <li>
        <figure>
            <a>
                <img src="img.jpg" alt="">
            </a>
        </figure>
    </li>
    <li>
        <figure>
            <a href="">
                <img src="img.jpg" alt="">
            </a>
        </figure>
    </li>
</ul>

You can get into child elements by searching for them with the following: 您可以通过以下方式搜索子元素来进入它们:

getElementsByTagName 的getElementsByTagName

getElementById 的getElementById

There will probably be a more generic one too for classes similar to javascript's querySelectorAll , but not sure what that is currently. 对于类似于javascript的querySelectorAll类,可能还会有一个更通用的类,但不确定当前是什么。

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

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