2013-09-23 16 views
1

更新:作爲數組進行投射確實有竅門。 See this response,因爲我沒有足夠的實力:)simplexml_load_string和不受歡迎的分析錯誤

我開始在這個問題上與許多潛在的罪魁禍首,但經過大量的診斷問題仍然存在,並沒有明顯的答案。

我想打印地名「Gaborone」,它位於此API加載的XML文件的第一個標記下的第一個標記下的第一個標記。我如何解析這個來返回這個內容?

<?php 

     # load the XML file 
     $test1 = (string)file_get_contents('http://www.afdb.org/fileadmin/uploads/afdb/Documents/Generic-Documents/IATIBotswanaData.xml'); 

     #throw it into simplexml for parsing  
     $xmlfile = simplexml_load_string($test1); 

     #output the parsed text 
     echo $xmlfile->iati-activity[0]->location[0]->gazetteer-entry; 

    ?> 

從未未能返回此:

Parse error: syntax error, unexpected '[', expecting ',' or ';' 

我試圖改變語法,以避免在標籤名稱的連字符,例如:

echo $xmlfile["iati-activity"][0]["location"][0]["gazetteer-entry"]; 

。 。 。但返回完全沒有;沒有錯誤,沒有來源。

我也試過基於theseotherwise-helpfulthreads調試,但沒有任何解決方案工作。在我的simplexml尋址中是否有明顯的錯誤?

回答

1

I've tried changing the syntax to avoid the hyphens in the tag names as such: echo $xmlfile["iati-activity"][0]["location"][0]["gazetteer-entry"];

您的問題在於,對象本機強制轉換爲數組不是遞歸的,因此您只對主鍵執行此操作。是的,你的猜測是正確的 - 由於語法問題,當使用返回值simplexml_load_string()時,你不應該處理對象屬性。相反,您應該遞歸地將它的返回值(stdclass)轉換爲數組。您可以使用此功能爲:

function object2array($object) { 
    return json_decode(json_encode($object), true); 
    } 

其餘:

// load the XML file 
    $test1 = file_get_contents('http://www.afdb.org/fileadmin/uploads/afdb/Documents/Generic-Documents/IATIBotswanaData.xml'); 

    $xml = simplexml_load_string($test1); 

    // Cast an object into array, that makes it much easier to work with 
    $data = object2array($xml); 

    $data = $data['iati-activity'][0]['location'][0]['gazetteer-entry']; // Works 

    var_dump($data); // string(8) "Gaborone" 
+0

完美。像'魅力'一樣工作,用'回聲'擊打它就像我期待的那樣回報結果。非常感謝! –

+0

不客氣! – Yang

+0

哎呦 - 還有一個問題:是否將數組轉換爲數組刪除我使用xpath的能力?這一直至少部分工作[在這個例子中](https://gist.github.com/wboykinm/6666168#file-iati2geojson-L22),但現在返回「致命錯誤:調用成員函數xpath()on a非對象「 –

0

我有一個類似的問題,使用SimpleXML的命令,直到我做了以下字符串替換解析XML:

//$response contains the XML string 
$response = str_replace(array("\n", "\r", "\t"), '', $response); //eliminate newlines, carriage returns and tabs 
$response = trim(str_replace('"', "'", $response)); // turn double quotes into single quotes 
$simpleXml = simplexml_load_string($response); 
$json = json_decode(json_encode($simpleXml)); // an extra step I took so I got it into a nice object that is easy to parse and navigate 

如果還是不行,有some talk over at PHP about CDATA not always being handled properly - PHP版本的依賴。

你可以試試這個代碼調用simplexml_load_string功能之前:

if(strpos($content, '<![CDATA[')) { 
    function parseCDATA($data) { 
     return htmlentities($data[1]); 
    } 
    $content = preg_replace_callback(
     '#<!\[CDATA\[(.*)\]\]>#', 
     'parseCDATA', 
     str_replace("\n", " ", $content) 
    ); 
} 

我重讀這一點,我認爲你的錯誤是在你的最後一行發生 - 試試這個:

回聲$ XMLFILE - > { 'IATI活動'} [0] - >位置[0] - > { '地名索引條目'};

+0

感謝您的建議,但恐怕沒有做到這一點。解析錯誤是相同的,並且尋址仍然不返回指示的對象。 –

+0

我添加了另一種可能性 - 因爲你在'['我得到了錯誤',我想這是CDATA導致的問題,但也檢查出這個計算器:http://stackoverflow.com/questions/14236607/xml-data- cleanup-before-calling-simplexml –

+0

當你訪問元素而不是解析xml時,你的錯誤是否發生在最後一行?如果是這樣,解決方案添加回答上面。 –