SAXBrowser.java - SAX Interface Java Example

This section provides a tutorial example on how to parse an XML file and print it back in a tree format using SAX interface in Java - JDK 1.8.

Let's build a simple SAX based XML browser by implementing those event handler methods defined in the org.xml.sax.DocumentHandler interface:

/* SAXBrowser.java
 - Copyright (c) 2014, 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"?>
<p>Hello world!</p>

Ran "java SAXBrowser hello.xml" with JDK 1.8, I got:

Starting document...
e_p
c__Hello world!
Ending document...

Excellent. The program seems to be working.

Notes and sample codes in this section are based on JDK 1.8.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

SAX (Simple API for XML) Programming Interface

 What Is SAX (Simple API for XML)?

 Using SAX Implementation Provided in JDK 1.8

 SAX Content Handler Interface

SAXBrowser.java - SAX Interface Java Example

 SAX Parsing Pattern Example

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version