6
XML Programming Best Practices 6-1
6
XML Programming Best Practices
The following sections discuss best programming practices when creating Java applications that process XML data:
■
Section 6.1, When to Use the DOM, SAX, and StAX APIs
■
Section 6.2, Increasing Performance of XML Validation
■
Section 6.3, When to Use XML Schemas or DTDs
■
Section 6.4, Configuring External Entity Resolution for Maximum Performance
■
Section 6.5, Using SAX InputSources
■
Section 6.6, Improving Performance of Transformations
6.1 When to Use the DOM, SAX, and StAX APIs
You can parse an XML document with the DOM, SAX, or StAX APIs. This section describes the pros and cons of each API.
The DOM API is good for small documents, or those that contain under a thousand elements. Because DOM constructs a tree of your XML data, it is ideal for editing the
structure of your XML document by adding or deleting elements.
The DOM API parses the entire XML document and converts it into a DOM tree before you can begin processing it. This cost might be beneficial if you know that you need to
access the entire document. If you occasionally need to access only part of the XML document, the cost could decrease the performance of your application with no added
benefit. In this case the SAX or StAX API is preferable.
The SAX API is the most lightweight of the APIs. It is ideal for parsing shallow documents documents that do not contain much nesting of elements with unique
element names. SAX uses a callback structure; this means that programmers handle parsing events as the API is reading an XML document, which is a relatively efficient
and quick way to parse.
However, the callback nature of SAX also means that it is not the best API to use if you want to modify the structure of your XML data. Additionally, programming your
application to handle callbacks is not always straight-forward and intuitive.
The StAX API is based on SAX, so all the reasons for using SAX also apply to the StAX API. In addition, the StAX API is more intuitive to use than SAX, because
programmers ask for events rather than react to them as they happen. The StAX API is also best if you plan to pass the entire XML document as a parameter; it is easier to
pass an input stream than it is to pass SAX events. Finally, the StAX API was designed to be used when binding XML data to Java objects.
6-2 Programming XML for Oracle WebLogic Server
6.2 Increasing Performance of XML Validation