0

I'm breaking my head about this one for 2 days now, so I hope someone can help a bit.

I need the data from an XML, which I get from a website. The code returns a nice list, but missing the time array (array from an array). Here is my code :

<?php 
$url="http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected=";
echo $url;

$sxml = Simplexml_load_file($url);
var_dump($sxml);
foreach ($sxml ->ForecastGraphItems ->WindForecastingGraphItem as $type){
    echo 'FC ';
    echo $type ->Forecast."<br>";
    echo 'LF ';
    echo $type ->LoadFactor."<br>";
    echo 'DT ';
    echo $type ->Time[0]->DateTime;
    echo "<br>";
    echo 'BID ';
    echo $type ->Bid."<br>";
    echo 'RA '; 
    echo $type ->RunningAverage."<br>";
    echo "<br>";
}
?>

The XML format I get when I just manualy open the website looks like the code below, or check the Original website : http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected=

<?xml version="1.0" encoding="UTF-8"?>

<WindForecastingGraphDataResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Elia.PublicationService.DomainInterface.WindForecasting.v1">

    <ErrorMessage i:nil="true"/>
    <ForecastGraphItems>
        <WindForecastingGraphItem>
            <Bid>No</Bid>
            <Forecast>571.45</Forecast>
            <LoadFactor>0.32</LoadFactor>
            <RunningAverage>585.59</RunningAverage>
            <Time xmlns:a="http://schemas.datacontract.org/2004/07/System">
                <a:DateTime>2015-05-12T22:00:00Z</a:DateTime>
                <a:OffsetMinutes>120</a:OffsetMinutes>
            </Time>
        </WindForecastingGraphItem>
        <WindForecastingGraphItem>
            <Bid>No</Bid>
            <Forecast>562.95</Forecast>
            <LoadFactor>0.32</LoadFactor>
            <RunningAverage>578.47</RunningAverage>
            <Time xmlns:a="http://schemas.datacontract.org/2004/07/System">
                <a:DateTime>2015-05-12T22:15:00Z</a:DateTime>
                <a:OffsetMinutes>120</a:OffsetMinutes>
            </Time>

Now, when I var_dump($sxml), or if I try to get it, the sub-array for TIME dropped out... (see below). I also tried foreach ($sxml ->ForecastGraphItems ->WindForecastingGraphItem -> Time as $time){ ..., which worked, but returned empty.

Anyone can help why the 'time' array is empty ?

Below the echo for var_dump:

http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected=object(SimpleXMLElement)#1 (2) { ["ErrorMessage"]=> object(SimpleXMLElement)#2 (0) { } ["ForecastGraphItems"]=> object(SimpleXMLElement)#3 (1) { ["WindForecastingGraphItem"]=> array(481) { [0]=> object(SimpleXMLElement)#4 (5) { ["Bid"]=> string(2) "No" ["Forecast"]=> string(6) "571.45" ["LoadFactor"]=> string(4) "0.32" ["RunningAverage"]=> string(6) "585.59" ["Time"]=> object(SimpleXMLElement)#485 (0) { } } [1]=> object(SimpleXMLElement)#5 (5) { ["Bid"]=> string(2) "No" ["Forecast"]=> string(6) "562.95" ["LoadFactor"]=> string(4) "0.32" ["RunningAverage"]=> string(6) "578.47" ["Time"]=> object(SimpleXMLElement)#485 (0) { } }

John Conde
  • 217,595
  • 99
  • 455
  • 496
dwindey1
  • 7
  • 1

1 Answers1

0

As michi has commented already you'd like to get access to elements that are in a different namespace:

            <Time xmlns:a="http://schemas.datacontract.org/2004/07/System">
                <a:DateTime>2015-05-12T22:15:00Z</a:DateTime>
                <a:OffsetMinutes>120</a:OffsetMinutes>
            </Time>

The <Time> element sets for all it's children the prefix a to the XML namespace http://schemas.datacontract.org/2004/07/System. You're interested in two children with that prefix:

                <a:DateTime>2015-05-12T22:15:00Z</a:DateTime>
                <a:OffsetMinutes>120</a:OffsetMinutes>

You do this by telling the namespace of the children you're interested in, e.g. for the first element:

$type ->Time->children('http://schemas.datacontract.org/2004/07/System')->DateTime;

And that's it already.

Now to correct some misunderstanding:

  • that is not an array
  • yes, var_dump told you it's one, but it's just not.
  • var_dump and print_r are not particular useful with SimpleXMLElement as they lie about it. SimpleXMLElement::asXML() shows you the concrete XML, always.
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Indeed this was the sollution ! – dwindey1 May 18 '15 at 06:20
  • Hi @dwindey1: As you're a new user, please take a quick look here on how accepting an answer works (part of the vote system on Stackoverflow): [Meta: How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – hakre May 18 '15 at 12:50