📄 jaxpdom2.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>When to Use DOM</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JAXPDOM.html" /> <link rel="Next" href="JAXPDOM3.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JAXPDOM.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPDOM3.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp67733"> </a><h2 class="pHeading1">When to Use DOM</h2><a name="wp67744"> </a><p class="pBody">The Document Object Model (DOM) is a standard that is, above all, designed for <span style="font-style: italic">documents</span> (for example, articles and books). In addition, the JAXP 1.2 implementation supports XML Schema, which may be an important consideration for any given application.</p><a name="wp69253"> </a><p class="pBody">On the other hand, if you are dealing with simple <span style="font-style: italic">data</span> structures, and if XML Schema isn't a big part of your plans, then you may find that one of the more object-oriented standards like <a href="IntroXML3.html#wp66315">JDOM and dom4j</a> is better suited for your purpose. </p><a name="wp67780"> </a><p class="pBody">From the start, DOM was intended to be language neutral. Because it was designed for use with languages like C or Perl, DOM does not take advantage of Java's object-oriented features. That fact, in addition to the document/data distinction, also helps to account for the ways in which processing a DOM differs from processing a JDOM or dom4j structure.</p><a name="wp67803"> </a><p class="pBody">In this section, we'll examine the differences between the models underlying those standards to give help you choose the one that is most appropriate for your application.</p><a name="wp67812"> </a><h3 class="pHeading2">Documents Versus Data</h3><a name="wp67822"> </a><p class="pBody">The major point of departure between the document model used in DOM and the data model used in JDOM or dom4j lies in:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp67828"> </a><div class="pSmartList1"><li>The kind of node that exists in the hierarchy.</li></div><a name="wp67836"> </a><div class="pSmartList1"><li>The capacity for "mixed-content".</li></div></ul></div><a name="wp67853"> </a><p class="pBody">It is the difference in what constitutes a "node" in the data hierarchy that primarily accounts for the differences in programming with these two models. However, it is the capacity for mixed-content which, more than anything else, accounts for the difference in how the standards define a "node". So we'll start by examining DOM's "mixed-content model".</p><a name="wp67876"> </a><h3 class="pHeading2">Mixed Content Model</h3><a name="wp67886"> </a><p class="pBody">Recall from the discussion of <a href="IntroXML2.html#wp63997">Document-Driven Programming</a> that text and elements can be freely intermixed in a DOM hierarchy. That kind of structure is dubbed "mixed content" in the DOM model.</p><a name="wp67907"> </a><p class="pBody">Mixed content occurs frequently in documents. For example, to represent this structure:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><sentence>This is an <bold>important</bold> idea.</sentence><a name="wp67877"> </a></pre></div><a name="wp67926"> </a><p class="pBody">The hierarchy of DOM nodes would look something like this, where each line represents one node:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ELEMENT: sentence + TEXT: This is an + ELEMENT: bold + TEXT: important + TEXT: idea.<a name="wp67937"> </a></pre></div><a name="wp67998"> </a><p class="pBody">Note that the sentence element contains text, followed by a subelement, followed by additional text. It is that intermixing of text and elements that defines the "mixed-content model".</p><a name="wp68020"> </a><h4 class="pHeading3">Kinds of Nodes</h4><a name="wp68033"> </a><p class="pBody">In order to provide the capacity for mixed content, DOM nodes are inherently very simple. In the example above, for instance, the "content" of the first element (it's <em class="cEmphasis">value</em>) simply identifies the kind of node it is.</p><a name="wp68047"> </a><p class="pBody">First time users of a DOM are usually thrown by this fact. After navigating to the <sentence> node, they ask for the node's "content", and expect to get something useful. Instead, all they get is the name of the element, "sentence".</p><hr><a name="wp68061"> </a><p class="pNote">Note: The DOM Node API defines <code class="cCode">nodeValue()</code>, <code class="cCode">node.nodeType()</code>, and <code class="cCode">nodeName()</code> methods. For the first element node, <code class="cCode">nodeName()</code> returns "sentence", while <code class="cCode">nodeValue()</code> returns null. For the first text node, <code class="cCode">nodeName()</code> returns "#text", and <code class="cCode">nodeValue()</code> returns "This is an ". The important point is that the <em class="cEmphasis">value</em> of an element is not the same as its <em class="cEmphasis">content</em>.</p><hr><a name="wp79294"> </a><p class="pBody">Instead, obtaining the content you care about when processing a DOM means inspecting the list of subelements the node contains, ignoring those you aren't interested in, and processing the ones you do care about.</p><a name="wp68075"> </a><p class="pBody">For example, in the example above, what does it mean if you ask for the "text" of the sentence? Any of the following could be reasonable, depending on your application:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp68086"> </a><div class="pSmartList1"><li>This is an</li></div><a name="wp68087"> </a><div class="pSmartList1"><li>This is an idea.</li></div><a name="wp68088"> </a><div class="pSmartList1"><li>This is an important idea.</li></div><a name="wp68084"> </a><div class="pSmartList1"><li>This is an <bold>important</bold> idea.</li></div></ul></div><a name="wp68115"> </a><h3 class="pHeading2">A Simpler Model</h3><a name="wp68128"> </a><p class="pBody">With DOM, you are free to create the semantics you need. However, you are also required to do the processing necessary to implement those semantics. Standards like JDOM and dom4j, on the other hand, make it a lot easier to do simple things, because each node in the hierarchy is an object.</p><a name="wp68152"> </a><p class="pBody">Although JDOM and dom4j make allowances for elements with mixed content, they are not primarily designed for such situations. Instead, they are targeted for applications where the XML structure contains data.</p><a name="wp68171"> </a><p class="pBody">As described in <a href="IntroXML2.html#wp63987">Traditional Data Processing</a>, the elements in a data structure typically contain either text or other elements, but not both. For example, here is some XML that represents a simple address book:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><addressbook> <entry> <name>Fred</name> <email>fred@home</email> </entry>   ...</addressbook><a name="wp68272"> </a></pre></div><hr><a name="wp68279"> </a><p class="pNote">Note: For very simple XML data structures like this one, you could also use the regular expression package (java.util.regex) built into version 1.4 of the Java platform.</p><hr><a name="wp68328"> </a><p class="pBody">In JDOM and dom4j, once you navigate to an element that contains text, you invoke a method like <code class="cCode">text()</code> to get it's content. When processing a DOM, though, you would have to inspect the list of subelements to "put together" the text of the node, as you saw earlier -- even if that list only contained one item (a <code class="cCode">TEXT</code> node).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -