douwenan9849 2011-04-23 20:48
浏览 39
已采纳

PHP - simpleXML

I have this xml:

<events>
   <event>
      <eventId>Bskt-Bulls-Pacer-042111</eventId>
      <eventDescriptor></eventDescriptor>
      <eventStatus></eventStatus>
      <markets>
         <market>
            <lineType>PSH</lineType>
            <status>1</status>
            <num>100</num>
            <den>110</den>
            <points>4.0</points>
            <quickbet></quickbet>
            <price>-110</price>
            <openNum>100</openNum>
            <openDen>110</openDen>
            <openPoints>-5.0</openPoints>
            <openPrice>-110</openPrice>
            <percentage>23%</percentage>
         </market>
         <market></market>
         <market></market>
         <market>
            <lineType>PSA</lineType>
            <status>1</status>
            <num>100</num>
            <den>110</den>
            <points>-4.0</points>
            <quickbet>
               selection[Bskt-Bulls-Pacer-042111PSA]=Bskt-Bulls-Pacer-042111|PSA|1|100|110|-8|-110
            </quickbet>
            <price>-110</price>
            <openNum>100</openNum>
            <openDen>110</openDen>
            <openPoints>5.0</openPoints>
            <openPrice>-110</openPrice>
            <percentage>77%</percentage>
         </market>
         <market></market>
         <market></market>
      </markets>
      <hosturl></hosturl>
   </event>
   <event></event>
   <event></event>
   <event></event>
   <event></event>
   <event></event>
   <event></event>
   <event></event>
</events>

I'm stuck trying to pull out only <points> from market when lineType = PSA and TLO. I need to pull this from multiple <event> nodes. How do I test lineType in <market> in each <event> and pull out the ones I want?

This is what I have, but clearly isn't working:

foreach ($xml->event as $event) {

foreach ($xml->event->markets->market as $market) {

    if ($market->lineType == 'TLO') {

        echo "points are TLO  = " . $market->points;                

    }

    if ($market->lineType == 'PSH')  {

       echo "points are PSA = " . $market->points;


    }

    }
}
  • 写回答

2条回答 默认 最新

  • donkey199024 2011-04-23 22:02
    关注

    you can use the DOMDocument approach as shown above.

    I think the error in your code is that you are assuming that event is a single node in the inner loop. As shown in your xml, the event tag appears multiple times, so you need to use $event in the inner loop :-

    foreach ($xml->event as $event) {
    
    foreach ($event->markets->market as $market) {
    
        if ($market->lineType == 'TLO') {
            echo "points are TLO  = " . $market->points;
        }
    
        if ($market->lineType == 'PSH')  {
            echo "points are PSA = " . $market->points;
        }
    
     }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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 基于单片机的靶位控制系统