Simple SAX Example

This example creates a Java program that you can run from the command line. You will be able to specify an XML document to be parsed, and messages will be output onscreen in response to events generated by the SAX parser.

Note

Java, like XML, is case sensitive. All names of interfaces, classes, and so on in the following code must use the correct case if your application is to run correctly.



Listing 18.2 shows a simple SAX example.

Listing 18.2. myHandler.java: A Java Program That Provides Screen Output in Response to SAX Events
import org.xml.sax.XMLReader; 
import org.xml.sax.SAXException; 
import org.xml.sax.Attributes; 
import org.xml.sax.helpers.DefaultHandler; 
import org.xml.sax.helpers.XMLReaderFactory; 

import org.apache.xerces.parsers.SAXParser;  
public class myHandler extends DefaultHandler 
 { 
  public static void main(String[] argv) throws Exception { 
 if (argv.length == 0) { 
 System.out.println("You need to specify a file name"); 
 System.exit(0); 
 } 
System.out.println("The program myHandler has started ..."); 
 myHandler reader = new myHandler(); 
 reader.read(argv[0]); 
 } 

 public void read(String fileName) throws Exception{ 
  System.out.println("read() method entered ..."); 
 XMLReader parser = XMLReaderFactory.createXMLReader(); 

 parser.setContentHandler(this); 
 parser.parse(fileName); 
 } 

 public void startDocument() throws SAXException { 
 System.out.println("The document has been opened."); 
 } 

 public void processingInstruction(String target, 
  String data) throws SAXException { 
 System.out.println("A processing instruction with target, 
  " +target+ " and data " +data+ "."); 
 } 

 public void startElement(String uri, String localName, 
   String QName, Attributes attributes) throws SAXException{ 
 System.out.println("Start tag of element " 
  + localName + " was found."); 
 } 

 public void endElement(String uri, String localName, 
    String QName) throws SAXException{ 
  System.out.println("End tag of element " 
   + localName + " was found."); 
 } 

 public void characters(char[] characters, int start, 
  int length) throws SAXException {  
 System.out.println("Character content encountered."); 
 }
 
 public void endDocument() throws SAXException { 
  System.out.println("The document has been completed."); 
 } 
} 

The Java file must be compiled. Using the javac compiler, you can issue this command to create a class file:

javac myHandler.java 

To run the class file, issue this command:

java -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser 
  myHandler SAXSource.xml 

That command assigns the class org.apache.xerces.parsers.SAXParser to the environment variable org.xml.sax.driver.

When the code is run, you will see an onscreen appearance like that shown in Figure 18.1.

Figure 18.1. The output when the myHandler class is run.


Let’s look briefly at what the code does.

You first create a class called myHandler. The main() method accepts string arguments. So, when you enter the following command, the SAXSource.xml is the sole string argument.

java -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser 
  myHandler SAXSource.xml 

This next if statement checks to see if a filename has been supplied as an argument:

if (argv.length == 0) { 
System.out.println("You need to specify a file name"); 
System.exit(0); 

If no filename is supplied, an error message is output and the program exits.

This code indicates that the program has started successfully:

System.out.println("The program myHandler has started ..."); 

The read() method creates an XMLReader that parses the file supplied in the argument at the command line.

The parser raises events at many points as it parses the document. The startDocument() method outputs a message when the start of the document has been encountered. Then when the <?xml-stylesheet ?> processing instruction is encountered, the processingInstruction() method outputs a message that tells what the target and data of the processing instruction are.

When the start of the myDocument element is found, the startElement() method is called and a message is output. When the character content of the myDocument element is encountered, the characters() method is called and outputs a message.

Finally, the endElement() and endDocument() methods are called and appropriate messages are output to the screen.

In this short example, when each of the methods of the org.xml.sax.helpers.DefaultHandler interface is called as a result of the appropriate event happening during parsing, we have simply output a message to the screen that says what event(s) has been encountered.

Of course, in more serious use of SAX, the way in which you implement an interface is very flexible, and you can write Java code to do whatever is appropriate in response to particular events encountered by the SAX parser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset