我想從遠程位置抓取xml文件中的數據,該位置包含所有節點中的CDATA信息,如下所示。 我使用以下PHP函數來獲取這些信息,但它不起作用,似乎無法從xml文件中捕獲CDATA標記。 問題是我的代碼段是否正確?如果這是錯誤的,你可以建議任何PHP代碼來獲取請求的信息?simplexml不讀取CDATA
<Items>
<Item ID="1">
<Name>Mountain</Name>
<Properties>
<Property Code="feature"><![CDATA[<ul><li>sample text</li></ul>]]></Property>
<Property Code="SystemRequirements"><![CDATA[Windows XP/Windows Vista]]></Property>
<Property Code="Description" Type="plain"><![CDATA[sample text2]]></Property>
</Properties>
</Item>
<Items>
,這是我的PHP代碼:
<?
function xmlParse($file, $wrapperName, $callback, $limit = NULL) {
$xml = new XMLReader();
if (!$xml->open($file)) {
die("Failed to open input file.");
}
$n = 0;
$x = 0;
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == $wrapperName) {
while ($xml->read() && $xml->name != $wrapperName) {
if ($xml->nodeType == XMLReader::ELEMENT) {
//$subarray[]=$xml->expand();
$doc = new DOMDocument('1.0', 'UTF-8');
$simplexml = simplexml_import_dom($doc->importNode($xml->expand(), true));
$subarray[]=$simplexml;
}
}
if ($limit == NULL || $x < $limit) {
if ($callback($subarray)) {
$x++;
}
unset($subarray);
}
$n++;
}
}
$xml->close();
}
echo '<pre>';
function func1($s) {
print_r($s);
}
xmlParse('myfile.xml', 'Item', 'func1', 100);
當我用的print_r($ S)打印對象;結果我看不到CDATA! 你有任何想法來檢索CDATA上下文嗎?
你看到的信息在這個問題? [從XMLReader打開的simplexml中的CData](http://stackoverflow.com/q/10057352/367456) - 這可能正是你的情況。其實它*就是你的情況。不,你不能相信帶有simplexmlelement的'print_r'或'var_dump'的輸出。它是騙你(參考:http://stackoverflow.com/q/16119597/367456; http://stackoverflow.com/q/3410520/367456) – hakre