Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        Xml xml = new Xml();
        Xml nextSibling = null;
        Xml prevSibling = null;

        try {
            xml.loadXmlFile("qa_data/xml/fruit.xml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Iterate over the direct children by using FirstChild / NextSibling
        Xml child = xml.firstChild();
        boolean bContinue = xml.getLastMethodSuccess();
        while (bContinue) {
            System.out.println(child.getTag() + " : " + child.getContent());
            nextSibling = child.nextSibling();
            bContinue = child.getLastMethodSuccess();
            child.close();
            child = nextSibling;
        }

        System.out.println("-----");

        // Do the same, but with FirstChild2 / NextSibling2 to avoid
        // creating so many XML object instances:
        success = xml.firstChild2();
        while (success) {
            System.out.println(xml.getTag() + " : " + xml.getContent());
            success = xml.nextSibling2();
        }

        // Revert back up to the parent:
        xml.getParent2();

        System.out.println("-----");

        // Iterate in reverse order using LastChild / PreviousSibling
        child = xml.lastChild();
        bContinue = xml.getLastMethodSuccess();
        while (bContinue) {
            System.out.println(child.getTag() + " : " + child.getContent());
            prevSibling = child.previousSibling();
            bContinue = child.getLastMethodSuccess();
            child.close();
            child = prevSibling;
        }

        System.out.println("-----");

        // Do the same, but with LastChild2 / PreviousSibling2 to avoid
        // creating so many XML object instances:
        try {
            xml.lastChild2();
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        while (success) {
            System.out.println(xml.getTag() + " : " + xml.getContent());
            xml.previousSibling2();
        }

        // Revert back up to the parent:
        xml.getParent2();
    }
}