T HE SAX API 9 method can also take a variety of other input sources, including an InputStream object, a URL, and an InputSource object. SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("priceList.xml", handler);
The result of calling the method parse depends, of course, on how the methods in handler were implemented. The SAX parser will go through the file priceList.xml line by line, calling the appropriate methods. In addition to the methods already mentioned, the parser will call other methods such as start- Document , endDocument , ignorableWhiteSpace , and processingInstructions , but these methods still have their default implementations and thus do nothing. The following method definitions show one way to implement the methods characters and startElement so that they find the price for Mocha Java and print it out. Because of the way the SAX parser works, these two methods work
together to look for the
name element, the characters "Mocha Java", and the price element immediately following Mocha Java. These methods use three flags to keep track of which conditions have been met. Note that the SAX parser
will have to invoke both methods more than once before the conditions for print-
ing the price are met.
public void startElement(..., String elementName, ...){ if(elementName.equals("name")){ inName = true; } else if(elementName.equals("price") && inMochaJava ){ inPrice = true;
inName = false;
} } public void characters(char [] buf, int offset, int len) { String s = new String(buf, offset, len);
if (inName && s.equals("Mocha Java")) {
inMochaJava = true;
inName = false;
} else if (inPrice) { System.out.println("The price of Mocha Java is: " + s);
inMochaJava = false;
inPrice = false;
}
} }

Web Hosting by Oxxus.Net

JavaWSTutorial-851.html 
JavaWSTutorial-853.html