GRBZ Posted August 22 Share Posted August 22 Hi I'm not yet very familiar with PHP, XML and curl, so I need your help. Trying to get flight number, but here is something weird. This is the XML-file I get from Finavia API; <?xml version="1.0" encoding="UTF-8"?> <flights xmlns="http://www.finavia.fi/FlightsService.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <arr> <header> <timestamp>2024-08-22T01:52:35Z</timestamp> <from>Finavia</from> <description>Arrival flight data</description> </header> <body> <flight> <h_apt>KEM</h_apt> <fltnr>AY519</fltnr> <sdt>2024-08-22T16:55:00Z</sdt> <sdate>20240822</sdate> <acreg>OHATH</acreg> <actype>AT75</actype> ................ </flight> </body> </arr> Using this PHP code to get started; <?php $xml = simplexml_load_file('flights.xml'); $otsikot = $xml->xpath('//flight[fltnr="AY456"]/acreg'); // Tulosta tulokset foreach ($otsikot as $otsikko) { echo $otsikko . "<br>"; } ?> This returns empty page, but when I remove lines; <flights xmlns="http://www.finavia.fi/FlightsService.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> It works. Why is this? AI told me to do it like this; $otsikot = $xml->xpath('//flights:flight/flights:fltnr', $xml->getNamespaces()); But this crashes the whole page, so what is wrong and what should I do. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/323191-xml-reading-problem/ Share on other sites More sharing options...
Solution requinix Posted August 22 Solution Share Posted August 22 When you do stuff with SimpleXML, by default it starts in the "" namespace. But your XML doesn't use the "" namespace. It uses the "http://www.finavia.fi/FlightsService.xsd" namespace. Set up that namespace with XPath using registerXPathNamespace(), then try querying it with a namespace prefix. // prefix can be anything, xmlns URL is what matters $xml->registerXPathNamespace('f', 'http://www.finavia.fi/FlightsService.xsd'); // names need an f: prefix $otsikot = $xml->xpath('//f:flight[f:fltnr="AY456"]/f:acreg'); Quote Link to comment https://forums.phpfreaks.com/topic/323191-xml-reading-problem/#findComment-1633245 Share on other sites More sharing options...
GRBZ Posted September 3 Author Share Posted September 3 Late answer but big thanks. This works now. Quote Link to comment https://forums.phpfreaks.com/topic/323191-xml-reading-problem/#findComment-1634587 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.