Datasheet

5: * Example from "Professional XML Development with Apache Tools"
6: *
7: */
8: package com.sauria.apachexml.ch1;
9:
10: import java.io.IOException;
11: import java.util.Stack;
12:
13: import org.apache.xerces.xni.XMLAttributes;
14: import org.apache.xerces.xni.XNIException;
15: import org.apache.xerces.xni.parser.XMLInputSource;
16: import org.cyberneko.pull.XMLEvent;
17: import org.cyberneko.pull.XMLPullParser;
18: import org.cyberneko.pull.event.CharactersEvent;
19: import org.cyberneko.pull.event.ElementEvent;
20: import org.cyberneko.pull.parsers.Xerces2;
21:
22: public class NekoPullMain {
23:
24: public static void main(String[] args) {
25: try {
26: XMLInputSource is =
27: new XMLInputSource(null, args[0], null);
28: XMLPullParser pullParser = new Xerces2();
29: pullParser.setInputSource(is);
30: Book book = makeBook(pullParser);
You start by instantiating an instance of the pull parser and setting it up with the input source for the
document. Then you pass the parser, which is at the correct position to start reading a book, to a con-
structor function for the Book class.
31: System.out.println(book.toString());
32: } catch (IOException ioe) {
33: ioe.printStackTrace();
34: }
35: }
36:
37: private static Book makeBook(XMLPullParser pullParser)
38: throws IOException {
39: Book book = null;
40: Stack textStack = new Stack();
When you ask the parser for the next bit of XML, you get back an event. That event is an object (a struct,
really) that contains all the information about the piece of XML the parser saw. NekoPull includes event
types for the document, elements, character data, CDATA, comments, text declaration, DOCTYPE decla-
ration, processing instructions, entities, and namespace prefix mappings. The event types are deter-
mined by integer values in the type field of XMLEvent. Some of the events are bounded; that is, they
correspond to a start/end pairing and are reported twice. The bounded events are DocumentEvent,
ElementEvent, GeneralEntityEvent, CDATAEvent, and PrefixMappingEvent; a boolean field called start
distinguishes start events from end events.
You loop and call pullParser’s nextEvent method to get events until there aren’t any more (or until you
break out of the loop):
46
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 46