php – simplexml不加载标记类?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – simplexml不加载标记类?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1628字,纯文字阅读大概需要3分钟。
内容图文
![php – simplexml不加载标记类?](/upload/InfoBanner/zyjiaocheng/773/05d2346b30ce41599b7e8c08fe89617b.jpg)
我有一些php从页面抓取html并将其加载到simplexml对象中.但是它没有获得元素的类
//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="#" onm ouseover="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不加载标记类?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。