?
Solved

parsing XML files with DOM problem

Posted on 2003-02-24
8
Medium Priority
?
220 Views
Last Modified: 2013-11-23
Hi,

I'm trying to parse a simple XML document like this:
<policy>
 <portnum>60000</portnum>
 <check name="1">
 etc..

So, when I do: (using getNthElement() method which simply does root.getElementsByTagName("portnum"))
 root = document.getDocumentElement();

 Element pPort = this.getNthElement(root, "portnum", 0);
 Node pPortText = pPort.getFirstChild();
 portNumber = new      Integer(pPortText.getNodeValue()).intValue();

I can get the port number without a problem. Actually, by using the getElementsByTagName() method I can access any Element in the XML tree, and reach their child TEXT nodes.

However, when I try the following:

 Node myroot = document.getFirstChild();
 Node portNumNode = myroot.getFirstChild();
 System.out.println("root name: "+myroot.getNodeName());
 System.out.println("Num node: " + portNumNode.getNodeName());

I get the result:
root name: policy
Num node: #text

I also did some other tries, but it seems that I can never make my way around the XML tree by using these getFirstChild() methods. What could be the problem?
0
Comment
Question by:kushcu
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:nir2002
ID: 8015511
Hi,

change the code to the code below and you will get what you want?

Node myroot = document.getFirstChild();
Node portNumNode = myroot.getFirstChild();
System.out.println("root name: "+myroot.getNodeName());
System.out.println("Num node: " + portNumNode.getNodeValue());

You use the API worng. I suggest you use JDOM Element API.
According to the API if you ad the name of node of type Text you get "#text"
if the node is an element (for eg a node that have other nodes) this method retun the tag name of the element.

Best regards
Nir
 


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016389
Listening..
0
 

Author Comment

by:kushcu
ID: 8086884

In that case, I get the following output:
root name: policy
Num node:

(an empty field.)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 8087677
What happens when you do

>>System.out.println("Num node name: " + portNumNode.getNodeName());

?
0
 
LVL 2

Accepted Solution

by:
nir2002 earned 800 total points
ID: 8088936
Please test this code:

Node myroot = document.getFirstChild();
Node portNumNode = (Node)(myroot.getFirstChild()).getNextSibling();
System.out.println("root name: "+myroot.getNodeName());
System.out.println("Num node: " + portNumNode.getNodeName());

and tell me what the output?


0
 

Author Comment

by:kushcu
ID: 8092262
CEHJ, please check my original posting.

nir2002, it prints:
root name: policy
Num node: portnum.

could you explain me why that is so?
0
 

Author Comment

by:kushcu
ID: 8095071
btw, I guess your code fragment fully solves my original problem. thanks.
0
 
LVL 2

Expert Comment

by:nir2002
ID: 8097159
:-)
I don't use the API in this way.
It more like the way in your beginning of you question

>>So, when I do: (using getNthElement() method which >>simply does root.getElementsByTagName("portnum"))
>>root = document.getDocumentElement();

>>Element pPort = this.getNthElement(root, "portnum", 0);
>>Node pPortText = pPort.getFirstChild();
>>portNumber = new      Integer(pPortText.getNodeValue()).intValue();

>>I can get the port number without a problem. Actually, by using the getElementsByTagName() method I can access any Element in the XML tree, and reach their child TEXT nodes.

Well, it is probably a matter of taste.

For your question, Although it may seem odd but when you build your dom, before the portnum node, there was a text node. the getNextSibling() function bring us the next node skipping it. since it is a element the getNodeName() bring is tag name portnum.

Best regards
Nir


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

616 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