// 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class GetAndParseXml {
    public static String getXml(String urlstr) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader((new URL(urlstr)).openConnection().getInputStream()));
        StringBuilder res = new StringBuilder();
        String line;
        while((line = br.readLine()) != null) {
            res.append(line);
            res.append("\r\n");
        }
        br.close();
        return res.toString();
    }
    public static List<Person> parse(String xmlstr) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(xmlstr)));
        List<Person> res = new ArrayList<Person>();
        NodeList allp = doc.getElementsByTagNameNS("http://arne/p", "person");
        for (int i = 0; i < allp.getLength(); i++) {
            Element p = (Element)allp.item(i);
            NodeList allelm = p.getChildNodes();
            String name = null;
            String animal = null;
            String characteristic = null;
            for (int j = 0; j < allelm.getLength(); j++) {
                if(allelm.item(j).getNodeType() == Node.ELEMENT_NODE && allelm.item(j).getNamespaceURI().equals("http://arne/p")) {
                    Element elm = (Element)allelm.item(j);
                    if(elm.getLocalName().equals("name")) {
                        name = elm.getFirstChild().getNodeValue();
                    } else if(elm.getLocalName().equals("animal")) {
                        animal = elm.getFirstChild().getNodeValue();
                    } else if(elm.getLocalName().equals("characteristic")) {
                        characteristic = elm.getFirstChild().getNodeValue();
                    }
                }
            }
            res.add(new Person(name, animal, characteristic));
        }
        return res;
    }
    public static void main(String[] args) throws Exception {
        for(Person p : parse(getXml("http://localhost:81/listpersons_webservice.xml"))) {
            System.out.println(p.getName() + " / " + p.getAnimal() + " : " + p.getCharacteristic());
        }
    }
}
