I am trying to parse the below xml file in java using DOM.
First, I am getting the root element using
Then, I am trying to find out how many children for the root element using
I am getting as 3 for the above. But the root element is printing as "MessageBean"
and the "MessageBean" root element have only one child node "Request", then why it's coming as 3?
I am trying get the <form> attribute values (name="emp1" and value="10") and the actual valus (St1).
I need to get all the children of <Request> only with the tag <form> adn not the <Unit>
Then I need to get the all the children of the <Unit> separatley.
Why the "childNode11111 ::#text" is coming as #text??
Please help on this. I don't uderstand how to process it. If someone explain how it works I can able to parse more complex xmls too.
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Those extra #text nodes are from the formatting - crlf + tabs or spaces - which your XML has to make it more readable.
This is one of the most common Gotcha! for new xml programmers.
See the JavaDocs for org.xml.dom.Node for an essential table that explains all the node types and the associated names and values. All newbies should become VERY familiar with this table.
So - the take home here is - Never depend on the "next" node being what you expect, always check, use methods such as
which will automatically skip those formatting #text nodes.
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter