PHP-simpleXML

发布于 2024-11-03 10:57:09 字数 2648 浏览 1 评论 0原文

我有这个 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>

当 lineType = PSA 和 TLO 时,我试图仅从市场中提取 。我需要从多个 节点中提取它。如何在每个 中测试 中的 lineType 并提取我想要的?

这是我所拥有的,但显然不起作用:

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;


    }

    }
}

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;


    }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-11-10 10:57:09

您可以使用 DOMDocument 方法,如上所示。

我认为您的代码中的错误是您假设事件是内部循环中的单个节点。
如您的 xml 所示,事件标记出现多次,因此您需要在内部循环中使用 $event :-

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

 }
 }

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

 }
 }
彻夜缠绵 2024-11-10 10:57:09

我个人总是使用 DOMDocument,这将是:

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

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;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文