-1

I am struggling to read this XML string when applying simplexml_load_string

The XML is as below:-

<?xml version="1.0" encoding="utf-8"?>                  
 <Pport xmlns:fc="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" xmlns:ct="http://www.thalesgroup.com/rtti/PushPort/CommonTypes/v1" ts="2015-04-02T21:40:43.6505299+01:00" version="12.0" xmlns="http://www.thalesgroup.com/rtti/PushPort/v12">
    <uR updateOrigin="TD">          
        <TS rid="201504021071622" uid="L24370" ssd="2015-04-02">        
            <fc:Location tpl="SHRDHST" wta="21:39:30" wtd="21:40" pta="21:40" ptd="21:40">  
                <fc:arr at="21:39" src="TD" />
                <fc:dep at="21:40" src="TD" />
                <fc:plat>2</fc:plat>
            </fc:Location>  
        </TS>       
    </uR>           
 </Pport>

With var_dump(simplexml_load_string($xml)); I get the following:-

object(SimpleXMLElement)#3 (2) {
 ["@attributes"]=>
 array(2) {
  ["ts"]=>
  string(33) "2015-04-02T21:40:43.6505299+01:00"
  ["version"]=>
  string(4) "12.0"
}
["uR"]=>
object(SimpleXMLElement)#4 (2) {
 ["@attributes"]=>
 array(1) {
  ["updateOrigin"]=>
  string(2) "TD"
}
["TS"]=>
object(SimpleXMLElement)#5 (1) {
  ["@attributes"]=>
  array(3) {
    ["rid"]=>
    string(15) "201504021071622"
    ["uid"]=>
    string(6) "L24370"
    ["ssd"]=>
    string(10) "2015-04-02"
   }
  }
 }
}

I am trying to read the contents within the <fc:Location band........ for example the "21:39" arrival time. However the var_dump(simplexml_load_string($xml)); does not show this part of the XML as an array as you can see above. It seems to miss off everything between <fc:Location................ and </fc:Location>

I was hoping that using the code below I could read the "21:39" arrival time.

$newxml = simplexml_load_string($xml);
foreach ($newxml->TS->fc:Location->fc:arr->movedata as $movedata) {
   $uid=$movedata['at'];
   echo $uid;
}   

All I get is a syntax error ........for the ":".

hakre
  • 193,403
  • 52
  • 435
  • 836
user2635961
  • 379
  • 3
  • 19
  • 1
    I think it has to do with your `fc` namespace. See http://stackoverflow.com/questions/740730/parse-an-xml-with-simplexml-which-has-multiple-namespaces – nateevans Apr 02 '15 at 21:47
  • 1
    You're confusing the output of `var_dump` with the actual data and XML structure. Like a `var_dump` of a database connection wouldn't dump all data of the database it represents, it's similar with the **var_dump** of a **SimpleXMLElement** from a string or file: It doesn't show the whole data, just a selection of the underlying structure. In reality there isn't even an array, the output is just meant figuratively and a simplification. – hakre Apr 02 '15 at 22:19

1 Answers1

1

You'd get those attributes by traversing properly and using the attributes() method.

$newxml = simplexml_load_string($xml);
$children = $newxml->uR->TS->children('fc', true)->children('fc', true);

foreach($children as $movedata) {
    $attr = $movedata[0]->attributes();
    $uid = $attr['at'];
    echo $uid . '<br />';
}

Note that namespaces can be accessed by using the children() method.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks Adeneo that worked. Using this method how would I get the platform number .ie 2 in the above case – user2635961 Apr 02 '15 at 22:24
  • hint: that's the last child, and `2` is the nodeValue – adeneo Apr 02 '15 at 22:29
  • Am I on the right tracks with $children=$xml->uR->TS->children('fc',true)->children('fc',true)->nodeValue; – user2635961 Apr 02 '15 at 23:03
  • Try `$children->plat[0]->__toString()` – adeneo Apr 02 '15 at 23:38
  • Sorry for being a bit slow on this but where would that code sit relative to the above code as I'm gettinga syntax error? $children=$xml->uR->TS->children('fc',true)->children('fc',true); $attr=$children->plat[0]_toString(); echo $attr. ''; – user2635961 Apr 02 '15 at 23:47
  • Just got it to work. I now have a separte problem in that I have different length streams of this XML coming through causing other issues. Need to get my head around that. Thanks for your help again – user2635961 Apr 03 '15 at 00:01