Java Reference
In-Depth Information
Processing only the necessary data here gives a predictable performance benefit. Table 10-3
shows the average time in milliseconds to parse the sample document assuming parsing stops
after 10 items, and to process the entire document. Stopping after finding 10 items does not
save 50% of the time (because there are other sections of the document that still get parsed),
but the difference is still significant.
Table 10-3. Performance of pull parsers
Items processed XML parser JSON parser
10
143 ms
68 ms
20
265 ms
146 ms
Push parsers (SAX)
The standard XML parser is a SAX (Simple API for XML) parser. The SAX parser is a push
parser: it reads the data and when it finds a token, it executes a callback to some class that is
expected to handle the token. The parsing logic for the test remains the same, but the logic
now appears in callback methods defined in a class:
protected
protected class
class CustomizedInnerHandler
CustomizedInnerHandler extends
extends DefaultHandler {
public
public void
void startElement ( String space , String name ,
String raw , Attributes atts ) {
iif ( name . length () == 0 )
name = raw ;
iif ( name . equalsIgnoreCase ( ITEM_ID ))
isItemID = true
true ;
}
public
public void
void characters ( char
char [] ch , int
int start ,
int
int length ) throws
throws SAXDoneException {
iif ( isItemID ) {
String s = new
new String ( ch , start , length );
isItemID = false
false ;
iif ( addItemId ( s )) {
throw
throw new
new SAXDoneException ( "Done" );
}
}
}
}
Search WWH ::




Custom Search