我有这个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时,我只能从市场上撤出<points>。我需要从多个<event>节点中提取此数据。如何在每个<event>中测试<market>中的lineType并提取出我想要的<event>?
这就是我所拥有的,但显然不起作用:
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;
}
}
}发布于 2011-04-24 06:02:33
您可以使用如上所示的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;
}
}
}发布于 2011-04-24 05:54:18
就我个人而言,我总是使用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;
}
}https://stackoverflow.com/questions/5766852
复制相似问题