来自URL的XML multidimentional数组php

时间:2015-05-15 14:46:55

标签: php arrays xml

我现在已经打破了这个问题2天了,所以我希望有人可以帮助我。

我需要来自XML的数据,这是我从网站获得的。代码返回一个很好的列表,但缺少时间数组(数组中的数组)。 这是我的代码:

<?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>";
}
?>

我刚刚手动打开网站时获得的XML格式如下面的代码所示,或查看原始网站: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>

现在,当我var_dump($sxml)或者我试图获得它时,TIME的子数组退出了......(见下文)。 我也试过了foreach ($sxml ->ForecastGraphItems ->WindForecastingGraphItem -> Time as $time){ ...,虽然有效,但还是空了。

任何人都可以帮助为什么&#39; time&#39;数组是空的吗?

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){[&#34; ErrorMessage&#34;] =&gt; object(SimpleXMLElement)#2(0){}   [&#34; ForecastGraphItems&#34;] =&GT; object(SimpleXMLElement)#3(1){   [&#34; WindForecastingGraphItem&#34;] =&GT; array(481){[0] =&gt;   object(SimpleXMLElement)#4(5){[&#34; Bid&#34;] =&gt;字符串(2)&#34;否&#34;   [&#34;预测&#34;] =&GT; string(6)&#34; 571.45&#34; [&#34; LoadFactor&#34;] =&GT; string(4)&#34; 0.32&#34;   [&#34; RunningAverage&#34;] =&GT; string(6)&#34; 585.59&#34; [&#34;时间&#34;] =&GT;   object(SimpleXMLElement)#485(0){}} [1] =&gt;   object(SimpleXMLElement)#5(5){[&#34; Bid&#34;] =&gt;字符串(2)&#34;否&#34;   [&#34;预测&#34;] =&GT; string(6)&#34; 562.95&#34; [&#34; LoadFactor&#34;] =&GT; string(4)&#34; 0.32&#34;   [&#34; RunningAverage&#34;] =&GT; string(6)&#34; 578.47&#34; [&#34;时间&#34;] =&GT;   object(SimpleXMLElement)#485(0){}}

1 个答案:

答案 0 :(得分:0)

正如michi已经评论过你想要访问不同命名空间中的元素:

            <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>

<Time>元素为其所有孩子设置前缀 a 到XML命名空间 http://schemas.datacontract.org/2004/07/System 。你对两个有这个前缀的孩子感兴趣:

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

您可以通过告诉您感兴趣的孩子的名称空间来做到这一点,例如:对于第一个元素:

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

就是这样。

现在纠正一些误解:

  • 不是数组
  • 是的,var_dump告诉你它是一个,但事实并非如此。
  • var_dumpprint_r对于 SimpleXMLElement 并不特别有用,因为它们就是谎言。 SimpleXMLElement::asXML()始终显示具体的XML。