Java核心代码例程之:(JAXP) SAX
2008-01-05 08:40:00 来源:WEB开发网核心提示:import javax.xml.parsers.*;import org.xml.sax.*;/** * SAXDemo uses JAXP to acquire a SAX parser to parse an XML file. * The example XML file rePResents a shoppi
import javax.xml.parsers.*;
import org.xml.sax.*;
/**
* SAXDemo uses JAXP to acquire a SAX parser to parse an XML file.
* The example XML file rePResents a shopping cart.
*
* The following JARs must be in your CLASSPATH:
* - jaxp.jar
* - xerces.jar (for SAX parser implementation)
*
* Download JAXP (which includes these JARs) here: http://java.sun.com/xml/
* Find additional Xerces info here: http://xml.apache.org/
*
* Note: Unlike DOM, SAX parsing does not load the XML file into memory.
* SAX parsers traverse the XML file and report parse "events" to an event handler.
**/
public class SAXDemo
extends org.xml.sax.HandlerBase
{
/**
* main creates and runs a SaxTest instance.
**/
public static void main( String[] args )
{
SAXDemo me = new SAXDemo();
me.run();
}
public void run()
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
log( "SAXParserFactory classname: " + factory.getClass().getName() );
SAXParser saxParser = factory.newSAXParser();
log( "SAXParser classname: " + saxParser.getClass().getName() );
/*
The SAXParser.parse method initiates parsing of the XML file.
The second parameter specifies which class will handle parse events.
This class must extend org.xml.sax.HandlerBase
更多精彩
赞助商链接