wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes read the values from xml using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read the values from xml using java" Watch "read the values from xml using java" New topic
Author

read the values from xml using java

vinoth Robert
Ranch Hand

Joined: Feb 02, 2011
Posts: 31

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..


vinoth.R
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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?)

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
 
subject: read the values from xml using java
 
Similar Threads
xml parser design question
Flat file to XML COnversion
ParserImpl for DOM
parse xml
return as xml document