0

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;
2
  • it would better i think to skip json_*code functions looks pointless to me Commented Jul 20, 2012 at 8:21
  • Depending on the XML it's not possible: XML is "more powerful" than simple arrays and thus you cannot map a XML directly onto such a structure. Commented Jul 20, 2012 at 8:21

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.