📄 3_apis.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><TITLE>API Overview, Javasoft XML APIs</TITLE><STYLE TYPE=text/css></STYLE><META NAME=fileID CONTENT=quickIntro><META NAME=keywords CONTENT=XML></HEAD><BODY BGCOLOR=#ffffff><HR><B>HtmlDiff: </B>Here is the <A href=#DIFF0>first difference.</A> There are 34 differences. <IMG src=../diffpics/oold.gif><STRIKE>is old.</STRIKE><IMG src=../diffpics/onew.gif><STRONG><I> is new. </I></STRONG><HR><TABLE WIDTH=100%> <TR> <TD ALIGN=left> <A HREF=2_specs.html><IMG SRC=../images/PreviousArrow.gif WIDTH=26 HEIGHT=26 ALIGN=bottom BORDER=0 ALT="Previous | "></A><A HREF=4_design.html><IMG SRC=../images/NextArrow.gif WIDTH=26 HEIGHT=26 ALIGN=bottom BORDER=0 ALT="Next | "></A><A HREF=../alphaIndex.html><IMG SRC=../images/xml_IDX.gif WIDTH=26 HEIGHT=26 ALIGN=bottom BORDER=0 ALT="Index | "></A><A HREF=../TOC.html><IMG SRC=../images/xml_TOC.gif WIDTH=26 HEIGHT=26 ALIGN=bottom BORDER=0 ALT="TOC | "></A><A HREF=../index.html><IMG SRC=../images/xml_Top.gif WIDTH=26 HEIGHT=26 ALIGN=bottom BORDER=0 ALT="Top | "></A></TD> <TD ALIGN=right><STRONG><EM><A HREF=index.html>Top</A></EM></STRONG> <A HREF=../TOC.html#intro><STRONG><EM>Contents</EM></STRONG></A> <A HREF=../alphaIndex.html><STRONG><EM>Index</EM></STRONG></A> <A HREF=../glossary.html><STRONG><EM>Glossary</EM></STRONG></A> </TD> </TR></TABLE><P> <CENTER> <IMG SRC=../images/shoeline2.gif ALIGN=BOTTOM BORDER=0 WIDTH=202 HEIGHT=25 NATURALSIZEFLAG=3> <IMG SRC=../images/shoeline2.gif ALIGN=BOTTOM BORDER=0 WIDTH=202 HEIGHT=25 NATURALSIZEFLAG=3> </CENTER><BLOCKQUOTE> <BLOCKQUOTE> <HR SIZE=4> </BLOCKQUOTE></BLOCKQUOTE><P> <H2> <A name=DIFF0></A><A href=#DIFF1><IMG src=../diffpics/oold.gif></A><STRIKE>An Overview of Sun's Java XML APIs </STRIKE><A name=DIFF0></A><A href=#DIFF1><IMG src=../diffpics/onew.gif></A><STRONG><I>3. An Overview of the APIs</STRONG></I></H2><TABLE WIDTH=40% BORDER=1 ALIGN=right> <TR> <TD> <DIV ALIGN=center><B><I>Link Summary</I></B></DIV> </TD> </TR> <TR> <TD> <DL> <DT><B><I>Local Links</I></B></DT> </DL> <UL> <LI><A HREF=3_apis_addlSAX.html>Other SAX APIs</A></LI> <LI><A HREF=../index.html#XmlThread>The XML Thread</A></LI> <LI><A HREF=4_design.html>Designing an XML Data Structure</A></LI> <LI><A HREF=../sax/index.html>The Simple API for XML (SAX)</A></LI> <LI><A HREF=../dom/index.html>The Document Object Model (DOM)</A></LI> <LI><A HREF=../../../examples/index.html>Examples</A></LI> </UL> <P><B><I>API References</I></B></P> <UL> <LI><A name=DIFF1></A><A href=#DIFF2><IMG src=../diffpics/oold.gif></A><STRIKE>ParserFactory </STRIKE><A name=DIFF1></A><A href=#DIFF2><IMG src=../diffpics/onew.gif></A><STRONG><I><A HREF=../../api/javax/xml/parsers/SAXParserFactory.html>javax.xml.parsers.SAXParserFactory</A></STRONG></I></LI> <LI><A name=DIFF2></A><A href=#DIFF3><IMG src=../diffpics/onew.gif></A><STRONG><I><A HREF=../../api/javax/xml/parsers/DocumentBuilderFactory.html>javax.xml.parsers.DocumentBuilderFactory</A></LI> <LI><A HREF=../../api/javax/xml/parsers/DocumentBuilder.html>javax.xml.parsers.DocumentBuilder</A></STRONG></I></LI> <LI><A HREF=../../api/org/xml/sax/Parser.html>org.xml.sax.Parser</A></LI> <LI><A HREF=../../api/com/sun/xml/parser/package-summary.html>com.sun.xml.parser</A></LI> <LI><A HREF=../../api/org/xml/sax/HandlerBase.html>org.xml.sax.HandlerBase</A></LI> <LI><A HREF=../../api/org/xml/sax/helpers/package-summary.html>org.xml.sax.helpers</A></LI> <LI><A HREF=../../api/org/w3c/dom/package-summary.html>org.w3c.dom</A></LI> <LI><A name=DIFF3></A><A href=#DIFF4><IMG src=../diffpics/onew.gif></A><STRONG><I><A HREF=../../api/org/w3c/dom/Document.html>org.w3c.dom.Document</A></STRONG></I></LI> <LI><A HREF=../../api/com/sun/xml/tree/package-summary.html>com.sun.xml.tree</A></LI> <LI><A name=DIFF4></A><A href=#DIFF5><IMG src=../diffpics/onew.gif></A><STRONG><I><A HREF=../../api/com/sun/xml/treediff/package-summary.html>com.sun.xml.treediff</A></LI> <LI><A HREF=../../api/com/sun/xml/xhtml/package-summary.html>com.sun.xml.xhtml</A></STRONG></I></LI> </UL> <P><B><I>Glossary Terms</I></B></P> <DL> <DD><A HREF=../glossary.html#namespace>namespace</A>, <A HREF=../glossary.html#prolog>prolog</A>, <A HREF=../glossary.html#URL>URL</A>, <A HREF=../glossary.html#URN>URN</A>, <A HREF=../glossary.html#w3c>W3C</A> <A HREF=../glossary.html#prolog></A> </DD> </DL> <DL> <DT> </DT> </DL> </TD> </TR></TABLE><P> This page gives you a map so you can find your way around <A name=DIFF5></A><A href=#DIFF6><IMG src=../diffpics/oold.gif></A><STRIKE>Sun's Java </STRIKE><A name=DIFF5></A><A href=#DIFF6><IMG src=../diffpics/onew.gif></A><STRONG><I>JAXP and the associated </STRONG></I>XML APIs. <A name=DIFF6></A><A href=#DIFF7><IMG src=../diffpics/oold.gif></A><STRIKE>The DOM API builds on the SAX API as shown here: </STRIKE><H3><A name=DIFF7></A><A href=#DIFF8><IMG src=../diffpics/onew.gif></A><STRONG><I><A NAME=JAXP></A>The JAXP APIs</STRONG></I></H3><P><A name=DIFF8></A><A href=#DIFF9><IMG src=../diffpics/onew.gif></A><STRONG><I>The JAXP APIs, contained in the <TT>jaxp.jar</TT> file, are comprised of the <CODE>javax.xml.parsers</CODE> package. That package contains two vendor-neutral factory classes: <A HREF=../../api/javax/xml/parsers/SAXParserFactory.html>SAXParserFactory</A> and <A HREF=../../api/javax/xml/parsers/DocumentBuilderFactory.html>DocumentBuilderFactory</A> that give you a SAX parser and a DocumentBuilder, respectively. The <A HREF=../../api/javax/xml/parsers/DocumentBuilder.html>DocumentBuilder</A>, in turn, creates DOM-compliant <A HREF=../../api/org/w3c/dom/Document.html>Document</A> object. </STRONG></I><P><A name=DIFF9></A><A href=#DIFF10><IMG src=../diffpics/onew.gif></A><STRONG><I>The factory APIs give you the ability to plug in an XML implementation offered by another vendor without changing your source code. The implementation you get depends on the setting of the <TT>javax.xml.parsers.SAXParserFactory</TT> and <TT>javax.xml.parsers.DocumentBuilderFactory</TT> system properties. The default values (unless overridden at runtime) point to Sun's reference implementations at <CODE>com.sun.xml</CODE>. <P>The remainder of this section shows how those APIs relate to each other in an application. As you read, watch for this logo: <IMG SRC=../images/sun.gif WIDTH=62 HEIGHT=29> It identifies the parts of the discussion that deal exclusively with Sun's reference implementation.<H3><A NAME=overview></A>An Overview of SAX and DOM</H3><P>As discussed in the previous section, the SAX and DOM APIs are defined by XML-DEV group and by the <A HREF=../glossary.html#w3c>W3C</A>, respectively. The libraries that define those APIs are included in the <TT>parser.jar</TT> file, which also contains Sun's reference implementation, Project X. </STRONG></I><P>The "Simple API" for XML (SAX) is the event-driven, serial-access mechanism that does element-by-element processing. The API for this level reads and writes XML to a data repository or the Web. For server-side and high-performance apps, you will want to fully understand this level. But for many applications, a minimal understanding will suffice. <P><A name=DIFF10></A><A href=#DIFF11><IMG src=../diffpics/oold.gif></A><STRIKE>Sun's implementation of the Document Object Model (DOM) API uses the SAX libraries to read in XML data and construct the tree of data objects that constitutes the DOM. </STRIKE><A name=DIFF10></A><A href=#DIFF11><IMG src=../diffpics/onew.gif></A><STRONG><I>The DOM API is generally an easier API to use. It provides a relatively familiar tree structure of objects. </STRONG></I>You can <A name=DIFF11></A><A href=#DIFF12><IMG src=../diffpics/oold.gif></A><STRIKE>then </STRIKE>use the DOM API to manipulate the hierarchy of application objects it encapsulates. The DOM API is ideal for interactive applications because the entire object model is present in memory, where it can be accessed and manipulated by the user. <P><A name=DIFF12></A><A href=#DIFF13><IMG src=../diffpics/oold.gif></A><STRIKE>The DOM API is also an easier API to use. It hides the intricacies of the SAX API, and provides a relatively familiar tree structure of objects. It also provides a framework to help output the object tree as XML data. </STRIKE>On the other hand, constructing the DOM requires reading the entire XML structure and holding the object tree in memory, so it is much more CPU and memory intensive. For that reason, the SAX API will tend to be preferred for server-side applications and data filters that do not require an in-memory representation of the data. <H3><A name=DIFF13></A><A href=#DIFF14><IMG src=../diffpics/oold.gif></A><STRIKE>The SAX Parser </STRIKE><A name=DIFF13></A><A href=#DIFF14><IMG src=../diffpics/onew.gif></A><STRONG><I><A NAME=SAX></A>The SAX APIs</STRONG></I></H3> <TABLE ALIGN=right WIDTH=40% BORDER=1> <TR> <TD><A name=DIFF14></A><A href=#DIFF15><IMG src=../diffpics/onew.gif></A><STRONG><I><IMG SRC=images/sax-api.gif WIDTH=384 HEIGHT=471></STRONG></I></TD> </TR> </TABLE>The basic outline of a SAX parser is shown at right. <A name=DIFF15></A><A href=#DIFF16><IMG src=../diffpics/oold.gif></A><STRIKE>First the ParserFactory generates an instance of the parser. </STRIKE><A name=DIFF15></A><A href=#DIFF16><IMG src=../diffpics/onew.gif></A><STRONG><I>First, the <CODE>SAXParserFactory</CODE> shown at the top generates an instance of the parser. </STRONG></I><P>The XML text is shown coming in to the parser from the left. As the data is parsed, the parser invokes one of several callback methods defined by the interfaces <CODE>DocumentHandler</CODE>, <CODE>ErrorHandler</CODE>, <CODE><A name=DIFF16></A><A href=#DIFF17><IMG src=../diffpics/oold.gif></A><STRIKE>DTDEventHandler </STRIKE><A name=DIFF16></A><A href=#DIFF17><IMG src=../diffpics/onew.gif></A><STRONG><I>DTDHandler</STRONG></I></CODE>, and <CODE>EntityResolver</CODE>. </P> <P>Here is a summary of the key SAX APIs: </P> <DL> <DT><A name=DIFF17></A><A href=#DIFF18><IMG src=../diffpics/oold.gif></A><STRIKE>ParserFactory </STRIKE><A name=DIFF17></A><A href=#DIFF18><IMG src=../diffpics/onew.gif></A><STRONG><I><CODE><B><A NAME=ParserFactory></A>SAXParserFactory</B></CODE></STRONG></I></DT> <DD><A name=DIFF18></A><A href=#DIFF19><IMG src=../diffpics/oold.gif></A><STRIKE>The ParserFactory class defines the makeParser() method, which uses the parser specified by the system property, org.xml.sax.parser . (An exception is thrown if this system property is not defined.) This class also defines makeParser(String), which lets you specify the classname of the parser to use. </STRIKE><A name=DIFF18></A><A href=#DIFF19><IMG src=../diffpics/onew.gif></A><STRONG><I>A <A HREF=../../api/javax/xml/parsers/SAXParserFactory.html>SAXParserFactory</A> object creates an instance of the parser determined by the system property, <CODE>javax.xml.parsers</CODE><TT>.SAXParserFactory</TT>.</STRONG></I></DD> <DT><CODE><B><A NAME=Parser></A>Parser</B></CODE></DT> <DD>The <A HREF=../../api/org/xml/sax/Parser.html><CODE>org.xml.sax.Parser</CODE></A> interface defines methods like <CODE>setDocumentHandler</CODE> to set up event handlers and <CODE>parse(URL)</CODE> to actually do the parsing. This interface is implemented by the <CODE>Parser</CODE> and <CODE>ValidatingParser</CODE> classes in the <A HREF=../../api/com/sun/xml/parser/package-summary.html><CODE>com.sun.xml.parser</CODE></A> package.</DD> <DT> </DT> <DT><CODE><B><A NAME=DocumentHandler></A>DocumentHandler</B></CODE></DT> <DD>Methods like <CODE>startDocument</CODE>, <CODE>endDocument</CODE>, <CODE>startElement</CODE>, and <CODE>endElement</CODE> are invoked when an XML tag is recognized. This interface also defines methods <CODE>characters</CODE> and <CODE>processingInstruction</CODE>, which are invoked when the parser encounters the text in an XML element or an inline processing instruction, respectively.</DD> <DT> </DT> <DT><CODE><B><A NAME=ErrorHandler></A>ErrorHandler</B></CODE></DT> <DD>Methods <CODE>error</CODE>, <CODE>fatalError</CODE>, and <CODE>warning</CODE> are invoked in response to various parsing errors. The default error handler throws an exception for fatal errors and ignores other errors (including validation errors). That's one reason you need to know something about the SAX parser, even if you are using the DOM. Sometimes, the application may be able to recover from a validation error. Other times, it may need to generate an exception. To ensure the correct handling, you'll need to supply your own error handler to the parser.</DD> <DT> </DT> <DT><CODE><B><A NAME=DTDHandler></A>DTDHandler</B></CODE></DT> <DD>Methods defined in this interface are invoked when processing definitions in a <A HREF=../glossary.html#DTD>DTD</A>. These methods are discussed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -