Jump to content

XML "reading" problem


Go to solution Solved by requinix,

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/323191-xml-reading-problem/
Share on other sites

  • Solution

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');

 

Link to comment
https://forums.phpfreaks.com/topic/323191-xml-reading-problem/#findComment-1633245
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.