What is the best way to parse an XML file in PHP?
First
Using the DOM Object
//code $dom = new DOMDocument(); $dom->load("xml.xml"); $root = $dom->getElementsByTagName("tag"); foreach($root as $tag) { $subChild = $root->getElementsByTagName("child"); // extract values and loop again if needed }
Second
Using the simplexml_load Method
// code $xml = simplexml_load_string("xml.xml"); $root = $xml->root; foreach($root as $tag) { $subChild = $tag->child; // extract values and loop again if needed }
Note: These are the ones that I know of. If there is more filling.
You need to know which method is best suited for parsing large XML files, as well as which method is the fastest , regardless of how the method should be implemented
The size will vary from 500 KB to 2 MB. The parser should be able to parse both small and large files in minimal time with good memory usage, if possible.
xml php parsing domdocument simplexml
Zaje
source share