php – simplexml不加载标记类?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – simplexml不加载标记类?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些PHP页面抓取htML并将其加载到simpleXMl对象中.但是它没有获得元素的类

PHP

//load the html page wITh curl
$html = curl_exec($ch);
curl_close($ch);

$doc = new DOMDocument();
$doc->loadhtml($html);
$sxml = simplexml_import_dom($doc);

页面html.如果我做一个$html的var_dump显示它已被删除并存在于$html中

<li class="large">
        <a style="" id="ref_3" class="off" href="#" onmouSEOver="highlightme('07');return false;" onclick="req('379');return false;" title="">07</a>
    </li>

$doc和$sxml的VAR_dump(下面)显示现在缺少一个’off’类.不幸的是,我需要根据这个类来处理页面.

[8]=>
             object(SimpleXMLElement)#50 (2) {
              ["@attributes"]=>
              array(1) {
                ["class"]=>
                string(16) "large"
              }
              ["a"]=>
              string(2) "08"
            }

解决方法

使用simplexml_load_file和xpath,请参阅内联注释.

你所追求的是什么,真的,一旦找到你需要的元素就是这个

$row->a->attributes()->class=="off"

以下完整代码

// let's take all the divs that have the class "stff_grid"
$divs = $xml->xpath("//*[@class='stff_grid']");

// for each of these elements,let's PRint out the value inside the First p tag
foreach($divs as $div){
    print $div->p->a . PHP_EOL;

    // Now for each li tag let's print out the contents inside the a tag
    foreach ($div->ul->li as $row){

        // same as before
        print "  - " . $row->a;
        if ($row->a->attributes()->class=="off") print " *off*";
        print PHP_EOL;

        // or shorter
        // print "  - " . $row->a . (($row->a->attributes()->class=="off")?" *off*":"") . PHP_EOL;

    }
}
/* this outputs the following
PErson 1
  - 1 hr *off*
  - 2 hr
  - 3 hr *off*
  - 4 hr
  - 5 hr
  - 6 hr *off*
  - 7 hr *off*
  - 8 hr
Person 2
  - 1 hr
  - 2 hr
  - 3 hr
  - 4 hr
  - 5 hr
  - 6 hr
  - 7 hr *off*
  - 8 hr *off*
Person 3
  - 1 hr
  - 2 hr
  - 3 hr
  - 4 hr *off*
  - 5 hr
  - 6 hr
  - 7 hr *off*
  - 8 hr
*/

脚本宝典总结

以上是脚本宝典为你收集整理的php – simplexml不加载标记类?全部内容,希望文章能够帮你解决php – simplexml不加载标记类?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。