An Example
The XML file of Listing 15.22 serves as input.
|
Listing 15.22 musicians.xml-Info about Musicians
|
1: <?xml version="1.0"?> 2: <musicians> 3: <musician> 4: <name>Joey Baron 5: </name> 6: <instrument>drums 7: </instrument> 8: <NrOfRecordings>1 9: </NrOfRecordings> 10: </musician> 11: <musician> 12: <name>Bill Frisell 13: </name> 14: <instrument>guitar 15: </instrument> 16: <NrOfRecordings>3 17: </NrOfRecordings> 18: </musician> 19: <musician> 20: <name>Don Byron 21: </name> 22: <instrument>clarinet 23: </instrument> 24: <NrOfRecordings>2 25: </NrOfRecordings> 26: </musician> 27: <musician> 28: <name>Dave Douglas 29: </name> 30: <instrument>trumpet 31: </instrument> 32: <NrOfRecordings>1 33: </NrOfRecordings> 34: </musician> 35: </musicians>
The OurHandler class is defined in Listing 15.23.
Listing 15.23 OurHandler.java-Concrete Implementations of Event Handlers
1: import org.xml.sax.HandlerBase;
2: import org.xml.sax.AttributeList;
3: import java.io.*;
4:
5: public class OurHandler extends HandlerBase { //subclassing
HandlerBase
6:
7: private PrintWriter fout;
8:
9: public OurHandler() throws IOException
10: {
11: fout = new PrintWriter(new FileWriter("out.htm"));
12: //object created for writing to the file "out.htm"
13: }
14:
15: public void startDocument ()
16: {
17: fout.println("<HTML>");
18: fout.println("<HEAD><TITLE>SAX
example</TITLE></HEAD>");
19: fout.println("<BODY>");
20: }
21:
22: public void endDocument ()
23: {
24: fout.println("</BODY></HTML>");
25: fout.close();
26: }
27:
28: public void startElement (String name, AttributeList atts)
29: {
30: if (name == "musicians")
31: fout.println("<TABLE BORDER='1'
CELLPADDING='5'>");
32: else if (name == "musician")
33: fout.println("<TR>");
34: else
35: fout.println("<TD>");
36: }
37:
38: public void endElement (String name)
39: {
40: if (name == "musicians")
41: fout.println("</TABLE>");
42: else if (name == "musician")
43: fout.println("</TR>");
44: else
45: fout.println("</TD>");
46: }
47:
48: public void characters (char ch[], int start, int length)
49: {
50: for (int i=start; i < start+length; i++)
51: fout.print(ch[i]);
52: }
53: }
| First we import supporting classes and interfaces (lines 1-3). Then we start (line 5) our subclassing of the HandlerBase class. |
On line 11 we create an object for writing to a file named out.htm.
Starting from line 15 you will see the different event handlers:
- • At the event start of the musicians.xml document, the HTML content defined on lines 17, 18, and 19 is sent to the file out.htm
- • At the event end of the musicians.xml document, the HTML content defined on line 24 is sent to the file out.htm
-
• At the event start of an element, additional testing is done (lines 30-35)
- • If the name of the element started is musicians, then output a table starttag
- • If the name of the element started is musician then output a table row <TR> starttag
- • If the name of the element started is something else then output a table cell <TD> starttag
- • At the event end of an element the same testing is done
- • At the event characters encountered, these are sent to the output
We also need to create an application (see Listing 15.24) to parse the document using our handler.
Listing 15.24 convert.java-The Program that Uses OurHandler Class
1: import org.xml.sax.Parser;
2: import org.xml.sax.DocumentHandler;
3: import org.xml.sax.helpers.ParserFactory;
4:
5: public class convert {
6: static final String parserClass = "com.ibm.xml.parser.SAXDriver";
7: //using IBM's XML parser
8: //static final String parserClass =
"com.datachannel.xml.sax.SAXDriver";
9: //in case we want to use the DXP parser
10: static final String xmlfile="c:\\xmlex\\musicians.xml";
11:
12: public static void main (String args[]) throws Exception
13: {
14: Parser parser; //variable declaration
15: //the name parser will be used to refer to a
Parser object
16: DocumentHandler handler; //variable declaration
17: //the name handler will be used to refer to a
DocumentHandler object
18: parser = ParserFactory.makeParser(parserClass);
19: //A Parser object is created by supplying a class
name to the ParserFactory
20: handler = new OurHandler();
21: //The new object is created and initialized
22:
23: parser.setDocumentHandler(handler);
24: //handler is registered with the parser
25: parser.parse(xmlfile);
26: //our xml-file is parsed
27:
28: }
29: }
| We declare a variable with the name parserClass and of type String. We assign a value to this variable-the full classname of the SAX driver of the parser we will be using: com.ibm.xml.parser.SAXdriver (line 6). This value can be replaced by com.datachannel.xml.sax.SAXdriver, for example (line 10), or com.microstar.xml.SAXdriver. |
The rest of the code is about declaring variables and instantiating them (lines 14-21).
Finally (line 23) the handler OurHandler is registered with the parser and subsequently our XML file, musicians.xml, is being parsed.
In Figure 15.4 you'll see both files, OurHandler.java and convert.java, edited and compiled in the KAWA IDE (http://www.tek-tools.com/kawa/).