Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating and Parsing XML Documents in JDeveloper : Page 5

Oracle's XDK 10G extends JAXP to make reading, writing, and querying XML easy.


advertisement
Parsing an XML Document with the SAX API
SAX parsing is based on a push model in which a SAX parser generates events, and a document handler receives notification of those events. The SAX parsing model is faster than the DOM parsing, but is limited in scope to generating parsing events; it has no provision for navigating nodes or retrieving nodes with XPath. This section shows you how to parse the example XML document with a SAX parser and handle the parser events. The sample SAX parsing application was developed in JDeveloper using the downloadable SAXParserApp.java application. First, import the oracle.xml.jaxp package:

import oracle.xml.jaxp.*;

A SAX parsing application typically extends the DefaultHandler class, which exposes event notification methods for the parse events. The DefaultHandler class implements the ErrorHandler interface, so you can use a DefaultHandler to handle errors directly. Create a JXSAXParserFactory object by calling the static method newInstance(). The newInstance() method returns a SAXParserFactory object that may be cast to JXSAXParserFactory because it extends the SAXParserFactory class:

JXSAXParserFactory factory = (JXSAXParserFactory) JXSAXParserFactory.newInstance();

Create a SAXParser object from the factory object with the newSAXParser() method. JXSAXParser extends SAXParser, so you can cast a SAXParser object to JXSAXParser:



JXSAXParser saxParser = (JXSAXParser) factory.newSAXParser();

Create an InputStream for the XML document you want to parse and call one of the parse() methods in SAXParser class. The parse method parameters require an XML document in any of the forms InputSource, InputStream, URI, or File, and an event handler such as the DefaultHandler:

InputStream input = new FileInputStream( new File("c:/j2eeapp/catalog.xml")); saxParser.parse(input, this);

The DefaultHandler class provides the parsing event notification methods and error handling methods, which you can override with custom event and error handling methods. Table 7 lists some of the event notification methods in the DefaultHandler class.

Table 7. DefaultHandler Event Notification Methods: The table lists the event notification methods and describes when each event fires.
Method Name Description
startDocument() Fires when the parser reaches the start of the document.
startElement(
   java.lang.String uri,
   java.lang.String localName,
   java.lang.String qName,
   Attributes attributes)
Fires when the parser reaches the beginning of an element. The URI argument specifies the namespace URI. LocalName specifies the element's local name, which is the element name without the prefix. QName specifies the element's qualified name—with prefix. Attributes specifies the list of attributes in a element.
endDocument() Fires when the parser reaches the end of the document.
endElement(
   java.lang.String uri,
   java.lang.String localName,
   java.lang.String qName)
Fires when the parser reaches the end of an element.
characters(char[] ch,
   int start, int length)
Fires when the parser has read some text from a text node.

The SAXParserApp.java application overrides some of the notification methods to output the event type, element name, element attributes, and element text. For example, you can iterate through the attributes in the Attributes object and output the attribute name, namespace URI, and attribute value:

for (int i = 0; i < atts.getLength(); i++) { System.out.println("Attribute QName:" + atts.getQName(i)); System.out.println("Attribute Local Name:" + atts.getLocalName(i)); System.out.println("Attribute Namespace URI:" + atts.getURI(i)); System.out.println("Attribute Value:"+atts.getValue(i)); }

You can also override the error handler methods in the DefaultHandler class. The SAXParserApp.java application overrides some of the methods to output error messages. Table 8 lists the error handler methods in the DefaultHandler class.

Table 8. DefaultHandler Error Handler Methods: The table lists the DefaultHandler error handling methods, which you can override.
Method Name Description
error(SAXParseException
   exception)
Receives notification of a recoverable error.
fatalError(SAXParseException
   exception)
Receives notification of a non-recoverable error.
warning(SAXParseException exception) Receives notification of a warning.

 
Figure 7. SAX Output: The figure shows part of the output from the SAX parsing application running in JDeveloper.
Listing 4 shows the complete code for the SAXParserApp.java application.

Figure 7 shows a portion of the output of the sample SAX parsing application running in JDeveloper, while Listing 5 shows the complete output.

Again, you can see how the application handles errors by adding an error in the example XML document such as removing a node. Run the SAXParserApp.java application again, and you'll see an error message:

Fatal Error:<Line 15, Column 10>: XML-20121: (Fatal Error) End tag does not match start tag 'journal'.

This article demonstrated and documented both DOM and SAX approaches to parsing an XML document. DOM parsing is suitable if you need to modify document nodes, or need random or repeated access to the nodes. SAX parsing is the better choice for large documents and documents where you need only parse events and node values.



Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap