• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin

DOM XML Parsing - Newbie

Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
which will automatically skip those formatting #text nodes.

It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic