kushcu
asked on
parsing XML files with DOM problem
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.getDocumentElemen t();
Element pPort = this.getNthElement(root, "portnum", 0);
Node pPortText = pPort.getFirstChild();
portNumber = new Integer(pPortText.getNodeV alue()).in tValue();
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?
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(
root = document.getDocumentElemen
Element pPort = this.getNthElement(root, "portnum", 0);
Node pPortText = pPort.getFirstChild();
portNumber = new Integer(pPortText.getNodeV
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?
Listening..
ASKER
In that case, I get the following output:
root name: policy
Num node:
(an empty field.)
What happens when you do
>>System.out.println("Num node name: " + portNumNode.getNodeName()) ;
?
>>System.out.println("Num node name: " + portNumNode.getNodeName())
?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CEHJ, please check my original posting.
nir2002, it prints:
root name: policy
Num node: portnum.
could you explain me why that is so?
nir2002, it prints:
root name: policy
Num node: portnum.
could you explain me why that is so?
ASKER
btw, I guess your code fragment fully solves my original problem. thanks.
:-)
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.getDocumentElemen t();
>>Element pPort = this.getNthElement(root, "portnum", 0);
>>Node pPortText = pPort.getFirstChild();
>>portNumber = new Integer(pPortText.getNodeV alue()).in tValue();
>>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
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(
>>root = document.getDocumentElemen
>>Element pPort = this.getNthElement(root, "portnum", 0);
>>Node pPortText = pPort.getFirstChild();
>>portNumber = new Integer(pPortText.getNodeV
>>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
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