• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting element value not attribute value

 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin, I think this FAQ entry in our wiki might help you out.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic