Java Reference
In-Depth Information
SPACE
The cursor moves forward through the document from start to finish, pointing to each item
along the way. If you're used to using SAX, this should be familiar.
Let's start with a very simple and rather poorly defined XML file as an example. Use this
XML file as the basis for your parsing:
<catalog>
<book sku="123_xaa">
<title>King Lear</title>
<author>William Shakespeare</author>
<price>6.95</price>
<category>classics</category>
</book>
<book sku="988_yty">
<title>Hamlet</title>
<author>William Shakespeare</author>
<price>5.95</price>
<category>classics</category>
</book>
<book sku="434_asd">
<title>1984</title>
<author>George Orwell</author>
<price>12.95</price>
<category>classics</category>
</book>
<book sku="876_pep">
<title>Java Generics and Collections</title>
<authors>
<author>Maurice Naftalin</author>
<author>Phillip Wadler</author>
</authors>
<price>34.99</price>
<category>programming</category>
</book>
</catalog>
So here you have a catalog that holds a bunch of topics, presumably for sale. Each book has
an identifier, a title, one or more authors, and so on. The trickiest part of the catalog.xmlfile
is that the authors element is optional, used only if the topic has more than one author.
The program in Example 3-1 illustrates how to use the cursor parsing method in StAX. Here
the author objects are stored in a TreeSet as they are discovered; the Tree part provides nat-
ural sorting, and the Set part ensures uniqueness.
Example3-1.Using the cursor parsing method in StAX
Search WWH ::




Custom Search