?
Solved

parsing XML files with DOM problem

Posted on 2003-02-24
8
Medium Priority
?
209 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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