php function call to simplexml_load_string vs parse as array



I was wondering the best (in terms of time/efficiency) way to get data out out a XML file in PHP. I see two ways (open to other suggestions). 1. Load the xml, parse to array, call array elements



$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$array = json_decode($json, true);


and then call items in the array as normal



$array['response']['@attributes']['date']




  1. load the XML and call using the built in class methods


    $xml = file_get_contents($url, false, $context); $xml = simplexml_load_string($xml); $event->response->attributes()->{'date'}




My issue is wanting to call XML attributes. 1. I call in an array, but need to parse the array somehow at the start. But i think the individual calls would be quicker than 2. as I need to call the class function each time.


Overall i think they will be about the same, so is there a preferred method? (note: i know i will have overheads in how i am reading/getting the XML. If it is not directly related to improving the general efficiency, please don't flame me for it. The question is more to do with calling attribute efficiency gains)


No comments:

Post a Comment