2012-07-20 92 views
0

我需要讀取xml文件並將其內容加載到數組中。什麼是最簡單的方法來做到這一點?讀取xml文件並將其內容加載到數組中

$xml = json_decode(json_encode((array) simplexml_load_file("./test.xml")), 1); 
print_r $xml; 
+0

它會更好,我想跳過json_ *碼功能看起來毫無意義,我 – Gntem 2012-07-20 08:21:11

+0

取決於它的XML不可能:XML比簡單數組「更強大」,因此您不能將XML直接映射到這樣的結構。 – KingCrunch 2012-07-20 08:21:22

回答

7

您可以嘗試使用PHP的的SimpleXMLElement

$source = 'test.xml'; 

// load as string 
$xmlstr = file_get_contents($source); 
$xmlcont = new SimpleXMLElement($xmlstr); 
foreach($xmlcont as $url) 
{ 
    echo "{$url->loc} - {$url->lastmod} - {$url->changefreq} - {$url->priority}\r\n"; 
} 

參考:http://php.net/manual/en/class.simplexmlelement.php

相關問題