Handle DOM exceptions in Java
Takeaway: There are some common DOM exceptions seen while working with XML documents. Here's how to manage them.
This article originally appeared as an XML e-newsletter.
By Brian Schaffner
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:
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. Click here.
Notice that we use a switch mechanism to determine which condition is met. This way we can easily test each condition until we find the correct one.
In this example, we've placed a message in the condition for an invalid character. When you compile and run this example, you'll see the message There is an invalid character. You'll want to handle each condition appropriately, rather than just leaving the condition empty.
You may want to extract the conditional exception handling and place it in its own method or even its own class. That way, you could have simpler exception handling in your code. For example, we might create a new class called DOMExceptionHandler, as shown in Listing B. Click here.
Now that we have a special handler class for our DOM exceptions, we can call it from within our processing code. Listing C shows a revision of our DOMFail class that uses our new DOMExceptionHandler class. Click here.
This new class is much simpler than our original DOMFail class. It also enables us to more easily reuse our exception handler code. Instead of cutting and pasting the handler code each time we need it, we now just call the DOMExceptionHandler class.
Brian Schaffner is an associate director for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Technology Consulting practice.
SponsoredWhite Papers, Webcasts, and Downloads
- Voice over IP on the Road: Making the Mobile Workforce Accessible ShoreTel
- Accelerating Network-based Backup Riverbed
- IP Telephony from A to Z: The Complete IP Telephony eBook ShoreTel
- Strategies for Centralizing Data Backup Riverbed
- Economist Intelligence Unit whitepaper: "Enterprise Knowledge Workers: Understanding Risks and Opportunities" SAP
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





