some of that penalty and gives us the assurance that the XML document in question is well
formed, but validation always carries a significant price.
Table 10-7. Performance of validation of an XML document
231 ms 265 ms
Default validation 730 ms N/A
649 ms 1392 ms
1. When schema validation is functionally important, make sure to use it; just be
aware that it will add a significant performance penalty to parsing the data.
2. Always reuse schemas to minimize the effect of validation.
Building a Document Object Model (DOM) or JSON object is a relatively simple series of
calls. The object itself is created with an underlying parser, so it is important to configure the
parser for optimal performance (in the case of DOM, the StAX parser is used by default).
DOM objects are created with a DocumentBuilder object that is retrieved from the Docu-
mentBuilderFactory . The default document builder factory is specified via the
javax.xml.parsers.DocumentBuilderFactory property (or the META-INF/services file
of that name). Just as it is important to configure a property for optimal performance when
creating a parser, it is important to configure that system property for optimal performance
when creating document builders.
Like SAX parsers, DocumentBuilder objects can be reused as long as their reset() method
is called in between uses.
JSON objects are created with a JsonReader object that is retrieved from the Json object
directly (by calling the Json.createReader() method) or from a JsonReaderFactory ob-