PHP-simpleXML
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DOMDocument 方法,如上所示。
我认为您的代码中的错误是您假设事件是内部循环中的单个节点。
如您的 xml 所示,事件标记出现多次,因此您需要在内部循环中使用 $event :-
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 :-
我个人总是使用 DOMDocument,这将是:
Personally I always use DOMDocument, this would be: