SAX Parser based XML Document dump

In this chapter you will learn:

  1. Dump a XML document with SAX Parser

Dump a XML document with SAX Parser

import java.io.StringReader;
// j a  va 2  s  .c  om
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.XMLReader;

class MyContentHandler implements ContentHandler {
  private boolean namespaceBegin = false;

  private String currentNamespace;

  private String currentNamespaceUri;

  private Locator locator;

  public void setDocumentLocator(Locator locator) {
    this.locator = locator;
  }

  public void startDocument() {
    System.out.println("<?xml version=\"1.0\"?>");
    System.out.println();
  }

  public void endDocument() {
  }

  public void startPrefixMapping(String prefix, String uri) {
    namespaceBegin = true;
    currentNamespace = prefix;
    currentNamespaceUri = uri;
  }

  public void endPrefixMapping(String prefix) {
  }

  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
    System.out.println("<" + qName);
    if (namespaceBegin) {
      System.out.print(" xmlns:" + currentNamespace + "=\"" + currentNamespaceUri + "\"");
      namespaceBegin = false;
    }
    for (int i = 0; i < atts.getLength(); i++) {
      System.out.print(" " + atts.getQName(i) + "=\\" + atts.getValue(i) + "\"");
    }
    System.out.print(">");
  }

  public void endElement(String namespaceURI, String localName, String qName) {
    System.out.print("</" + qName + ">");
  }

  public void characters(char[] ch, int start, int length) {
    for (int i = start; i < start + length; i++)
      System.out.print(ch[i]);
  }

  public void ignorableWhitespace(char[] ch, int start, int length) {
    for (int i = start; i < start + length; i++)
      System.out.print(ch[i]);
  }

  public void processingInstruction(String target, String data) {
    System.out.print("<?" + target + " " + data + "?>");
  }

  public void skippedEntity(String name) {
    System.out.print("&" + name + ";");
  }
}

public class Main {
  public static void main(String args[]) throws Exception {
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = spf.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setContentHandler(new MyContentHandler());    
    reader.parse(is);

  }
  static String xmlRecords = 
      "<data>" +
      "  <employee>" +
      "    <name>Tom</name>"+ 
      "    <title>Manager</title>" +
      "  </employee>" +
      "  <employee>" +
      "    <name>Jerry</name>"+ 
      "    <title>Programmer</title>" +
      "  </employee>" +
      "</data>";
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to handle errors from SAX Parser
Home » Java Tutorial » XML

DOM Parser

    DOM
    Parse XML string
    Parse XML file with DOM Parser
    DOM Parser Error
    Output DOM Document to console
    Save DOM Document to a file
    Format a DOM Document
    Output Document to String
    Visit all elements
    Element retrieve by tag name
    Element character data retrieve
    DOM node visiting
    Node type
    DOM node dump
    Children nodes
    Root Element
    Doc Type
    Declared Entities
    External Entities resolve
    DOMImplementation

DOM Edit

    Element text child data
    Element remove
    Add text node to element
    Element text data update
    Add attribute to element
    Attribute remove
    Create new empty element
    Element clone
    CDATASection
    Comments element
    Processing Instruction
    New XML Document

SAX Parser

    SAX Parser action
    Parse an XML file with SAX parser
    SAX Parser based XML Document dump
    SAX Parser Error Handler
    SAX Parser content handler
    SAX Locator

Utility

    Encode String for XML attribute
    Convert Source to InputSource