加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – simplexml不加载标记类?

发布时间:2020-12-13 15:58:44 所属栏目:PHP教程 来源:网络整理
导读:我有一些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
我有一些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
*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读