Presents your XML E-NEWSLETTER for April 9, 2003 <-------------------------------------------> HANDLE DOM EXCEPTIONS IN JAVA When working with Java-based XML parsers, you are often faced with handling exceptions. Some exceptions are simple to handle, while others are more difficult. In this article, we'll look at some common exceptions seen while working with XML documents and explore how to manage them. DOM EXCEPTIONS When parsing with a DOM parser, the XML document is processed all at once and stored in memory. The in-memory object is referred to as a Document Object Model--which is basically an object-oriented approach to accessing the underlying XML document. Many things can go wrong with the processing of XML documents under DOM. Most DOM-oriented exceptions occur as an instance of the DOMException class. This class supports 15 different, specific exception conditions. Each condition is specified as a member of the DOMException class called code. In addition to the code member, the DOMException class contains a set of 15 static members, which are used to determine the condition of the exception. The 15 conditions are: * DOMSTRING_SIZE_ERR * HIERARCHY_REQUEST_ERR * INDEX_SIZE_ERR * INUSE_ATTRIBUTE_ERR * INVALID_ACCESS_ERR * INVALID_CHARACTER_ERR * INVALID_MODIFICATION_ERR * INVALID_STATE_ERR * NAMESPACE_ERR * NO_DATA_ALLOWED_ERR * NO_MODIFICATION_ALLOWED_ERR * NOT_FOUND_ERR * NOT_SUPPORTED_ERR * SYNTAX_ERR * WRONG_DOCUMENT_ERR When a DOMException is thrown, you won't know which of these conditions caused the exception without examining the code in the exception object. In order to process the exception appropriately, you'll need to determine which condition caused the exception. LISTING A shows a sample Java program that fails when creating a new DOM document: Listing A: DOMFail.Java import org.apache.xerces.dom.*; import org.w3c.dom.*; public class DOMFail { public static void main(String[] args) { Document doc = new DocumentImpl(); Element rootElement; try{ rootElement = doc.createElement("Order"); rootElement.setAttribute("