Java Reference
In-Depth Information
Element: movie, null, null, null
Element: name, null, null, null
Text: Le Fabuleux Destin d'Amélie Poulain
Element: language, null, null, null
Text: français
The output is pretty much as expected, but there is one problem: the XML declara-
tion's encoding attributehasnotbeensetto ISO-8859-1 .Itturnsoutthatyoucan-
notaccomplishthistaskviatheDOMAPI.Instead,youneedtousetheXSLTAPIfor
thistask.WhileexploringXSLT,you'lllearnhowtosetthe encoding attribute,and
you'll also learn how to output this tree to an XML document file.
However,thereisonemoredocument-parsing-and-document-creationAPItoexplore
(and a tour of the XPath API to accomplish) before we turn our attention to XSLT.
Parsing and Creating XML Documents with StAX
Streaming API for XML (StAX) is an API for parsing an XML document sequentially
fromstarttofinish.ItisalsoadocumentcreationAPI.StAXbecameacoreJavaAPIin
the Java 6 release (in late 2006).
STAX VERSUS SAX AND DOM
Because Java already supports SAX and DOM for document parsing and DOM for
documentcreation,youmightbewonderingwhyanotherXMLAPIisneeded.The
following points justify StAX's presence in core Java:
• StAX(likeSAX)canbeusedtoparsedocumentsofarbitrarysizes.Incon-
trast, the maximum size of documents parsed by DOM is limited by the
available memory, which makes DOM unsuitable for mobile devices with
limited amounts of memory.
• StAX(likeDOM)canbeusedtocreatedocumentsofarbitrarysizes.Incon-
trast, the maximum size of a document created by DOM is constrained by
available memory. SAX cannot be used to create documents.
• StAX (like SAX) makes infoset items available to applications almost im-
mediately.Incontrast,theseitemsarenotmadeavailablebyDOMuntilafter
it finishes building the tree of nodes.
• StAX (like DOM) adopts the pull model , in which the application tells the
parserwhenitisreadytoreceivethenextinfosetitem.Thismodelisbased
Search WWH ::




Custom Search