Java Reference
In-Depth Information
After verifying the number of command-line arguments, Listing 10-16 ' s main()
methodcreatesafactory,usesthefactorytocreateastream-basedreaderthatobtainsits
XMLdatafromthefileidentifiedbythesolitarycommand-lineargument,andentersa
parsingloop.Whenever next() returns XMLStreamReader.START_ELEMENT or
XMLStreamReader.END_ELEMENT , XMLStreamReader 's getName() meth-
od is called to return the element's qualified name.
Forexample,whenyouexecute StAXDemo against Listing10-2 ' smoviedocument
file ( movie.xml ), this application generates the following output:
START_ELEMENT
Qname = movie
START_ELEMENT
Qname = name
END_ELEMENT
Qname = name
START_ELEMENT
Qname = language
END_ELEMENT
Qname = language
END_ELEMENT
Qname = movie
Note : XMLStreamReader declaresa void close() methodthatyouwillwant
to call to free any resources associated with this stream-based reader when your ap-
plicationisdesignedtorunforanextendedperiodoftime.Callingthismethoddoesn't
close the underlying input source.
Parsing Documents with Event-Based Readers
An event-based reader is created by calling one of XMLInputFactory 's cre-
ateXMLEventReader() methods, such as XMLEventReader cre-
ateXMLEventReader(Reader reader) . These methods throw
XMLStreamException when the event-based reader cannot be created.
The following example creates an event-based reader whose source is a file named
recipe.xml :
Reader reader = new FileReader("recipe.xml");
XMLEventReader xmler = xmlif.createXMLEventReader(reader);
Search WWH ::




Custom Search