我在學習如何用PHP的簡單XML解析XML。我的代碼是:用PHP的simpleXML解析XML
<?php
$xmlSource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?> <Document xmlns=\"http://www.apple.com/itms/\" artistId=\"329313804\" browsePath=\"/36/6407\" genreId=\"6507\"> <iTunes> myApp </iTunes> </Document>";
$xml = new SimpleXMLElement($xmlSource);
$results = $xml->xpath("/Document/iTunes");
foreach ($results as $result){
echo $result.PHP_EOL;
}
print_r($result);
?>
當這運行它返回一個空白屏幕,沒有錯誤。如果我從文檔標記中刪除所有屬性,則返回:
myApp SimpleXMLElement Object ([0] => myApp)
這是預期結果。
我在做什麼錯?請注意,我無法控制XML源,因爲它來自Apple。
太棒了!你能指點我詳細解釋這個功能的文檔嗎? – SooDesuNe 2010-03-06 01:47:44
嗯,就是這樣的事情:就我所知,沒有關於如何處理默認名稱空間的文檔。 – 2010-03-06 16:37:19