import org.xml.sax.*; import org.xml.sax.helpers.*; import com.japisoft.fastparser.*;
import java.io.*;
/** * Sample of Sax usage inside the Xerces API. This is * a case of <code>FastParser</code> integration without changing * your DOM API usage. * * This class shows all SAX event during parsing. * * @author (c) 2002-2003 JAPISOFT * @version 1.0 * @since 1.0 */ public class Demo implements DocumentHandler {
public void setDocumentLocator (Locator locator) { }
public void startDocument() throws SAXException { System.out.println( "- start document" ); }
public void endDocument() throws SAXException { System.out.println( "- end document" ); }
public void startElement(String name, AttributeList atts) throws SAXException { System.out.println( "* start tag " + name + " / " + printAttributes( atts ) ); }
public void endElement(String name) throws SAXException { System.out.println( "* end tag" + name ); }
public void characters(char ch[], int start, int length) throws SAXException { System.out.println( "+ text [" + new String( ch ) + "]" ); }
public void ignorableWhitespace(char ch[], int start,int length) throws SAXException { }
public void processingInstruction(String target, String data) throws SAXException { System.out.println( "! instruction " + target + " " + data ); }
private String printAttributes( AttributeList atts ) { StringBuffer s = new StringBuffer(); if ( atts != null ) { for ( int i = 0; i < atts.getLength(); i++ ) { s.append( atts.getName( i ) + "=" + atts.getValue( i ) ).append( " /" ); } } return s.toString(); }
public static void main( String[] args ) throws Throwable { System.out.println( "SAX usage sample" ); SaxParser p = new SaxParser(); p.setDocumentHandler( new Demo() ); p.parse( new InputSource( new FileInputStream( args[ 0 ] ) ) ); } }
|