• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

getting element value not attribute value

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic