简单的XML读取foreach


simple xml read foreach

不知道圣诞节你能不能帮忙。

我试图阅读XML,但我有几个问题,基本上foreach结构不让我在我想要的结构返回数据,我不确定这样做的正确方式。在下面的例子:

`<event id="298640100" date="Sat Dec 31 16:00:00 CET 2011">
<market id="9064667" type="1" status="Open" period="FT">
<description>Match Betting</description>
<place_terms>Win only</place_terms>
−
<outcome id="6798861400">
<description>Draw</description>
−
<price id="24532283602">
<decimal>3.5</decimal>
<fractional>5/2</fractional>
</price>
<position>2</position>
</outcome>
−
<outcome id="6798861200">
<description>Bolton Wanderers</description>
−
<price id="24532283402">
<decimal>2.0</decimal>
<fractional>1/1</fractional>
</price>
<position>1</position>
</outcome>
−
<outcome id="6798861300">
<description>Wolves</description>
−
<price id="24532283502">
<decimal>3.6</decimal>
<fractional>13/5</fractional>
</price>
<position>3</position>
</outcome>
</market>
</event>`
PHP

`<?php
$source = file_get_contents("vc.xml");
$xml = simplexml_load_string($source);
$game = $xml->xpath("//event");
foreach ($game as $event)
{
    echo "<b>Event ID:</b> " . $event['id'] . "<br />";
    echo "<b>Event Date:</b> " . $event['date'] . "<br />";
        {
            foreach ($event->children() as $market)
                {
                    if ($market['period'] == 'FT')
                        {
                            foreach ($market->children() as $outcome) 
                                {
                                    echo "<b>Outcome ID:</b> " . $outcome['id'] . "<br />";
                                    foreach ($outcome->children() as $price) 
                                    {
                                        echo "<b>Price ID:</b> " . $price ['id'] . "<br />";
                                        foreach ($price->children() as $value)
                                            {
                                                echo "<b>Value:</b> " . $value . "<br />";
                                            }
                                    }
                                }
                        }
                }
        }
    echo "<br />";
}
?>`

这基本上是返回这个:

事件ID: 298640100
活动日期:2011年12月31日星期六16:00:00 CET
结果ID:
结果ID:
结果ID: 6798861400
价格ID:
价格ID: 24532283602
值:3.5
价值:5/2
价格ID:
结果ID: 6798861200
价格ID:
价格ID: 24532283402
值:2.0
值:1/1
价格ID:
结果ID: 6798861300
价格ID:
价格ID: 24532283502
值:3.6
值:13/5
价格ID:

理想情况下,我只想返回以下内容:

事件ID: 298640100
活动日期:2011年12月31日星期六16:00:00 CET
结果ID: 6798861400
价格ID: 24532283602
取值范围:5/2

你知道我做错了什么吗?我怎么才能做到这一点?

Thanks in advance

理查德

你有两个问题。首先,使用children()函数,它返回所有子节点,而不仅仅是您想要的特定类型。这就是为什么你在一开始会得到3次Outcome ID:。你应该用foreach ($market->outcome as $outcome)代替foreach ($market->children() as $outcome)

第二,您似乎只想要第一个结果。在这种情况下,不应该使用foreach。simplexml对象是一组数组,您可以通过索引号访问数组中的单个对象。你可以去掉很多代码,直接获取第一个结果对象,像这样:

$xml->event->market->outcome[0]

您可能需要阅读官方simpleXML文档http://www.php.net/manual/en/simplexml.examples-basic.php

这是我认为你需要的:

foreach ($game as $event)
{
    echo "<b>Event ID:</b> " . $event['id'] . "<br />";
    echo "<b>Event Date:</b> " . $event['date'] . "<br />";
        {
            foreach ($event->children() as $market)
                {
                    if ($market['period'] == 'FT')
                        {
                            foreach ($market->children() as $name => $outcome ) 
                                {
                                    if ( $name != "outcome" )
                                    {
                                      continue;
                                    }
                                    echo "<b>Outcome ID: - $name</b> " . $outcome['id'] . "<br />";
                                    foreach ($outcome->children() as $name => $price) 
                                    {
                                      if ( $name != "price" )
                                      {
                                        continue;
                                      }
                                        echo "<b>Price ID:</b> " . $price ['id'] . "<br />";
                                        foreach ($price->children() as $name => $value)
                                            {
                                                if ( $name != "fractional" )
                                                {
                                                    continue;
                                                }
                                                echo "<b>Value: - $name</b> " . $value . "<br />";
                                                break;
                                            }
                                    }
                                                break;
                                }
                                                break;
                        }
                }
        } 
    echo "<br />";
}