• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.








output
>
 
Author and all-around good cowpoke
Posts: 13078
6
  • 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
getElementsByTagName("Request")
which will automatically skip those formatting #text nodes.

Bill
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic