Sample code for 30+ languages & platforms
Xbase++

Traverse Direct Children via FirstChild / NextSibling, or LastChild / PreviousSibling

See more XML Examples

Demonstrates some ways to iterate over direct child nodes using the FirstChild / NextSibling and LastChild / PreviousSibling methods.

The input XML, available at http://www.chilkatsoft.com/data/fruit.xml, is this:

<root>
    <fruit color="red">apple</fruit>
    <fruit color="green">pear</fruit>
    <veg color="orange">carrot</veg>
    <meat animal="cow">beef</meat>
    <xyz>
        <fruit color="blue">blueberry</fruit>
        <veg color="green">broccoli</veg>
    </xyz>
    <fruit color="purple">grape</fruit>
    <cheese color="yellow">cheddar</cheese>
</root>

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXml
LOCAL oChild
LOCAL oNextSibling
LOCAL oPrevSibling
LOCAL nBContinue

nSuccess := 0

oXml := CreateObject("Chilkat.Xml")

nSuccess := oXml:LoadXmlFile("qa_data/xml/fruit.xml")
IF (nSuccess != 1)
    ? oXml:LastErrorText
    oXml:destroy()
    RETURN
ENDIF

//  Iterate over the direct children by using FirstChild / NextSibling
oChild := oXml:FirstChild()
nBContinue := oXml:LastMethodSuccess
DO WHILE (nBContinue == 1)
    ? oChild:Tag + " : " + oChild:Content
    oNextSibling := oChild:NextSibling()
    nBContinue := oChild:LastMethodSuccess
    oChild:destroy()
    oChild := oNextSibling
ENDDO
? "-----"

//  Do the same, but with FirstChild2 / NextSibling2 to avoid
//  creating so many XML object instances:
nSuccess := oXml:FirstChild2()
DO WHILE nSuccess == 1
    ? oXml:Tag + " : " + oXml:Content
    nSuccess := oXml:NextSibling2()
ENDDO
//  Revert back up to the parent:
nSuccess := oXml:GetParent2()

? "-----"

//  Iterate in reverse order using LastChild / PreviousSibling
oChild := oXml:LastChild()
nBContinue := oXml:LastMethodSuccess
DO WHILE (nBContinue == 1)
    ? oChild:Tag + " : " + oChild:Content
    oPrevSibling := oChild:PreviousSibling()
    nBContinue := oChild:LastMethodSuccess
    oChild:destroy()
    oChild := oPrevSibling
ENDDO
? "-----"

//  Do the same, but with LastChild2 / PreviousSibling2 to avoid
//  creating so many XML object instances:
nSuccess := oXml:LastChild2()
DO WHILE nSuccess == 1
    ? oXml:Tag + " : " + oXml:Content
    nSuccess := oXml:PreviousSibling2()
ENDDO
//  Revert back up to the parent:
nSuccess := oXml:GetParent2()

oXml:destroy()