dqq9695 2017-10-16 09:20
浏览 20
已采纳

simplexml_load_file:如果主标记包含某些内容

I'm not so sure about the title, will try to explain in the next lines.

I have an xml file like this :

 <CAR park="3" id="1" bay="0">
    <SITE_ID>0</SITE_ID>
    <SITE_NAME>Car Seller 1</SITE_NAME>
    . . .
 </CAR>

I am sucessfully iterating through my xml to get all the data. But, I want to be able to filter by bays. I want to do something like

$xml = simplexml_load_file('myfile.xml');
$x = 1;
    foreach($xml as $car) {
    if($car->bay == '0'){
        echo $car->SITE_ID;
    $x++;
     }

    }
  • 写回答

1条回答 默认 最新

  • doubi2228 2017-10-16 09:48
    关注

    You can use XPath to fetch only the bay 0 cars...

    $bay0 = $xml->xpath('//CAR[@bay="0"]');
    foreach ( $bay0 as $car )   {
        echo $car->SITE_ID.PHP_EOL;
    }
    

    The XPath statement is simply - any CAR element that has an attribute bay with the value 0 in it.

    In case you need to access attributes in other cases, with SimpleXML - you access them as though they are array elements, so it would be $car['bay'] in the code you had above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)