dongxing4643 2014-01-14 03:04
浏览 42

解析具有相同标记名称的XML页面

Just started getting oritented with SimpleXML and have run into a problem

I am able to parse simple xml pages like below

<something>
 <something2> value1 </something2>
 <something3> value2 </something3>
  </something>

Fairly simple to get the values of something2 and something3 if that's the only thing i need

However I am having problem with XML files that have multiple instances of a tag e.g

     <car>
       <carID>001</carID>
         <carTyre>4</carTyre>
          <carColor>Red</carColor>
            <carName>Bruce</carName>
    </car>
   <car>
     <carID>002</carID>
        <carTyre>4</carTyre>
          <carColor>Blue</carColor>
            <carName>Tom</carName>
     </car>
     <car>
          <carID>003</carID>
            <carTyre>4</carTyre>
              <carColor>Yellow</carColor>
                 <carName>Alex</carName>
   </car>
   <car>
     <carID>004</carID>
         <carTyre>4</carTyre>
            <carColor>White</carColor>
              <carName>Tina</carName>
          </car>

If i only wanted specific information (not the whole data set) for e.g

Tina - White Tome - Blue

How could I do this?

  • 写回答

2条回答 默认 最新

  • dongqigu0429 2014-01-14 03:56
    关注

    Lets say that is all in the Cars.xml, then you can try something like this:

    <?php 
     $cars =  simplexml_load_file("Cars.XML"); 
     foreach($cars->xpath('//car') as $car) { 
    
    $row = simplexml_load_string($car->asXML()); 
    $v = $row->xpath('//carID[. ="004"]'); 
    if($v[0]){ 
        print $car-> carName . '-' . $car-> carColor; 
     } 
    
    } 
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)