6.3 Notes on the SAX Parser
The following table gives cautionary notes on the SAX parser.
No. |
Notes |
---|---|
1 |
In multi-thread programming, the SAXParserFactory class is not thread-safe. Therefore, multiple threads must not access the same SAXParserFactory instance at the same time. To avoid conflicts between threads, use one of the following methods:
|
2 |
In multi-thread programming, the SAXParser class is not thread-safe. Therefore, multiple threads must not use the same SAXParser instance at the same time. To avoid conflicts between threads, use the following method:
|
3 |
In multi-thread programming, the objects defined by org.xml.sax, org.xml.sax.ext, and org.xml.sax.helpers packages are not thread-safe. Therefore, multiple threads must not access these objects at the same time. Not only update methods, but also reference methods must not access such trees at the same time. To avoid conflicts between threads, use the following method:
|
4 |
The SAX parser can divide single character string data into multiple character strings (chunks) and report to the application as multiple characters events. Therefore, in the ContentHandler implementation, you must be concerned that character string data might be divided. An example of the ContentHandler implementation is coded below. In this example, the character string data is buffered each time a characters event occurs, and the character string data is considered to be ended when an endElement event is reported. class MyHandler implements ContentHandler { String str = null; StringBuffer buffer = null; : public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { buffer = new StringBuffer(); } : public void characters(char c[], int start, int length) throws SAXException { buffer.append(c, start, length); } : public void endElement(String uri, String name, String qname) throws SAXException { str = buffer.toString(); } : |
5 |
If you generate an XMLReader by using the createXMLReader(String className) method of the XMLReaderFactory class, specify com.cosminexus.jaxp.impl.parsers.parsers.SAXParser for the className argument. An example of XMLReader generation is as follows: XMLReader reader = XMLReaderFactory.createXMLReader("com.cosminexus.jaxp.impl.parsers.parsers.SAXParser"); |
6 |
When an error occurs in the parse methods that take InputStream or InputSource as an argument of the SAX parser class, null might be returned if you apply the getSystemId method to the SAXParseException passed to the error handler. If you want to return the system identifier of the error source, use the parse method as follows:
|
7 |
When you parse an XML document saved in UTF-16 with BOM (Byte Order Mark) by using the parse(InputSource is ...) method, specify UTF-16 for the argument if you apply the setEncoding method to the InputSource. |
8 |
If an element name with 477 or more characters is included in the character string that defines an internal entity, the java.lang.IndexOutOfBoundsException exception might occur. |
9 |
The java.lang.IllegalStateException exception might occur, if the following conditions are satisfied:
|