-1

我正在使用 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 时我似乎无法处理它?我应该以另一种方式这样做还是我错过了什么?

4

2 回答 2

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...
}
于 2013-08-28T10:04:39.840 回答
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

于 2013-08-28T10:04:56.617 回答