This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Retreiving node values" Watch "Retreiving node values" New topic
Author

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.
zafer
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: 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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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.
 
wood burning stoves
 
subject: Retreiving node values
 
Similar Threads
read the values from xml using java
ParserImpl for DOM
Reading node values in specified datatype in schema( .XSD)
xmllint problem
JAXP-DOM Node Import Issue