Klausos Klausos
Klausos Klausos

Reputation: 16050

Read xml file and load its content into the array

I need to read xml file and load its content into the array. What is the simplest way to do this?

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

Upvotes: 0

Views: 18854

Answers (1)

Makesh
Makesh

Reputation: 1234

You can try using SimpleXMLElement of php

$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";
 }

Reference : http://php.net/manual/en/class.simplexmlelement.php

Upvotes: 7

Related Questions