This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes getting element value not attribute value 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 » Engineering » XML and Related Technologies
Bookmark "getting element value not attribute value" Watch "getting element value not attribute value" New topic
Author

getting element value not attribute value

Robin Clark
Ranch Hand

Joined: Dec 17, 2003
Posts: 81
I would like to process an element-based XML document that looks like this

as opposed to a attribute-based document that looks like this:

but I can't figure out how to extract the value associated with an element. I think that understand how to extract the value associated with an attribute based upon looking at some of the other posts in this forum.
This is the first time I have worked with processing XML in Java. Before I actually get started, I'd like to get your opinion on the pros and cons on the formats of the messages above. Can someone point me towards the code for extracting the value from an element? This is the code that I have so far for reading the file:

Thank you for your advice and assistance!
Robin
[ April 24, 2004: Message edited by: Robin Clark ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Robin, I think this FAQ entry in our wiki might help you out.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
See what happens, the text under the ELEMENT node is not the text directly belonging to the Element node, instead element node has a Text Node under it which contains the value.
So u need to use
Element lText = msg.getFirstChild(); // TO get the text Node
String lValue = lTest.getNodeValue(); // to get the value of the Text Node.

or simply u can use


String lValue = msg.getFirstChild().getNodeValue();


Hope this helps


Share Knowledge to gain it.
SCJP 2, SCWCD 2, SCDJWS, IBM 141 (In Progress), IBM 486 (Next)
Robin Clark
Ranch Hand

Joined: Dec 17, 2003
Posts: 81
This is exactly what I was looking for. I used the getNodeValue method on this page so my code looks like this:

It seems to be working great. Thank you!
 
wood burning stoves
 
subject: getting element value not attribute value
 
Similar Threads
Sample Code For DOM
xml:lang in faultstring
NullPointerException
Reading XML
Convert set of data into XML document