PHP使用简单XML处理XML提要


PHP handling XML Feed with Simple XML

我正在使用英国气象局的api来使用一些天气信息。XML的布局如下:

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

我的问题是,我想要的重要数据在Rep中,但在使用simplexml时似乎无法处理它?我应该换一种方式吗?还是我错过了什么?

试试这个来获得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...
}

更正XML后,我建议使用DOMDocument和DOMXPath,如下所示:

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