Java Reference
In-Depth Information
This outputs a message indicating the document line number where the error occurred. It also outputs
the string returned by the getMessage() method inherited from the base class, SAXException . This
will usually indicate the nature of the error that was detected.
You could implement the error() callback method similarly but you might want to implement
fatalError() so that it throws an exception. For example:
public void fatalError(SAXParseException spe) throws SAXException {
System.out.println("Fatal error at line "+spe.getLineNumber());
System.out.println(spe.getMessage());
throw spe;
}
Here we just rethrow the SAXParseException after outputting an error message indicating the line
number that caused the error. The SAXParseException class is a subclass of SAXException so we
can rethrow spe as the superclass type.
You could try these out with the previous example by adding these methods to the MySAXHandler
class. You could introduce a few errors into the XML file to get these methods called. Try deleting the
DOCTYPE declaration or deleting the forward slash on an element end tag or even just deleting one
character in an element name.
Summary
In this chapter we have discussed the fundamental characteristics of XML and how Java supports the
analysis and synthesis of XML documents. The key points we have covered include the following:
XML is a language for describing data that is to be communicated from one computer to
another. Data is described in the form of text that contains the data plus markup that defines
the structure of the data.
XML is also a meta-language because you can use XML to create new languages for defining
and structuring data.
Markup consists of XML elements that may also include attributes, where an attribute is a
name/value pair.
The structure and meaning of a particular type of document can be defined within a
Document Type Definition (DTD).
A DTD can be defined in an external file or it can be part of a document.
A DTD is identified by a DOCTYPE declaration in a document.
An XML namespace defines a set of names qualified by a prefix that corresponds to a URI.
The SAX API defines a simple event-driven mechanism for analyzing XML documents.
A SAX parser is a program that parses an XML document and identifies each element in a
document by calling a particular method in your program. The methods that are called are
those defined by the SAX API.
Search WWH ::




Custom Search