读取xml文件并将其内容加载到数组中

Kla*_*sos 0 php simplexml

我需要读取xml文件并将其内容加载到数组中.最简单的方法是什么?

$xml = json_decode(json_encode((array) simplexml_load_file("./test.xml")), 1);
print_r $xml;
Run Code Online (Sandbox Code Playgroud)

Mak*_*esh 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";
 }
Run Code Online (Sandbox Code Playgroud)

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