Java Reference
In-Depth Information
slightly compatible with the other, the W3C subsequently took charge of DOM's de-
velopmenttopromotestandardization,andhassofarreleasedDOMLevels1,2,and3.
Java 7 supports all three DOM levels through its DOM API.
DOM has two big advantages over SAX. First, DOM permits random access to a
document'sinfosetitemswhereasSAXonlypermitsserialaccess.Second,DOMlets
you also create XML documents, whereas you can only parse documents with SAX.
However,SAXisadvantageousoverDOMinthatitcanparsedocumentsofarbitrary
size,whereasthesizeofdocumentsparsedorcreatedbyDOMislimitedbytheamount
of available memory for storing the document's node-based tree structure.
ThissectionfirstintroducesyoutoDOM'streestructure.Itthentakesyouonatour
of the DOM API; you learn how to use this API to parse and create XML documents.
A Tree of Nodes
DOMviewsanXMLdocumentasatreethat'scomposedofseveralkindsofnodes.This
treehasasinglerootnode,andallnodesexceptfortheroothaveaparentnode.Also,
eachnodehasalistofchildnodes.Ifthislistisempty,thechildnodeisknownasa leaf
node.
Note DOMpermitsnodestoexistthatarenotpartofthetreestructure.Forexample,
anelementnode'sattributenodesarenotregardedaschildnodesoftheelementnode.
Also,nodescanbecreatedbutnotinsertedintothetree;theycanalsoberemovedfrom
the tree.
Eachnodehasa node name ,whichisthecompletenamefornodesthathavenames
(such as an element's or an attribute's prefixed name), and # node-type for un-
named nodes, where node-type is one of cdata-section , comment , doc-
ument , document-fragment , or text . Nodes also have local names (names
withoutprefixes),prefixes,andnamespaceURIs(althoughtheseattributesmaybenull
forcertainkindsofnodes,suchascomments).Finally,nodeshavestringvalues,which
happentobethecontentoftextnodes,commentnodes,andsimilartext-orientednodes;
normalized values of attributes; and null for everything else.
DOMclassifiesnodesintotwelvetypes,ofwhichseventypescanbeconsideredpart
of a DOM tree. All these types are described here:
Attribute node :oneofanelement'sattributes.Ithasaname,alocalname,apre-
fix,anamespace URI,andanormalized stringvalue.Thevalueis normalized
by resolving any entity references and by converting sequences of whitespace
toasinglewhitespacecharacter.Anattributenodehaschildren,whicharethe
Search WWH ::




Custom Search