turnin Posted March 20, 2013 Share Posted March 20, 2013 OK, I admit I'm a total newbie but been reading all day and making no progress in understanding. I have a script that calls an api, that api returns xml, like so https://dl.dropbox.com/u/40331447/samplereturn.xml I've tried SimpleXML and it fails as the return contains namespaces, I think. I will run this script once per hour for eg: I want to be able to send this to my mysql database and I'll eventually I'll query it based on the location fields What would be the easiest php method or library to parse this, so that each database entry is stored like so: ( from Parent_<tns:motorways> tns:name), (fromchild <tns:Locations> <tns:name>, <tns:congestion>, <Tns:direction>,<tns:lat>,<tns:lng> etc So the parent is in each entry along with each iteration of the children. Hope that makes sence , any help is really appreciated ! Cheers James. Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/ Share on other sites More sharing options...
requinix Posted March 20, 2013 Share Posted March 20, 2013 What's your code? Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419878 Share on other sites More sharing options...
turnin Posted March 20, 2013 Author Share Posted March 20, 2013 Currently I just have code that calls the api with curl.this echos the return. I tried xmlsimple and it returned an empty array. Not too sure where to start or even if im going the right way about doing it. Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419881 Share on other sites More sharing options...
requinix Posted March 20, 2013 Share Posted March 20, 2013 What's your code? Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419887 Share on other sites More sharing options...
turnin Posted March 20, 2013 Author Share Posted March 20, 2013 (edited) <?php $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, '[url=https://infoconnect1.highwayinfo.govt.nz/ic/jbi/TrafficConditions2/REST/FeedService/]https://infoconnect1.highwayinfo.govt.nz/ic/jbi/TrafficConditions2/REST/FeedService/[/url]'); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_HTTPHEADER, array ('username:xxxx' , 'password: xxxx')); $response = curl_exec($handle); curl_close($handle); $xml = new SimpleXML($response); $motorway=$xml -> 'tns:motorways' //print_r($response); print_r($motorway); ?> Edited March 20, 2013 by ignace Added code tags Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419892 Share on other sites More sharing options...
Solution requinix Posted March 20, 2013 Solution Share Posted March 20, 2013 (edited) SimpleXML only allows you to work in a single namespace in a time. It starts in the empty namespace (xmlns="") so if you do $xml->motorways it will try to find a in that namespace. Which doesn't exist in your XML. Use children to switch namespaces. $motorways = $xml->children("tns", true)->trafficConditions->motorways;(You don't need to children() every time after, only the first time really just to change the namespace.) [edit] is a child of . You have to go through that first. Also note that there may be more than one . Edited March 20, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419894 Share on other sites More sharing options...
turnin Posted March 20, 2013 Author Share Posted March 20, 2013 Got it. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419898 Share on other sites More sharing options...
turnin Posted March 21, 2013 Author Share Posted March 21, 2013 Requinix, Got it parsing nicely, thanks heaps for helping, appreciated Quote Link to comment https://forums.phpfreaks.com/topic/275914-advice-on-parsing-xmlns-with-php/#findComment-1419991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.