FileDocCategorySizeDatePackage
SAXParserDemo.javaAPI DocExample15654Fri Jun 02 15:56:24 BST 2000None

SAXParserDemo

public class SAXParserDemo extends Object
SAXParserDemo will take an XML file and parse it using SAX, displaying the callbacks in the parsing lifecycle.
author
Brett McLaughlin
version
1.0

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

This provides a command line entry point for this demo.

        if (args.length != 1) {
            System.out.println("Usage: java SAXParserDemo [XML URI]");
            System.exit(0);
        }
        
        String uri = args[0];
    
        SAXParserDemo parserDemo = new SAXParserDemo();
        parserDemo.performDemo(uri);
    
public voidperformDemo(java.lang.String uri)

This parses the file, using registered SAX handlers, and output the events in the parsing process cycle.

param
uri String URI of file to parse.

        System.out.println("Parsing XML File: " + uri + "\n\n");
        
        // Get instances of our handlers        
        ContentHandler contentHandler = new MyContentHandler();
        ErrorHandler errorHandler = new MyErrorHandler();        
        
        try {
            // Instantiate a parser
            XMLReader parser = 
                XMLReaderFactory.createXMLReader(
                    "org.apache.xerces.parsers.SAXParser");
                
            // Register the content handler
            parser.setContentHandler(contentHandler);
            
            // Register the error handler
            parser.setErrorHandler(errorHandler); 

            // Turn on validation
            parser.setFeature("http://xml.org/sax/features/validation",
                              true);           

            // Turn off namespace awareness
            parser.setFeature("http://xml.org/sax/features/namespaces",
                              false);                     
                
            // Parse the document
            parser.parse(uri);
            
        } catch (IOException e) {
            System.out.println("Error reading URI: " + e.getMessage());
        } catch (SAXException e) {
            System.out.println("Error in parsing: " + e.getMessage());
        }