Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extract Tags from XML using org.w3c.dom

Posted on 2008-10-14
2
Medium Priority
?
859 Views
Last Modified: 2012-05-05
I have an xml that looks something like the below. I am using Java. I have parsed it. (There can be an infinite number of <Result> tags as well as an infinite number of <Return> tags.)

<TestXML>
    <FirstStuff>blah</FirstStuff>
    <MoreStuff>blah blah</MoreStuff>
    <Result>1234</Result>
    <Result>5678</Result>
    <Return>
         <Comment>This one worked</Comment>
         <Pool>1234</Pool>
    </Return>
    <Return>
         <Comment>This one worked better</Comment>
         <Pool>5678</Pool>
    </Return>
</TestXML>

This is a piece of code that I have used:
     final Document document = xmlObject.getXmlAsDocument();
     final NodeList nodeList = document.getElementsByTagName("Result");
     final Vector results = new Vector();
     for (int i = 0; i < nodeList.getLength(); i++) {
          final Node node = nodeList.item(i);
          final String nodeName = node.getNodeName();
          final Node nodeValue =  node.getChildNodes().item(0);
          results.add(nodeValue.getNodeValue());
     }

This works fine. But now I don't need to get the results tag anymore, but the two tags inside the Return tag. And I need to associate the comment with the pool, so I need to get them together so I can put them in some object before adding them to the Vector. I am very unfamiliar with how this work and am struggling to read the tags inside the Results tag. I tried the getChildren and getSibling thing...but I don't think I am using it in the right way. Can someone help me ??      
0
Comment
Question by:Zenoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:manuel_m
ID: 22712121
If you want to read the tags inside of the return tag, you have to add another for loop.

First, get elements by tag name from "Return" tag.
Now you have a node list with all Return nodes.
To get the tags inside you have to get the child nodes (getChildNodes()) from the items in your for loop.
With getChildNodes you get another node list.
Iterate over it to get the Comment and Pool tags. To get the text try getTextContent on the nodes.
0
 
LVL 5

Accepted Solution

by:
muktajindal earned 320 total points
ID: 22718361
You may use the attached code snippet.
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new File("C:\\test.xml"));
			NodeList nodeList = document.getElementsByTagName("Return");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node1 = nodeList.item(i);
				if (node1.getNodeType() == Node.ELEMENT_NODE) {
					NodeList nodes = node1.getChildNodes();
					for (int j= 0; j < nodes.getLength(); j++) {
						Node node2 = nodes.item(j);
						if (node2.getNodeType() == Node.ELEMENT_NODE) {
							NodeList internalNodes = node2.getChildNodes();
							String nodeName = node2.getNodeName();
							for (int k = 0; k < internalNodes.getLength(); k++) {
								Node node = internalNodes.item(k);
								String nodeValue = node.getNodeValue();
								System.out.println(nodeName + " = " + nodeValue);
							}
						}
					}
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}

Open in new window

0

Featured Post

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question