📄 3_apis.html
字号:
which can legitimately be ignored would be returned as part of the normal character stream. The nonvalidating parser in Sun's Java XML library implements both of these optional behaviors -- it processes all external entities and it identifies ignorable whitespace. </p> </blockquote> <h4>Other SAX Interfaces</h4> <p>In addition to the APIs described here, the SAX APIs define a few other interfaces that you are likely to use when you write a SAX application, as well as a utility package with a number of classes that are helpful for building real-world applications. When you are ready for a bit more information pertaining to SAX, go here: <a href="3_apis_addlSAX.html">3b. Other SAX APIs</a>.</p> <blockquote> <p></p> </blockquote><blockquote> <blockquote> <p></p> </blockquote></blockquote><h3><a name="DOM"></a>The Document Object Model (DOM) APIs</h3><p>The diagram below shows the JAXP APIs in action:</p><blockquote> <p><img src="images/dom-api.gif" width="439" height="454"></p></blockquote><p>You use the <code>javax.xml.parsers.</code>DocumentBuilderFactory class to get a DocumentBuilder instance (upper left), and use that to produce a Document (a DOM) that conforms to the DOM specification (lower right). The builder you get, in fact, is determined by the System property, <tt>javax.xml.parsers.DocumentBuilderFactory</tt>, which selects the factory implementation that is used to produce the builder. (The platform's default value can be overridden from the command line.) </p> <p>You can use the builder's <code>newDocument()</code> method to create an empty Document that implements the <a href="../../api/org/w3c/dom/Document.html">org.w3c.dom.Document</a> interface. Alternatively, you can use one of the builder's parse methods to create a Document from existing XML data. The result is a DOM tree like that shown in the lower right corner of the diagram.</p> <h4><a name="DomPackages"></a>Packages</h4> <p>The Document Object Model implementation is defined in the following packages:</p> <table width="92%" border="1"> <tr> <td width="22%"><b><i>Package</i></b></td> <td width="78%"><b><i>Description</i></b></td> </tr> <tr> <td width="22%"><a href="../../api/org/w3c/dom/package-summary.html">org.w3c.dom</a> </td> <td width="78%">Defines the DOM programming interfaces for XML (and, optionally, HTML) documents, as specified by the W3C.</td> </tr> <tr> <td width="22%"><a href="../../api/javax/xml/parsers/package-frame.html">javax.xml.parsers</a></td> <td width="78%">Defines the DocumentBuilderFactory class and the DocumentBuilder class, which returns an object that implements the W3C Document interface. The factory that is used to create the builder is determined by the <tt>javax.xml.parsers</tt> system property, which can be set from the command line or overridden when invoking the <code>newInstance</code> method. This package also defines the <code>ParserConfigurationException</code> class for reporting errors.</td> </tr> <tr> <td width="22%"> <p><a href="../../api/internal/com/sun/xml/tree/package-summary.html">com.sun.xml.tree</a> </p> </td> <td width="78%"> <p>Sun's Java XML implementation of the DOM libraries, including the <code>XmlDocument</code>, <code>XmlDocumentBuilder</code>, and <code>TreeWalker</code> classes. </p> </td> </tr> </table><h3><a name="ProjectX"></a>The Project X Reference Implementation</h3><p>This section shows how the reference implementation combines the SAX and DOM APIs. </p><blockquote> <p><img src="../images/sun.gif" width="62" height="29"> <b>Note:</b><br> The material in the remainder of this section is specific to Project X, Sun's reference implementation for the JAXP standard. The material in this section is <i>not</i> part of the standard. Because it is not part of the JAXP standard, the functionality described here may very well be implemented differently in other parsers. In addition, as standards evolve, future versions of the JAXP reference implementation could employ different mechanisms to achieve the same goals. </p></blockquote><h4>Overview </h4><p>In Sun's reference implementation, the DOM API builds on the SAX API as shown in the diagram below:<blockquote> <p><img src="images/all-apis.gif" width="322" height="498"></p></blockquote><p>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. Sun's implementation also provides a framework to help output the object tree as XML data. <h4>Implementation</h4><p>The diagram below shows how Sun's DocumentBuilder operates "under the hood":</p> <p><img src="images/dom-prjx.gif" width="639" height="500"></p> <p>The section of the diagram inside the wavy orange lines shows what Sun's reference implementation does when you parse existing XML data. </p> <p>The default DocumentBuilder creates an object which implements the SAX DocumentHandler interface. It then hands that object to one of the SAX parsers (<code>Parser</code> or <code>ValidatingParser</code>, depending on how the builder factory was configured). When the input source is parsed, the DocumentHandler creates a Document object. </p> <blockquote> <p><b>Note:</b> <br> To control other aspects of the parser's behavior, you use the DocumentBuilder methods <code>setErrorHandler</code> and <code>setEntityResolver</code>. DocumentBuilder does not implement a <code>setDTDHandler</code> method, though. It is shown here only because it is part of the SAX parser. (The DTDHandler is an older, SGML-derived mechanism for handling embedded binary data. More appropriate mechanisms that accomplish the same task are discussed in the section, <a href="../sax/5d_dtd.html">Referencing Binary Entities</a>.)</p> </blockquote><h3><a name="where"></a>Where Do You Go from Here?</h3> <p>At this point, you have enough information to begin picking your own way through the XML libraries. Your next step from here depends on what you want to accomplish. You might want to go to:</p> <dl> <dl> <dt><a href="../index.html#XmlThread"><b>The XML Thread</b></a></dt> <dd>If you want to learn more about XML, spending as little time as possible on Sun's Java XML APIs. (You will see all of the XML sections in the normal course of the tutorial. Follow this thread if you want to bypass the API programming steps.)</dd> <dt> </dt> <dt><a href="4_design.html"><b>Designing an XML Data Structure</b></a></dt> <dd>If you are creating XML data structures for an application and want some tips on how to proceed. (This is the next step in the XML overview.)</dd> <dt> </dt> <dt> </dt> <dt><a href="../sax/index.html"><strong>Serial Access with the Simple API for XML (SAX)</strong></a></dt> <dd>If the data structures have already been determined, and you are writing a server application or an XML filter that needs to do the fastest possible processing.</dd> <dt> </dt> <dt><strong><a href="../dom/index.html">Manipulating Document Contents with the Document Object Model (DOM)</a></strong><i><font color="#FF0000"></font></i></dt> <dd>If you need to build an object tree from XML data so you can manipulate it in an application, or convert an in-memory tree of objects to XML. </dd> <dt> </dt> <dt><b><a name="examples"></a>Browse the Examples</b> </dt> <dd>To see some real code. Sun's Java XML libraries come with a large number of examples (even though many of them may not make much sense just yet). You can find them in the JAXP <tt>examples</tt> directory, or you can browse to the <a href="../../../examples/index.html">XML Examples</a> page. The table below divides them into categories depending on whether they are primarily SAX-related, are primarily DOM-related, or serve some special purpose.<br> <br> </dd> </dl> <blockquote> <blockquote> <table width="100%" border="1"> <tr> <td width="17%"><i><b>Example</b></i></td> <td width="83%"><i><b>Description</b></i></td> </tr> <tr> <td width="17%">samples</td> <td width="83%">Sample XML files</td> </tr> <tr> <td width="17%">simple</td> <td width="83%">A very short example that creates a DOM using <code>XmlDocument</code>'s static <code>createXmlDocument</code> method and echoes it to <code>System.out</code>. Illustrates the least amount of coding necessary to read in XML data, assuming you can live with all the defaults -- for example, the default error handler, which ignores errors.</td> </tr> <tr> <td width="17%">dom</td> <td width="83%">A program that creates a Document Object Model in memory and uses it to output an XML structure.</td> </tr> <tr> <td width="17%">gui</td> <td width="83%">An example that reads XML data into a DOM and populates a JTree.</td> </tr> <tr> <td width="100%" colspan="2"> </td> </tr> <tr> <td width="17%">sax</td> <td width="83%">An application that uses the SAX API to echo the content and structure of an XML document using either the validating or non-validating parser, on either a well <hr size=4> -formed, valid, or invalid document so you can see the difference in errors that the parsers report. Lets you set the <code>org.xml.sax.parser</code> system variable on the command line to determine the parser returned by <code>org.xml.sax.helpers.ParserFactory</code>.</td> </tr> <tr> <td width="100%" colspan="2"> </td> </tr> <tr> <td width="17%">namespaces</td> <td width="83%">An application that reads an XML document into a DOM and echoes its namespaces.</td> </tr> <tr> <td width="17%">rpc</td> <td width="83%">A client/servlet pair that illustrates XML transmitted over a Remote Process Communication (RPC) connection and converted into a DOM.</td> </tr> <tr> <td width="17%">transcode</td> <td width="83%">A character set translation example. A document written with one character set is converted to another.</td> </tr> <tr> <td width="17%">jhtml</td> <td width="83%">A servlet for use with the Java Web Server<sup><font size="1">TM</font></sup> that validates an XML document. Precursor to a future JSP version.</td> </tr> </table> </blockquote> </blockquote></dl> <p> <p> <table width="100%"> <tr> <td align=left> <a href="2_specs.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><ahref="4_design.html"><img src="../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top 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></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -