开发者

PHP - simpleXML

开发者 https://www.devze.com 2023-02-28 02:37 出处:网络
I have this xml: <events> <event> <eventId>Bskt-Bulls-Pacer-042111</eventId> <eventDescriptor></eventDescriptor>

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><开发者_如何学C;/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;


    }

    }
}


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

 }
 }


Personally I always use DOMDocument, this would be:

$xml = new DOMDocument();
$xml->loadXML($varholdingxml); // or $xml->loadXMLFile("/path/to/xmlfile.xml");
foreach ($xml->getElementsByTagName("event") as $parent)
{ 
 $lineType = $parent->getElementsByTagName("lineType")->item(0)->nodeValue;
 if ($lineType == "PSH" || $lineType == "TLO")
 {
   $points = $parent->getElementsByTagName('points')->item(0)->nodeValue;
   echo "Points are " . $lineType . " = " . $points;
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消