Abbey Workshop | ||||||||||
This tip includes an example of how an XML file is parsed using the SAX event model and JAXP. The parser object are created on lines 15-17. First a factory object is created and this is used to configure the parser. Then, a parser object is created which will parse our XML file. Since SAX parsing is event driven, an event handler is created on line 20. This is where methods are created for managing SAX events.
Notice on line 16 the parser is made namespace aware. This is done so we can use the local part of the tag name passed, not the qname.
Download source for: MyParser.java
The methods declared in this class define the events that we will handle. Since the DefaultHandler class is used, we override only the methods we need. A method does not need to be written for each possible type of event. See the class documentation for DefaultHandler for a list of all of the possible events.
The following are links to sample XML files you can use with this class as a test: todo.xml | todons.xml
Copyright © Abbey Workshop 2006