IBM Version 5 manual XML namespace support, Example 3-3 SAX2 callback methods

Models: Version 5

1 340
Download 340 pages 15.44 Kb
Page 77
Image 77

implements the ContentHandler interface. To use DefaultHandler, create a subclass and override the methods that interest you. The other methods can safely be ignored, since they are just empty methods.

The ParserAdapter class makes a SAX1 Parser behave as a SAX2 XMLReader. The XMLReaderAdapter class makes a SAX2 XML reader behave as a SAX1 parser. The two classes should ease the transition from SAX1 to SAX2 by allowing SAX1 drivers and clients to co-exist with SAX2 drivers and clients in the same application.

XML namespace support

SAX2 adds XML namespace support. Every implementation of the SAX2 XMLReader interface is required to support namespace processing in its default state. Additionally, many XML readers allow namespace processing to be modified or disabled. Namespace processing changes only element and attribute naming, although it places restrictions on some other names. Each XML element and attribute has a single name called the qName which may contain colons. With namespaces, elements and attributes have two-part name, sometimes called the universal or expanded name, which consists of a URI, and a localName.

SAX2 is capable of supporting either of these views or both simultaneously. Similarly, documents may use both views simultaneously. SAX2 XMLReader implementations are required to report the namespace style names when documents use them.

Namespace support affects the ContentHandler and Attributes interfaces. In SAX2, the startElement and endElement callbacks in a content handler look like Example 3-3 on page 61.

Example 3-3 SAX2 callback methods

public void startElement (String uri, String localName,String qName, Attributes atts) throws SAXException;

public void endElement (String uri, String localName, String qName) throws SAXException;

By default, an XML reader will report a namespace URI and a local name for every element that belongs to a namespace, in both the start and end handler. Consider Example 3-4 on page 61.

Example 3-4 Sample namespace

<html:hr xmlns:html="http://www.ibm.com"/>

Chapter 3. Processing XML 61

Page 77
Image 77
IBM Version 5 manual XML namespace support, Example 3-3 SAX2 callback methods, Example 3-4 Sample namespace