douju4278 2016-01-25 07:49
浏览 44

用php读取长xml [关闭]

i have a problem with reading my xml. By code down i read xml, bud how i say at the end of this post, i need read subelements. But i don't know how.

   $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 />";
                    }}

I can read this, but my xml looks like:

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

and i need read for example. sensor: date0 and cat: date2 and unit: utc and now show me a string.

When you are looking for this code u say it is easy, but in my xml i have next tag

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

and the same items, bud i need to read only actual.

Please help.

Thank You

  • 写回答

1条回答 默认 最新

  • dthy81285 2016-01-25 07:59
    关注

    Make yourself familiar with SimpleXML and xpath, with these your task is very simple:

    <?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()
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统