2013-07-16 57 views
0

嘗試此查詢:如何讀取XML字符串作爲變量?

http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da

如何,我只是在建議作爲變量讀?我似乎無法找到解釋這一點的任何事情。

+0

通過使用[SimpleXML的](http://php.net/manual/en/book.simplexml.php )或[DOMDocument](http://php.net/manual/en/class.domdocument.php)。 [這裏是一個很好的解釋(http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) –

+0

而且有[幾個這裏的例子](http://stackoverflow.com/search?q=read+xml+with+php),它涵蓋了通過'simplexml_load_file()加載文件的過程' –

回答

1

您可以使用SimpleXmlIterator。這非常容易使用,並且您可以對將獲得的對象執行foreach。

Library source

例如用的file_get_contents或捲曲替換如果你喜歡:

$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da')); 
    foreach ($feed->suggestion as $suggestion) { 
     echo $value; 
     } 
相關問題