Which is the best way to parse an XML file in PHP ?
First
Using the DOM object
//code
$dom = new DOMDocument();
$dom->load(
I have started to use XMLReader to parse the XML files. After doing a bit of googling around found it the best to way parse XML files as it does not load the whole XML file into memory. Say if suppose my XML files was of 5 MB, while parsing it using XMLReader 5MB of my memory does not get wasted.
//usage
$xml = new XMLReader();
$xml->XML($xmlString);
while($xml->read)
{
if($xml->localName == 'Something') // check if tag name equals something
{
//do something
}
}
Using XML Reader we can find if the current tag is an opening tag or closing tag and do the needful as required.