PHP使用Simple XML处理XML Feed

时间:2013-08-28 09:54:17

标签: php xml simplexml

我正在使用UK Met office 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时我似乎无法处理它?我应该采取另一种方式吗?或者我错过了什么?

2 个答案:

答案 0 :(得分: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...
}

答案 1 :(得分: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