Abbey Workshop

Mapping Data from an XML File using SAX

This tip demonstrates a mapping technique for extracting data from an XML file. This basic technique can be used in a number of different situations. The key is the code in the handler class.

Line 58 defines a CharArrayWriter class that that is used as a buffer for this class. On line 65, the buffer is reset when an element start tag is encountered in the startElement() method. This removes any characters that may have been encountered before an element we are interested in. When the endElement() method is called on lines 73 - 83, a check is made to see if it is one of the elements we are interested in. If it is a name or phone element, then the data in the buffer is processed. In this simple example, it is simply printed out.

Listing for: SaxMap.java

   1:import javax.xml.parsers.*;
   2:import org.xml.sax.*;
   3:import org.xml.sax.helpers.*;
   4:import java.io.*;
   5:
   6:// A Simple SAX Application using JAXP with Namespace support
   7:public class SaxMap{
   8:    private SAXParserFactory factory; // Creates parser object
   9:    private SAXParser saxParser; // Holds a parser object
  10:    private DefaultHandler handler; // Defines the handler for this parser
  11:    
  12:    public SaxMap() throws SAXException{
  13:        try{
  14:            factory = SAXParserFactory.newInstance();
  15:            factory.setNamespaceAware(true);        
  16:            saxParser = factory.newSAXParser();
  17:            
  18:            // Set Content Handlers
  19:            handler = new MyDefaultHandler();
  20:
  21:        } catch (ParserConfigurationException e){
  22:            e.printStackTrace();
  23:        } catch (SAXException e){
  24:            e.printStackTrace();
  25:        }
  26:    }
  27:    
  28:    public void parseDocument(String xmlFile){
  29:        try{
  30:            saxParser.parse(xmlFile, handler);
  31:        } catch (SAXException e){
  32:            e.printStackTrace();
  33:        } catch (IOException e){
  34:            e.printStackTrace();
  35:        } catch (Exception e){
  36:            e.printStackTrace();
  37:        }
  38:    }
  39:    
  40:    public static void main(String[] args){
  41:        try {
  42:            if (args.length != 1) {
  43:                System.out.println(
  44:                    "Usage: java SimpleJAXPns " +
  45:                    "[XML Document Filename]");
  46:                System.exit(0);
  47:            }
  48:            SaxMap xmlApp = new SaxMap();
  49:            xmlApp.parseDocument(args[0]);
  50:        } catch (SAXException e){
  51:            e.printStackTrace();
  52:        } catch (Exception e) {
  53:            e.printStackTrace();
  54:        }
  55:    }
  56:    
  57:    class MyDefaultHandler extends DefaultHandler{
  58:        private CharArrayWriter buff = new CharArrayWriter();
  59:        /* With a handler class, just override the methods you need to use
  60:        */
  61:        public void startElement(String uri, String local, String qName, Attributes att){
  62:            /*
  63:                When a start tag is found, reset the buffer. This clears out 
  64:                any previous data we do not need and starts storing the text 
  65:                for this element.
  66:            */
  67:            buff.reset();
  68:        }
  69:        
  70:        public void characters(char[] ch, int start, int length){
  71:            // Store text in a buffer
  72:            buff.write(ch, start, length);
  73:        }
  74:
  75:        public void endElement(String uri, String local, String qName){
  76:            // Pick out the specific tags you are looking for
  77:            
  78:            if (local.equals("name")){
  79:                System.out.println("Name: " + buff.toString());
  80:            }
  81:            
  82:            if (local.equals("phone")){
  83:                System.out.println("Phone #: " + buff.toString());
  84:            }
  85:        }
  86:        
  87:    }
  88:}
  89:

Listing for: address.xml

   1 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
   2 <!-- A Todo List -->
   3 <address>
   4   <name>John Doe</name>
   5   <street>1234 1st Street</street>
   6   <city>Smallville</city>
   7   <state>Kansas</state>
   8   <zip>12345</zip>
   9   <phone>301-555-1212</phone>
  10 </address>