选择特定节点类型的值

时间:2018-03-24 23:54:01

标签: php xml

我需要在xml中获取某个特定类型的节点的值。目前我正在循环查看多天的结果,这里只显示了两个。我需要在降水量中获得Type RAIN的值。我需要再次循环吗?也许一会儿循环?或者我应该能够通过下面的if语句获取值?我将不得不在循环中做两次。

<xml>
    <locationResponseList>
        <locationResponse>
           <day>Day1</day>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>SNOW</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <type>RAIN</type>
                <value>0.20</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>FREEZING RAIN</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount> 
            <probabilityOfPrecipitation>
                <type>SNOW</type>
                <value>0</value>
            </probabilityOfPrecipitation>
            <probabilityOfPrecipitation>
                <type>RAIN</type>
                <value>70</value>
            </probabilityOfPrecipitation>
            <probabilityOfPrecipitation>
                <type>FREEZING RAIN</type>
                <value>0</value>
            </probabilityOfPrecipitation>          
        </locationResponse>            
        <locationResponse>
         <day>Day2</day>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>SNOW</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <type>RAIN</type>
                <value>0.10</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>FREEZING RAIN</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount> 
            <probabilityOfPrecipitation>
                <type>SNOW</type>
                <value>0</value>
            </probabilityOfPrecipitation>
            <probabilityOfPrecipitation>
                <type>RAIN</type>
                <value>66</value>
            </probabilityOfPrecipitation>
            <probabilityOfPrecipitation>
                <type>FREEZING RAIN</type>
                <value>0</value>
            </probabilityOfPrecipitation>
        </locationResponse>
    </locationResponseList>
</xml>

    foreach ($xml->locationResponseList->locationResponse as $locationResponse){ 

echo "Day: " . $locationResponse->day . '<br>';
    if ($locationResponse->probabilityOfPrecipitation->type != 'RAIN') {
echo "Chance of Rain: " . $locationResponse->probabilityOfPrecipitation->value . '% <br>';
}

 if ($locationResponse->precipitationAmount->type !='RAIN'){
echo "Amount of Rain: " . $locationResponse->precipitationAmount->value . 'in<br><br>';
}
}

3 个答案:

答案 0 :(得分:1)

要循环precipitationAmount个节点,您需要检查$xml->locationResponseList->locationResponse->precipitationAmount

这是一个小例子: 修改它以满足您的要求。

<?php

$s = '<xml>
    <locationResponseList>
        <locationResponse>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>SNOW</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <type>RAIN</type>
                <value>0.10</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>FREEZING RAIN</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <probabilityOfPrecipitation>
                <type>RAIN</type>
                <value>75</value>
            </probabilityOfPrecipitation>
        </locationResponse>
    </locationResponseList>
</xml>';

$xml = simplexml_load_string($s);

foreach ($xml->locationResponseList->locationResponse->precipitationAmount as $precipitationAmount){ 
    if ($precipitationAmount->type != 'RAIN') {
        continue;
    }

    echo "precipitationAmount value: " . $precipitationAmount->value . " <br/>";
    echo "precipitationAmount uom: " . $precipitationAmount->uom . "<br/>";

    echo 'chance of ' . $xml->locationResponseList->locationResponse->probabilityOfPrecipitation->type . ' is '
        . $xml->locationResponseList->locationResponse->probabilityOfPrecipitation->value . '% <br/>';
    echo '<hr/>';
}

答案 1 :(得分:0)

你刚刚错过了ShenyangAmount,因为循环应该是itterate ...

foreach($xml->locationResponseList->locationResponse->precipitationAmount as $locationResponse) {

    // Now you can access any property of that iteration prop ie. 'type' in this case

    echo $locationResponse->type == 'RAIN' ? 'Its raining day...' : 'Sunny day'; // Whatever
}

答案 2 :(得分:0)

您可以使用XPath选择之后的值,而不是遍历所有数据。下面的代码使用了两个表达式//precipitationAmount[type='RAIN']/value,它们选择了amount元素,//probabilityOfPrecipitation[type='RAIN']/value是可能性。当->xpath()返回匹配节点列表时,在输出中我使用[0]来挑出第一个元素。

$data = '<xml>
    <locationResponseList>
        <locationResponse>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>SNOW</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <type>RAIN</type>
                <value>0.10</value>
                <uom>in</uom>
            </precipitationAmount>
            <precipitationAmount>
                <notAvailable>true</notAvailable>
                <type>FREEZING RAIN</type>
                <value>0.00</value>
                <uom>in</uom>
            </precipitationAmount>
            <probabilityOfPrecipitation>
                <type>RAIN</type>
                <value>75</value>
            </probabilityOfPrecipitation>
        </locationResponse>
    </locationResponseList>
</xml>';

$xml = simplexml_load_string($data);
$rainAmount = $xml->xpath("//precipitationAmount[type='RAIN']/value");
$rainChance = $xml->xpath("//probabilityOfPrecipitation[type='RAIN']/value");
echo "Chance of Rain: " . (string)$rainChance[0] . "% <br>";
echo "Amount of Rain: " . (string)$rainAmount[0] . "in <br><br>";

我也将结果视为字符串((string)),尽管echo无论如何都会这样做。例如,将它分配给结果数组时,它会存储一个SimpleXMLElement,这可能会在其他代码中混淆。

<强>更新

如果您想要每日阅读,那么从foreach开始阅读并在该元素中提取您之后的数据会更容易。

$xml = simplexml_load_string($data);
foreach ( $xml->locationResponseList->locationResponse as $reading )    {
    $rainAmount = $reading->xpath("precipitationAmount[type='RAIN']/value");
    $rainChance = $reading->xpath("probabilityOfPrecipitation[type='RAIN']/value");
    echo "Day:" . (string)$reading->day . "<br>";
    echo "Chance of Rain: " . (string)$rainChance[0] . "% <br>";
    echo "Amount of Rain: " . (string)$rainAmount[0] . "in <br><br>";
}