// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class SiblingProblem {
    public static void main(String[] args) throws Exception {
        String xml = "<all><rec><a>1</a><b>2</b></rec><rec><a>2</a><b>4</b></rec></all>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(xml)));
        Node n = doc.getDocumentElement();
        while(n != null) {
            System.out.println(n);
            n = n.getNextSibling();
        }
        System.out.println("====");
        n = doc.getDocumentElement().getFirstChild();
        while(n != null) {
            System.out.println(n);
            n = n.getNextSibling();
        }
        System.out.println("====");
        n = doc.getDocumentElement().getFirstChild().getFirstChild();
        while(n != null) {
            System.out.println(n);
            n = n.getNextSibling();
        }
    }
}
