i want to read xml file from end to start suppose my xml like that :
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Samy</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't Miss me this weekend!</body>
</note>
i need to read last note first i use code like that
$x = $xmldoc->getElementsByTagName('note');
$nofnews = $xmldoc->getElementsByTagName('note')->length;
for ($i = 0; $i < $nofnews; $i++) {
$item_title = $x->item($i)->getElementsByTagName('to')
->item(0)->nodeValue;
$item_link = $x->item($i)->getElementsByTagName('from')
->item(0)->nodeValue;
}
thank you in advance
getElementsByTagNamereturns aDOMNodeList. You can access each element by it’s numerical index (0 to length-1) as you already do, but just start at the end, not the beginning in your for loop, then count downwards:This should already to it for you.