phpcan t load xml filesimplexml_load_file中访问问题

&&&您需要以后才能回答,未注册用户请先。PHP中通过SimpleXMLElement配合DOMDocument提取XML中的HTML内容
PHP中的simplexml_load_file在解析标准XML时没问题, 但是有两点缺陷:
1. 默认会忽略CDATA的内容
2. 所有HTML标签会被忽略, 在上级节点中能看到, 但是无法通过xpath检索
第一点可以通过设置simplexml_load_file的LIBXML_NOCDATA来解决
第二点无法直接解决, 只能通过其他办法, 将HTML节点提取出后, 使用DOMDocument来抽取所要的内容.
相关代码例子如下:
$xml = simplexml_load_file(&embeded_html.xml&, null, LIBXML_NOCDATA);
$node = $xml-&xpath(&/PathToHere/ContentItem/DataContent&);
$children = $node[0]-&children();
$html = $children-&asXML();
//print_r($html);
$dom = new DOMD
$dom-&loadHTML($html);
//get content
$items = $dom-&getElementsByTagName('div');
foreach ($items as $item) {
if ($item-&getAttribute('class') == 'content-attr') {
echo $item-&nodeValue, PHP_EOL;
后来对方技术又提供了另一种解决的方案:
print_r($xml-&NewsItem-&NewsComponent-&ContentItem-&DataContent-&html-&body);
print_r($xml-&NewsItem-&NewsComponent-&ContentItem-&DataContent-&html-&body-&div[1]);
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
i have a problem of this error or how do i fix it, any help.
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "&?xml version="1.0" encoding="UTF-8" ?&&response&&psid /&&date&&![CDATA[]]&&/date&&name /&&email /&&status&&![CDATA[declined]]&&/status&&message&&![CDATA[This company has not yet been approved.]]&&/message&&price&0&/price&&/response&"
i am using this line
$xml = simplexml_load_file($json);
114k18142300
closed as too localized by , , , ,
This question is unlikely to help it is only relevant to a small geographic area, a specific moment in time,
or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making
this question more broadly applicable, .
If this question can be reworded to fit the rules in the , please .
$json should contain a file name/path. Not XML.
Use simplexml_load_string() to load an XML string.
So try this in stead:
$xml = simplexml_load_string($json);
Also naming the variable json is weird as it suggests it will contain something to do with JSON. But JSON and XML are totally different things.
7,48121226
Not the answer you're looking for?
Browse other questions tagged
Upcoming Events
Stack Overflow works best with JavaScript enabled您现在的位置: >
php xml文件操作代码(一)
联盟电脑example.xml文件: 代码如下:?php $xml = simplexml_load_file('example.xml'); //创建SimpleXML对象 print_r($xml); //输出XML ?
代码如下:?xml version='1.0'? departs depart nameproduction support/name employees employee serial_no100001/serial_no nameSimon/name age24/age birthday/birthday salary5000.00/salary bonus1000.00/bonus /employee employee serial_no100002/serial_no nameElaine/name age24/age birthday/birthday salary6000.00/salary bonus2000.00/bonus /employee /employees /depart depart nametesting center/name employees employee serial_no110001/serial_no nameHelen/name age23/age birthday/birthday salary5000.00/salary bonus1000.00/bonus /employee /employees /depart /departs 代码如下:?php $xml = simplexml_load_file('example.xml'); //创建SimpleXML对象 var_dump($xml); //输出XML ? 代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML文件 foreach($xml-depart as $a) //循环读取XML数据中的每一个depart标签 { echo "$a-name BR"; //输出其中的name属性 } ?
代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML文件 echo $xml-depart-name[0]; //输出节点 ? 代码如下:?php $xml = simplexml_load_file('example.xml'); foreach ($xml-depart-children() as $depart) //循环读取depart标签下的子标签 { var_dump($depart); //输出标签的XML数据 } ?
代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML文件 $result = $xml-xpath('/departs/depart/employees/employee/name'); //定义节点 var_dump($result); //输出节点 ?
代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML $xml-depart-name[0] = "Human Resource"; //修改节点 ? 代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML数据 echo $xml-asXML(); //标准化XML数据 ? 代码如下:?php $xml = simplexml_load_file('example.xml'); //读取XML数据 $newxml = $xml-asXML(); //标准化XML数据 $fp = fopen("newxml.xml", "w"); //打开要写入XML数据的文件 fwrite($fp, $newxml); //写入XML数据 fclose($fp); //关闭文件 ?
(责任编辑:联盟电脑)
更多相关资讯
【联盟电脑】部分内容自于互联网,其言论不代表本站观点,若本站侵犯到您的版权,请与站长联系,我们将在第一时间核实并删除!
版权所有 & 【联盟电脑】 | 专注分享有价值、实用的电脑技术和知识
Copyright &
All rights reserved. 京ICP备号&&&&&&&& &&&&&&
这篇文章主要介绍了php使用simplexml_load_file加载XML文件并显示XML的方法,实例分析了simplexml_load_file操作XML文件的技巧,非常具有实用价值,需要的朋友可以参考下本文实例讲述了php使用simplexml_load_file加载XML文件并显示XML的方法。分享给大家供大家参考。具体实现方法如下:&?php$xml = simplexml_load_file("sample.xml");echo htmlspecialchars($xml-&asXML());?&sample.xml文件内容如下&library&&book&&title&A&/title&&author gender="female"&B&/author&&description&C&/description&&/book&&book&&title&C&/title&&author gender="male"&D&/author&&description&E&/description&&/book&&book&&title&F&/title&&author gender="male"&G&/author&&description&H&/description&&/book&&/library&希望本文所述对大家的php程序设计有所帮助。
更多信息请查看
【】&&&&&【】
由于各方面情况的不断调整与变化,易贤网所提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
& (04月17日)
云南各地招聘
&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp会员注册
本站不参与评论,(&&点此深度交流 )
自觉遵守:爱国、守法、自律、真实、文明的原则
尊重网上道德,遵守中华人民共和国各项有关法律法规
严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的评论
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
您在本站发表的评论,本站有权保留、转载、引用或者删除
参与本评论即表明您已经阅读并接受上述条款
将该文分享到:

我要回帖

更多关于 php simplexml 安装 的文章

 

随机推荐