I am parsing an XML document and I need to put every child in to a List and then once it is in a List I need to be able to grab a specific child node from an index in the List. My code so far only grabs every child node but I don't know how to put it in a List, looping through it doesn't seem to work. Here is what I have so far:
public static void main(String[] args){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
URL url = new URL ("http://ift.tt/1qRUoWJ");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
// use the factory to create a documentbuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// create a new document from input stream
Document doc = builder.parse(is); // get the first element
Element element = doc.getDocumentElement();
System.out.println(element);
// get all child nodes
NodeList nodes = element.getChildNodes();
// print the text content of each child
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("" + nodes.item(i).getTextContent());
} } catch (Exception ex) {
ex.printStackTrace();
}
}
No comments:
Post a Comment