• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

DOM XML Parsing - Newbie

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.








output
>
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
getElementsByTagName("Request")
which will automatically skip those formatting #text nodes.

Bill
 
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
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic