• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retreiving node values

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
I am using JAXP and have written a SAX program to parse a XML file as taught by the Java XML tutorial. It works fine, now I want to retrieve specific node values. How can it be done.
It would be fine if someone helps me with example code.
zafer
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did u read "Working with XML" tutorial at Sun's site
Here is the link: http://java.sun.com/xml/tutorial_intro.html
in DOM section. DomEcho04.java file. this is a class called:
adoptNode and a method content(). That may be what u are looking for.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trick when working with SAX is to remember that you have to catch things on the fly. This is completely different from the DOM approach. The methods to do this are all defined in SAX interfaces. The SUN JAXP package has some SAX examples.
To give you a feeling - the startElement method is called when SAX hits a tag that starts an element. In this fragment I set the lookForTitle variable when that happens.
//
public void startElement( String name, AttributeList attrib)
throws SAXException
{
if( "Title".equals( name )){
lookForTitle = true ; return ;
}
.... method continues
NOW - when SAX finds Element content it calls the characters method with pointers to a buffer where the content is, but this method gets called for all content so you have to keep track of which element you are in.
public void characters( char[] buf, int start, int count ){
if( lookForTitle ){
/// process characters.....
 
mohamed zafer
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you William, For giving an easy solution to my problem. Infact i had forget the very idea of what a SAX can do. And u had reminded me about that.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic