I had to rewrite part of a programme to use XMLReader to select parts of an XML file for processing.
Take this simplified XML as an example:
<odds>
<sport>
<region>
<group>
<event name="English Championship 2014-15" eventid="781016.1">
<bet name="Kazanan" betid="12377108.1">
<selection selectionid="52411062.1"/>
</selection>
</bet>
</event>
</group>
</region>
</sport>
</odds>
This call to xpath()
:
$bets = $xml->xpath(
"//odds/sport/region/group/event/bet/selection[contains(@selectionid,'".$selectionToFind."')]/.."
);
would select the whole <bet>
node and its children (<selection>
nodes).
My code, however, would select only one <selection>
node with a given selectionid
:
$reader = new XMLReader;
$reader->open('file.xml');
while($reader->read()) {
$event = $reader->getAttribute($value);
if ($event == 781016.1 ) {
$node = new SimpleXMLElement($reader->readOuterXML());
var_dump($node);
break;
}
}
How can replicate the behaviour of xpath()
with XMLReader
so that I select the <bet>
node and its children and not only one <selection>
child?
I guess the question boils down to: Can I select the whole parent node <bet>
by the attribute value of a child, e.g. <selection selectionid="[some_value]">
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…