Php 如何使用xpath提取以下内容的所有属性?

Php 如何使用xpath提取以下内容的所有属性?,php,html,xpath,Php,Html,Xpath,我有以下代码: <code> <div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist"> <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-focus" role="tab" aria-exp

我有以下代码:

<code>
    <div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-focus" role="tab"
            aria-expanded="false" tabindex="0">
            <a><img class="img" alt="NOS" src="/images/small-nos.png"><span>10:00</span>NOS</a></h3>

        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel"
             style="display: none; ">
            <a href="/news1276" target="_blank">Today's headlines</a>

            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-focus" role="tab"
                aria-expanded="false" tabindex="0">
                <a><img class="img" alt="SBS" src="/images/small-sbs.png"><span>11:00</span>SBS</a></h3>

            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel"
                 style="display: none; ">
                <a href="/news1278" target="_blank">Nieuws</a>
            </div>
        </div>
</code>

我现在做的是:

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    $xpath = new DOMXPath($dom);
    $accordions = $xpath->query("//div[@id='accordion']/div");

// Iterate events
for($i = 0; $i < ($accordions->length); $i++) {

    // The node
    $event = $accordions->item($i);

    // The urls of the node
    $urls = $xpath->evaluate('.//a/@href', $event);

    if ($urls->length > 0) 
    {
        $result[$i] .= $urls->item(0)->value;
    }
}
//将html解析为DOMDocument
$dom=新的DOMDocument();
@$dom->loadHTML($html);
$xpath=newdomxpath($dom);
$accordions=$xpath->query(“//div[@id='accordion']/div”);
//迭代事件
对于($i=0;$i<($accordions->length);$i++){
//节点
$event=$accordions->item($i);
//节点的URL
$URL=$xpath->evaluate('.//a/@href',$event);
如果($URL->长度>0)
{
$result[$i]。=$url->item(0)->value;
}
}
这让我得到了我想要的网址。然而,我也在尝试获取
中的所有元素,我想在h3的迭代中检索图像和所有span,但这让我头疼,因为我尝试了各种各样的事情。谁能帮我解释一下吗

谢谢


Al

您所需要做的就是遍历DOM树。检查有关节点属性的文档,如的parentNode、childNodes、nextSibling等。另外,纳克确信您理解了基本概念:感谢您的回复,并为我指出了图表,这肯定会有所帮助。