• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retreiving node values

 
mohamed zafer
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
 
Cynthia Yao
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic