2013-08-28 103 views
-1

我正在使用英国Met Office api来使用一些天气信息。 XML是奠定了为以下几点:使用简单XML处理XML Feed

<SiteRep> 
    <Wx> 
    <Param name="F" units="C">Feels Like Temperature</Param> 
    <Param name="G" units="mph">Wind Gust</Param> 
    <Param name="H" units="%">Screen Relative Humidity</Param> 
    <Param name="T" units="C">Temperature</Param> 
    <Param name="V" units="">Visibility</Param> 
    <Param name="D" units="compass">Wind Direction</Param> 
    <Param name="S" units="mph">Wind Speed</Param> 
    <Param name="U" units="">Max UV Index</Param> 
    <Param name="W" units="">Weather Type</Param> 
    <Param name="Pp" units="%">Precipitation Probability</Param> 
    </Wx> 
<DV dataDate="2013-08-28T08:00:00Z" type="Forecast"> 
<Location i="22" lat="53.5797" lon="-0.3472" name="HUMBERSIDE AIRPORT" country="ENGLAND" continent="EUROPE"> 
    <Period type="Day" value="2013-08-28Z"> 
    <Rep D="SSW" F="19" G="9" H="59" Pp="0" S="4" T="20" V="VG" W="3" U="3">720</Rep> 
    </Period> 
</Location> 
<Location i="25" lat="53.8658" lon="-1.6606" name="LEEDS BRADFORD INTERNATIONAL AIRPORT" country="ENGLAND" continent="EUROPE"> 
    <Period type="Day" value="2013-08-28Z"> 
    <Rep D="SW" F="17" G="11" H="72" Pp="7" S="7" T="18" V="GO" W="7" U="3">720</Rep> 
    </Period> 
</Location> 
</DV> 
</SiteRep> 

如果我使用使用simplexml_load_file和print_r的加载这个XML饲料我得到以下输出:

[Wx] => SimpleXMLElement Object 
    (
     [Param] => Array 
      (
       [0] => Feels Like Temperature 
       [1] => Wind Gust 
       [2] => Screen Relative Humidity 
       [3] => Temperature 
       [4] => Visibility 
       [5] => Wind Direction 
       [6] => Wind Speed 
       [7] => Max UV Index 
       [8] => Weather Type 
       [9] => Precipitation Probability 
      ) 

    ) 

[DV] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [dataDate] => 2013-08-28T08:00:00Z 
       [type] => Forecast 
      ) 

     [Location] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [@attributes] => Array 
          (
           [i] => 22 
           [lat] => 53.5797 
           [lon] => -0.3472 
           [name] => HUMBERSIDE AIRPORT 
           [country] => ENGLAND 
           [continent] => EUROPE 
          ) 

         [Period] => SimpleXMLElement Object 
          (
           [@attributes] => Array 
            (
             [type] => Day 
             [value] => 2013-08-28Z 
            ) 

           [Rep] => 720 
          ) 

        ) 

       [1] => SimpleXMLElement Object 
        (
         [@attributes] => Array 
          (
           [i] => 25 
           [lat] => 53.8658 
           [lon] => -1.6606 
           [name] => LEEDS BRADFORD INTERNATIONAL AIRPORT 
           [country] => ENGLAND 
           [continent] => EUROPE 
          ) 

         [Period] => SimpleXMLElement Object 
          (
           [@attributes] => Array 
            (
             [type] => Day 
             [value] => 2013-08-28Z 
            ) 

           [Rep] => 720 
          ) 

        ) 

我的问题是至关重要的数据我想是内众议员但我似乎无法处理它使用simplexml时?我应该以另一种方式来做这件事吗?

+1

请给我们的代码。另外,你想从上述数据中提取什么? –

+2

最后您错过了''。 –

+1

并缺少'' – Bora

回答

1

试试这一个越来越Rep

$xml = simplexml_load_file('file.xml'); 

foreach($xml->DV->Location as $location) 
{ 
    $att = $location->Period->Rep->attributes(); 

    echo $att['D']; 
    echo $att['F']; 
    echo $att['G']; 
    echo $att['H']; 

    etc... 
} 
+0

对不起,我可能不清楚,它不是代表值,而是开放Rep标签内的属性值。 – Philwn

+0

@PhilNewell Sory,更新! – Bora

+0

非常感谢你 – Philwn

0

纠正你的XML后,我会建议使用DOMDocumentDOMXPath这样的:

<?php 

$xml = <<<EOF 
<SiteRep> 
    <Wx> 
     <Param name="F" units="C">Feels Like Temperature</Param> 
     <Param name="G" units="mph">Wind Gust</Param> 
     <Param name="H" units="%">Screen Relative Humidity</Param> 
     <Param name="T" units="C">Temperature</Param> 
     <Param name="V" units="">Visibility</Param> 
     <Param name="D" units="compass">Wind Direction</Param> 
     <Param name="S" units="mph">Wind Speed</Param> 
     <Param name="U" units="">Max UV Index</Param> 
     <Param name="W" units="">Weather Type</Param> 
     <Param name="Pp" units="%">Precipitation Probability</Param> 
    </Wx> 
    <DV dataDate="2013-08-28T08:00:00Z" type="Forecast"> 
     <Location i="22" lat="53.5797" lon="-0.3472" name="HUMBERSIDE AIRPORT" country="ENGLAND" continent="EUROPE"> 
      <Period type="Day" value="2013-08-28Z"> 
       <Rep D="SSW" F="19" G="9" H="59" Pp="0" S="4" T="20" V="VG" W="3" U="3">720</Rep> 
      </Period> 
     </Location> 
     <Location i="25" lat="53.8658" lon="-1.6606" name="LEEDS BRADFORD INTERNATIONAL AIRPORT" country="ENGLAND" continent="EUROPE"> 
      <Period type="Day" value="2013-08-28Z"> 
       <Rep D="SW" F="17" G="11" H="72" Pp="7" S="7" T="18" V="GO" W="7" U="3">720</Rep> 
      </Period> 
     </Location> 
    </DV> 
</SiteRep> 
EOF; 

$doc = new DOMDocument(); 
$doc->loadXML($xml); 
$xpath = new DOMXPath($doc); 

$query = '//SiteRep/DV/Location/Period/Rep'; 

$entries = $xpath->query($query); 

foreach ($entries as $entry) { 
    var_dump($entry->nodeValue); 
} 

如果你需要的属性看看DOMNode

+0

我没有什么可以纠正xml,因为它是由英国气象局提供的。如果您指的是DV WX和siterep缺少的结束标记,那是因为整个XML Feed超过了5000个位置。我只是拿了一个样品。我会研究DOMNode,因为它是我之后的代表属性。谢谢 – Philwn