用php读取长xml

时间:2016-01-25 07:49:58

标签: php xml xmlreader

我在阅读我的xml时遇到问题。通过代码向下我读取xml,萌芽我在这篇文章的最后说,我需要读取子元素。但我不知道如何。

   $reader = new XMLReader();
$reader->open("data/sns.xml");
while($reader->read()){ 
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "item" && $reader->getAttribute('sensor') == "th0" && $reader->getAttribute('cat') == "temp" && $reader->getAttribute('unit') == "c") {
                        echo "Venkovní teplota: ".$reader->readString()." °C";
                        echo "<br />";
                    }
                    if($reader->nodeType == XMLReader::ELEMENT && $reader->name == "item" && $reader->getAttribute("sensor") == "th0" && $reader->getAttribute("cat") == "hum" && $reader->getAttribute("unit") == "rel"){
                        echo "venkovní vlhkost: ".$reader->readString();
                        echo "<br />";
                    }}

我可以读到这个,但我的xml看起来像:

 <data timeframe="actual">
<item sensor="system" cat="version" unit="text">4.9u</item>
<item sensor="system" cat="version" unit="num">49</item>
<item sensor="system" cat="build" unit="num">1261</item>
<item sensor="system" cat="platform" unit="text">SilverStone_DC01</item>
<item sensor="system" cat="language" unit="text">Czech</item>
<item sensor="system" cat="temp" unit="unit">c</item>
<item sensor="system" cat="hum" unit="unit">rel</item>
<item sensor="system" cat="press" unit="unit">hpa</item>
<item sensor="system" cat="wind" unit="unit">ms</item>
<item sensor="system" cat="rain" unit="unit">mm</item>
<item sensor="date0" cat="date" unit="utc">20160111090606</item>
<item sensor="date0" cat="date2" unit="utc">11.01.2016 09:06:06</item>
<item sensor="date0" cat="puredate" unit="utc">11.01.2016</item>
<item sensor="date0" cat="time" unit="utc">09:06:06</item>
<item sensor="date0" cat="year" unit="utc">2016</item>
<item sensor="date0" cat="month" unit="utc">01</item>
<item sensor="date0" cat="day" unit="utc">11</item>
<item sensor="date0" cat="dayofweek" unit="utc">1</item>
<item sensor="date0" cat="hour" unit="utc">09</item>
<item sensor="date0" cat="min" unit="utc">06</item>
<item sensor="date0" cat="sec" unit="utc">06</item>
<item sensor="date0" cat="date" unit="local">20160111100606</item>
<item sensor="date0" cat="date2" unit="local">11.01.2016 10:06:06</item>
<item sensor="date0" cat="puredate" unit="local">11.01.2016</item>
<item sensor="date0" cat="time" unit="local">10:06:06</item>
<item sensor="date0" cat="year" unit="local">2016</item>
<item sensor="date0" cat="month" unit="local">01</item>
<item sensor="date0" cat="day" unit="local">11</item>
</data>

我需要阅读例如。 sensor:date0和cat:date2和unit:utc,现在显示一个字符串。

当你在寻找这段代码时,你说它很容易,但在我的xml中我有下一个标签

<data timeframe="lastday"></data>

和相同的项目,芽我需要只读实际。

请帮忙。

谢谢

1 个答案:

答案 0 :(得分:1)

熟悉SimpleXMLxpath,您的任务非常简单:

<?php
$string = __your_xml_string_here;

$xml = simplexml_load_string($string);
$sensors = $xml->xpath("//item[@sensor='date0']");
print_r($sensors);
# or loop over them with foreach
foreach ($sensors as $sensor) {
    // do sth. useful here
    // the full string can be obtained with
    // $sensor->asXML() or $sensor->__toString()
}
?>