Java Reference
In-Depth Information
return new InputSource("recipeml.dtd") else return null;
statementwouldsuffice),I'vechosentouseamapincaseIwanttoexpandthenumber
of mappings in the future. In another scenario, you would probably find a map to be
very convenient. For example, it's easier to use a map than to use a series of if state-
mentsinacustomentityresolverthatmapsXHTML'sstrict,transitional,andframeset
formal public identifiers, andalso maps its various entity sets tolocal copies ofthese
document files.
Theoverriding resolveEntity() methoduses publicId 'sargumenttolocate
thecorrespondingsystemidentifierinthemap—the systemId parametervalueisig-
noredbecauseitneverreferstothelocalcopyof recipeml.dtd .Whenthemapping
isfound,an InputSource objectiscreatedandreturned.Ifthemappingcouldnotbe
found, the null reference would be returned.
To install this custom entity resolver in SAXDemo , specify xm-
lr.setEntityResolver(new LocalRecipeML()); prior to the parse()
methodcall.Afterrecompilingthesourcecode,execute java SAXDemo gcs.xml ,
where gcs.xml stores Listing10-12 ' stext.Intheresultingoutput,youshouldobserve
the message “ obtaining
recipeml.dtd ” prior to the call to
cached
startEntity() .
Tip TheSAXAPIincludesan org.xml.sax.ext.EntityResolver2 inter-
face that provides improved supportforresolving entities. Ifyouprefer toimplement
EntityResolver2 instead of EntityResolver , you must replace the
setEntityResolver() calltoinstalltheentityresolverwitha setFeature()
call whose feature name is use-entity-resolver2 (don't forget the ht-
tp://xml.org/sax/features/ prefix).
Parsing and Creating XML Documents with DOM
Document Object Model (DOM) is an API for parsing an XML document into an in-
memorytreeofnodes,andforcreatinganXMLdocumentfromatreeofnodes.Aftera
DOMparserhascreatedadocumenttree,anapplicationusestheDOMAPItonavigate
over and extract infoset items from the tree's nodes.
Note DOM originated as an object model for the Netscape Navigator 3 and Mi-
crosoft Internet Explorer 3 web browsers. Collectively, these implementations are
known as DOM Level 0. Because each vendor's DOM implementation was only
Search WWH ::




Custom Search