// 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.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

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

public class XmlParse1 {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
                  "<fql_query_response>\r\n" +
                  "    <user>\r\n" +
                  "        <name>User1</name>\r\n" +
                  "        <uid>0001</uid>\r\n" +
                  "    </user>\r\n" +
                  "    <user>\r\n" +
                  "        <name>User2</name>\r\n" +
                  "        <uid>0002</uid>\r\n" +
                  "    </user>\r\n" +
                  "</fql_query_response>";
        System.out.println(s);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(s)));
        NodeList users = doc.getElementsByTagName("user");
        for(int i = 0; i < users.getLength(); i++) {
            Node user = users.item(i);
            String name = user.getChildNodes().item(1).getFirstChild().getNodeValue();
            String uid = user.getChildNodes().item(3).getFirstChild().getNodeValue();
            System.out.println(name+ " " + uid);
        }
    }
} 
