Java Reference
In-Depth Information
DOMImplementationRegistry . newInstance ();
DOMImplementation domImpl = registry . getDOMImplementation ( "LS 3.0" );
domLS = ( DOMImplementationLS ) domImpl ;
LSParser lsp = domLS . createLSParser ( DOMImplementationLS . MODE_SYNCHRONOUS ,
"" );
lsp . setFilter ( new
new InputFilter ());
LSInput lsi = domLS . createLSInput ();
lsi . setByteStream ( is );
Document doc = lsp . parse ( lsi );
In the end, a Document object is created, just as it would have been without filtering the in-
put—but in this case, the resulting document is much smaller. That is the point of filtering:
the actual parsing and filtering will take much longer to produce a filtered document than a
document that contains all the original data. Because the document occupies less memory, it
is a useful technique if the document will be long-lived (or if there are many such documents
in use), since it reduces pressure on the garbage collector.
Table 10-9 shows the difference for parsing speeds of the usual XML file when constructing
a DOM object representing only half (10) of the items.
Table 10-9. Effect of filtering a DOM document
Standard DOM Filtering DOM
Time to create DOM 348 ms
417 ms
Size of DOM
101,440 bytes
58,824 bytes
1. DOM and JsonObject models of data are more powerful to work with than
simple parsers, but the time to construct the model can be significant.
2. Filtering data out of the model will take even more time than constructing the de-
fault model, but that can still be worthwhile for long-lived or very large docu-
Search WWH ::

Custom Search