File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Retreiving node values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Retreiving node values" Watch "Retreiving node values" New topic

Retreiving node values

mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
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.
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
Did u read "Working with XML" tutorial at Sun's site
Here is the link:
in DOM section. 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

Joined: Mar 22, 2000
Posts: 13035
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

Joined: Nov 03, 2000
Posts: 62
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.
I agree. Here's the link:
subject: Retreiving node values
It's not a secret anymore!