最近我從here得到了關於如何使用PHP中的XMLReader和SimpleXML解析大型XML文件。我想上面提到的教程的代碼應用到我的PHP程序是這樣的:在PHP中使用XMLReader + SimpleXML解析XML
$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文件是這樣(XML版本是here):
其他單標籤(標有'紅色')具有相同的結構,我也想打印他們的'描述'。
上面提到的php程序的輸出是:第1行第1列的錯誤:文檔結尾處的額外內容。任何幫助都會很棒。
難道你不能簡單地使用'simplexml_load_file'和xpath? – Passerby