• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

getting element value not attribute value

 
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 ]
 
author
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.
 
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!
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic