• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

read the values from xml using java

 
Ranch Hand
Posts: 32
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

this is my problem.

I have a XML file. I don't know the node(tag) names in that XML file.

i need to read the node values.

How to do this. I know to read values by giving the node name.

for example




help please..
 
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you try the below steps -
1. Parse the xml document and build a DOM
2. Get the document element (first element / top element) of the document using Document:getDocumentElement() method
3. Get the child nodes of the document element using getChildNodes() method which will return a node list
4. Iterate through the list and for each node convert it to an Element (just do (Element)nodeList[i]) and get its attribute ( i think at least you know the attr name?)

 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I read it correctly, the desired content is not an attribute but the element text content instead. Which is where either the getTextContent() method can help, or if that's not available (there was a thread about that just recently) check the nodes that are Text. Because the nodes do not need to be Element nodes; Text, ProcessingInstruction, Comment - those are nodes as well.

Another possibility is to not use the org.w3c.dom package but use a library (like JDOM) instead.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we have Thanks button , I just want to thanks "John Jai" for his clear and step by step solution kudos you man simple yet effective

just realized we can use +1 for that here we go my thumbs up to you both
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic