使用XMLReader解析XML+;PHP中的SimpleXML
最近,我了解了如何在PHP中使用XMLReader和SimpleXML解析大型xml文件。我尝试将上述教程的代码改编为我的php过程,如下所示:使用XMLReader解析XML+;PHP中的SimpleXML,php,xml-parsing,simplexml,xmlreader,Php,Xml Parsing,Simplexml,Xmlreader,最近,我了解了如何在PHP中使用XMLReader和SimpleXML解析大型xml文件。我尝试将上述教程的代码改编为我的php过程,如下所示: $xml_url = "http://localhost/rest/server.php?wstoken=".$token&function=contents"; $reader = new XMLReader; $reader->open($xml_url); while($reader->read())
$xml_url = "http://localhost/rest/server.php?wstoken=".$token&function=contents";
$reader = new XMLReader;
$reader->open($xml_url);
while($reader->read()){
if($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'SINGLE'){
$doc = new DOMDocument('1.0','UTF-8');
$xml = simplexml_import_dom($doc->importNode($reader->expand(), true));
//$titleString = (string) $xml->description;
echo $xml->description;
}
}
通过url调用的XML文件为so(XML版本为):
其他单个标记(用“红色”标记)具有相同的结构,我也想打印它们的“说明”
上述php过程的输出是:第1列第1行的错误:文档末尾的额外内容。任何帮助都很好。功能应该足够:
$xml=simplexml_load_file('http://dl.dropbox.com/u/72519118/response.xml');
var_dump($xml->xpath('//SINGLE/KEY[@name="description"]/VALUE/text()'));
上述var\u dump
输出:
array(3) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(1703) "<div class="no-overflow">..."
}
[1]=>
object(SimpleXMLElement)#3 (1) {
[0]=>
string(9906) "<div class="no-overflow">..."
}
[2]=>
object(SimpleXMLElement)#4 (1) {
[0]=>
string(4114) "<div class="no-overflow">..."
}
}
上述产出:
string(1703) "<div class="no-overflow"><p>..."
string(9906) "<div class="no-overflow"><h3>..."
string(4114) "<div class="no-overflow"><h3>..."
string(1703)“…”
字符串(9906)“…”
字符串(4114)“…”
您不能简单地使用simplexml\u load\u file
和xpath吗?谢谢您的回复,我使用了它,但现在的结果是:第1列第1行的错误:文档是empty@Dozent我再次尝试了这段代码,仍然得到了相同的结果。你确定你的server.php
生成的有效XML与你在DropBox上发布的相同吗?是的,现在它可以工作了,我错误地将内容类型设置为:application/XML而不是text/html。现在的结果是:数组(15){[0]=>object(simplexmlement){2(0){}[1]=>object(simplexmlement){3(0)}[2]=>object(simplexmlement){4(0){……老实说,我不知道如何从这组数组中提取值。你能告诉我方法吗?Thanks@Dozent更新了我的答案。
string(1703) "<div class="no-overflow"><p>..."
string(9906) "<div class="no-overflow"><h3>..."
string(4114) "<div class="no-overflow"><h3>..."