I'm trying to retrieve one specific node based on the <id> element from a huge XML file. I have used DOMDocument, but its not ideal since it loads the whole document first. There is around 1400 <item> nodes in the document. This is a simplified version of the document:
<main>
<body>
...
<sub>
...
<items>
...
<item>
<name>Abc</name>
...
<id>123</id>
<calls>
<call>
<name>Monkey</name>
<text>Monkeys r cool</text>
...
</call>
<call>
<name>Pig</name>
<text>Pigs too!</text>
...
</call>
</calls>
<cones>
<cone>
<name>Lorem</name>
<text>Lorem ipsum</text>
...
</cone>
<cone>
<name>More</name>
<text>Placeholder</text>
...
</cone>
</cones>
<a>true</a>
</item>
<item>
<name>Def</name>
...
<id>456</id>
<calls>
<call>
<name>aa</name>
<text>aa</text>
...
</call>
<call>
<name>bb</name>
<text>bb</text>
...
</call>
</calls>
<cones>
<cone>
<name>cc</name>
<text>cc</text>
...
</cone>
<cone>
<name>dd</name>
<text>dd</text>
...
</cone>
</cones>
<a>true</a>
</item>
</items>
</sub>
</body>
</main>
So basically I'm trying to retrieve the current node and its children's data from matching the <id> element. I have tried find tutorials on XMLReader, but can't seem to find that much. This is what I've tried so far:
$xml = new XMLReader();
$xml->open('doc.xml');
while($xml->read()) {
if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'id') {
$xml->read();
echo $xml->value;
}
}
This finds every <id> element, but i want to find one specific and read the data from the current node, and its children. Maybe using the example to find the node and readInnerXml() to get the data
I'm not an expert so any help / push to the right direction is much appreciated :D