SAXBrowser.java - SAX Based XML Browser

This section provides a tutorial example on how to write an XML file browser, SAXBrowser.java. This browser implements some event handler methods provided by the SAX interface.

Let's build a simple SAX based XML browser by handling the events in the ContentHandler interface:

/* SAXBrowser.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
class SAXBrowser {
   public static void main(String[] args) {
      try {
         File x = new File(args[0]);
         SAXParserFactory f = SAXParserFactory.newInstance();
         SAXParser p = f.newSAXParser();
         DefaultHandler h = new MyContentHandler();
         p.parse(x,h);
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString());
      } catch (SAXException e) {
         System.out.println(e.toString());
      } catch (IOException e) {
         System.out.println(e.toString());
      }
   }
   private static class MyContentHandler extends DefaultHandler {
      static String p = "_";
      public void startDocument() throws SAXException {
         System.out.println("Starting document...");
      }
      public void endDocument() throws SAXException {
         System.out.println("Ending document...");
      }
      public void startElement(String ns, String sName, String qName,
         Attributes attrs) throws SAXException {
         String eName = sName;
         if (sName.equals("")) eName = qName;
         System.out.println("e"+p+eName);
         if (attrs!=null) {
            for (int i=0; i<attrs.getLength(); i++) {
               String aName = attrs.getLocalName(i);
               if (aName.equals("")) aName = attrs.getQName(i);
               System.out.println("a"+p+" "+aName+"="
                  +attrs.getValue(i));
            }
         }
         p = p + "_";
      }
      public void endElement(String ns, String sName, String qName)
         throws SAXException {
         p = p.replaceFirst("__", "_");
      }
      public void characters(char buf[], int offset, int len)
         throws SAXException {
         String s = new String(buf, offset, len);
         System.out.println("c"+p+s);
      }
      public void ignorableWhitespace(char buf[], int offset, int len)
         throws SAXException {
         String s = new String(buf, offset, len);
         System.out.println("i"+p+s);
      }
   }
}

Note that:

Let's try this with hello.xml:

<?xml version="1.0"?>
<body>Hello world!</body>

Run java SAXBrowser hello.xml, I got:

herong> java SAXBrowser.java hello.xml

Starting document...
e_body
c__Hello world!
Ending document...

Excellent. The program seems to be working.

Now, let's try it with user.xml:

<?xml version="1.0"?>
<user status="active">
 <!-- This is not a real user. -->
 <first_name>John</first_name>
 <last_name>Smith</last_name>
</user>

Run SAXBrowser with newer versions of JDK, I got:

herong> java SAXBrowser user.xml

Starting document...
e_user
a_ status=active
c__

c__

e__first_name
c___John
c__

e__last_name
c___Smith
c__

Ending document...

The output has a number "characters" entries with just line breaks. Here is how to read them:

But I ran SAXBrowser on user.xml with JDK 1.4, I got:

herong> \j2sdk1.4.1_01\bin\java SAXBrowser user.xml

Starting document...
e_user
a_ status=active
c__
c__

c__
c__
c__

c__
e__first_name
c___John
c__
c__

c__
e__last_name
c___Smith
c__
c__

Ending document...

In JDK 1.4, the parser fired so many "characters()" events. It looks like the parser didn't group the space character, line feed, and cartridge return into a single char[] and fire one "characters()" event. It fired multiple events, one per character.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

SAX (Simple API for XML)

 SAX (Simple API for XML) Implementation in JDK

 SAX Content Handler Interface

SAXBrowser.java - SAX Based XML Browser

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB