The code examples in this section are written using JAXP 1.1, which supports SAX2.0.
To parse an XML document, you instantiate a javax.xml.parsers.SAXParseFactory object to obtain a SAX-based parser. This parser is then used to read the XML document a character at a time.
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new XMLParse(); saxParser.parse( new File(argv[0]), handler );
Your SAX parser class must extend the public class org.xml.sax.helpers.DefaultHandler. This class defines stub methods that receive notification (callbacks) when XML entities are parsed. By default, these methods do nothing, but they can be overridden to do anything you like. For example, a method called startElement() is invoked when the start tag for an element is recognized. This method receives the element's name and its attributes. The elements name can be passed in any one of the first three parameters to startElement(), see Table 16.6, depending on whether namespaces are being used.
Parameter | Contents |
---|---|
uri | The namespace URI or the empty string if the element has no namespace URI or if namespace processing is not being performed |
localName | The element name (without namespace prefix) will be a non-empty string when namespaces processing is being performed |
qualifiedName | The element name with namespace prefix |
attributes | The element's attributes |
In the following code example, handling for the qualified name is provided.
public void startElement(String uri, String localName, String qualifiedName, Attributes attributes) throws SAXException { System.out.println ("START ELEMENT " + qualifiedName); for (int i = 0; i< attributes.getLength(); i++) { System.out.println ("ATTRIBUTE " + attributes.getQName(i) + " = " + attributes .getValue(i)); } }
This example prints out a statement indicating that a start tag has been parsed followed by a list of the attribute names and values.
A similar endElement() method is invoked when an end tag is encountered.
public void endElement(String uri, String localName, String qualifiedName) throws SAXException { System.out.println ("END ELEMENT " + qualifiedName); }
In the parser in Listing 16.9, not all the XML components will be handled. The default action is for components to be ignored. For a complete list of the other DefaultHandler methods, see Table 16.7 or refer to the Java 2 Platform, Enterprise Edition, v 1.3 API Specification.
The parser first checks for the XML document, the name of which is provided on the command line (lines 9–12). After instantiating the SAXParserFactory (line 14) and constructing the handler (line 13), the XML file is parsed on line 17—that is all there is to it. Lines 32–57 are where the handler routines are defined. This parser reports the occurrence of the start and end of the document—the start and end of elements and the characters that form the element bodies only.
The complete listing for the SAX Parser is shown in Listing 16.9.
As already stated, lines 32–57 are the handler callback methods that are called when the corresponding XML entity is parsed. If an entity method is not declared in your parser, the entity is handled by the superclass DefaultHandler methods, the default action being to do nothing. Table 16.7 gives a full list of the callback DefaultHandler methods that can be implemented.
This parser can be invoked simply from the command line:
> java XMLParse jobSummary.xml
The output in Figure 16.1 is produced when this SAX parser is used on the jobSummary XML in Listing 16.4.
As you can see, the output is not very beautiful. You might like to improve it by adding indentation to the elements or even getting the output to look like the original XML.
In addition to making this parser more robust, the following functionality could be added:
Scan element contents for the special characters, such shown in a table, and replacing them with the symbolic strings as appropriate
Improve the handling of fatal parse errors (SAXParseException) with appropriate error messages giving error line numbers
Use the DefaultHandler error() and warning() methods to handle non-fatal parse errors
Configure the parser to be namespace aware with javax.xml.parsers.SAXParserFactory.setNamespaceAware(true), so that you can detect tags from multiple sources
You will now build a parser application that uses the DOM API.
3.149.241.250