⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxpdom5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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>Examining the Structure of a 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="JAXPDOM4.html" />    <link rel="Next" href="JAXPDOM6.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="JAXPDOM4.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="JAXPDOM6.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="wp64575"> </a><h2 class="pHeading1">Examining the Structure of a DOM</h2><a name="wp64576"> </a><p class="pBody">In this section, you'll use the GUI-fied <code class="cCode">DomEcho</code> application you created in the last section to visually examine a DOM. You'll see what nodes make up the DOM, and how they are arranged. With the understanding you acquire, you'll be well prepared to construct and modify Document Object Model structures in the future. </p><a name="wp64578"> </a><h3 class="pHeading2">Displaying A Simple Tree</h3><a name="wp64579"> </a><p class="pBody">We'll start out by displaying a simple file, so you get an idea of basic DOM structure. Then we'll look at the structure that results when you include some of the more advanced XML elements.</p><hr><a name="wp64580"> </a><p class="pNote">Note: The code used to create the figures in this section is in <code class="cCode"><a  href="../examples/jaxp/dom/samples/DomEcho02.java" target="_blank">DomEcho02.java</a></code>. The file displayed is <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample01.xml" target="_blank">slideSample01.xml</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample01-xml.html" target="_blank">slideSample01-xml.html</a></code>.)</p><hr><a name="wp64584"> </a><p class="pBody"><a  href="JAXPDOM5.html#wp64590">Figure 6-1</a> shows the tree you see when you run the DomEcho program on the first XML file you created in the DOM tutorial. </p><a name="wp64588"> </a><p class="pBody"></p><div align="left"><img src="images/pd-201a.gif" height="200" width="436" alt="Document, Comment, and Element Nodes Displayed" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64590"> </a><strong><font >Figure 6-1    Document, Comment, and Element Nodes Displayed</font></strong></p><a name="wp64591"> </a><p class="pBody">Recall that the first bit of text displayed for each node is the element <code class="cCode">type</code>. After that comes the element <code class="cCode">name</code>, if any, and then the element <code class="cCode">value</code>. This view shows three element types: <code class="cCode">Document</code>, <code class="cCode">Comment</code>, and <code class="cCode">Element</code>. There is only one node of <code class="cCode">Document</code> type for the whole tree, the root node. The <code class="cCode">Comment</code> node displays the <code class="cCode">value</code> attribute, while the <code class="cCode">Element</code> node displays the element <code class="cCode">name</code>, &quot;slideshow&quot;.</p><a name="wp64595"> </a><p class="pBody">Compare <a  href="JAXPDOM5.html#wp64590">Figure 6-1</a> with the code in the <code class="cCode">AdapterNode</code>'s <code class="cCode">toString</code> method to see whether the name or value is being displayed for a particular node. If you need to make it more clear, modify the program to indicate which property is being displayed (for example, with N: <span style="font-style: italic">name</span>, V: <span style="font-style: italic">value</span>).</p><a name="wp64599"> </a><p class="pBody">Expanding the slideshow element brings up the display shown in <a  href="JAXPDOM5.html#wp64605">Figure 6-2</a>.    </p><a name="wp64603"> </a><p class="pBody"></p><div align="left"><img src="images/pd-201b2.gif" height="290" width="447" alt="Element Node Expanded, No Attribute Nodes Showing" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64605"> </a><strong><font >Figure 6-2    Element Node Expanded, No Attribute Nodes Showing</font></strong></p><a name="wp64606"> </a><p class="pBody">Here, you can see the <code class="cCode">Text</code> nodes and <code class="cCode">Comment</code> nodes that are interspersed between Slide elements. The empty <code class="cCode">Text</code> nodes exist because there is no DTD to tell the parser that no text exists. (Generally, the vast majority of nodes in a DOM tree will be <code class="cCode">Element</code> and <code class="cCode">Text</code> nodes.)</p><a name="wp64607"> </a><p class="pDefinitionTerm">Important!</p><a name="wp64608"> </a><p class="pBody">Text nodes exist <span style="font-style: italic">under</span> element nodes in a DOM, and data is <span style="font-style: italic">always</span> stored in text nodes. Perhaps the most common error in DOM processing is to navigate to an element node and expect it to contain the data that is stored in that element. Not so! Even the simplest element node has a text node under it. For example, given <code class="cCode">&lt;size&gt;12&lt;/size&gt;</code>, there is an element node (<code class="cCode">size</code>), <span style="font-style: italic">and a text node under it</span> which contains the actual data (<code class="cCode">12</code>).</p><a name="wp64609"> </a><p class="pBody">Notably absent from this picture are the <code class="cCode">Attribute</code> nodes. An inspection of the table in <code class="cCode">org.w3c.dom.Node</code> shows that there is indeed an Attribute node type. But they are not included as children in the DOM hierarchy. They are instead obtained via the Node interface <code class="cCode">getAttributes</code> method.</p><hr><a name="wp64610"> </a><p class="pNote">Note: The display of the text nodes is the reason for including the lines below in the AdapterNode's <code class="cCode">toString</code> method. If your remove them, you'll see the funny characters (typically square blocks) that are generated by the newline characters that are in the text.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">String t = domNode.getNodeValue().trim();int x = t.indexOf(&quot;);if (x &gt;= 0) t = t.substring(0, x);s += t; <a name="wp64611"> </a></pre></div><hr><a name="wp64613"> </a><h3 class="pHeading2">Displaying a More Complex Tree</h3><a name="wp64614"> </a><p class="pBody">Here, you'll display the example XML file you created at the end of the SAX tutorial, to see how entity references, processing instructions, and CDATA sections appear in the DOM.</p><hr><a name="wp64615"> </a><p class="pNote">Note: The file displayed in this section is <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample10.xml" target="_blank">slideSample10.xml</a></code>. The <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample10.xml" target="_blank">slideSample10.xml</a></code> file references <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideshow3.dtd" target="_blank">slideshow3.dtd</a></code> which, in turn, references <code class="cCode"><a  href="../examples/jaxp/dom/samples/copyright.xml" target="_blank">copyright.xml</a></code> and a (very simplistic) <code class="cCode"><a  href="../examples/jaxp/dom/samples/xhtml.dtd" target="_blank">xhtml.dtd</a></code><code class="cCode">.</code> (The browsable versions are <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideSample10-xml.html" target="_blank">slideSample10-xml.html</a></code>, <code class="cCode"><a  href="../examples/jaxp/dom/samples/slideshow3-dtd.html" target="_blank">slideshow3-dtd.html</a></code><code class="cCode">, </code><code class="cCode"><a  href="../examples/jaxp/dom/samples/copyright-xml.html" target="_blank">copyright-xml.html</a></code>, and <code class="cCode"><a  href="../examples/jaxp/dom/samples/xhtml-dtd.html" target="_blank">xhtml-dtd.html</a></code>.) </p><hr><a name="wp64619"> </a><p class="pBody"><a  href="JAXPDOM5.html#wp64625">Figure 6-3</a> shows the result of running the <code class="cCode">DomEcho</code> application on <code class="cCode">slideSample10.xml</code>, which includes a <code class="cCode">DOCTYPE</code> entry that identifies the document's DTD.</p><a name="wp64623"> </a><p class="pBody"></p><div align="left"><img src="images/pd-210c3.gif" height="289" width="448" alt="DocType Node Displayed" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64625"> </a><strong><font >Figure 6-3    DocType Node Displayed</font></strong></p><a name="wp66841"> </a><p class="pBody">The <code class="cCode">DocType</code> interface is actually an extension of <code class="cCode">w3c.org.dom.Node</code>. It defines a <code class="cCode">getEntities</code> method that you would use to obtain <code class="cCode">Entity</code> nodes--the nodes that define entities like the <code class="cCode">product</code> entity, which has the value &quot;WonderWidgets&quot;. Like <code class="cCode">Attribute</code> nodes, <code class="cCode">Entity</code> nodes do not appear as children of DOM nodes.</p><a name="wp64630"> </a><p class="pBody">When you expand the <code class="cCode">slideshow</code> node, you get the display shown in <a  href="JAXPDOM5.html#wp64636">Figure 6-4</a>.</p><a name="wp64634"> </a><p class="pBody"></p><div align="left"><img src="images/pd-210d4.gif" height="292" width="447" alt="Processing Instruction Node Displayed" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="64636"> </a><strong><font >Figure 6-4    Processing Instruction Node Displayed</font></strong></p><a name="wp64637"> </a><p class="pBody">Here, the processing instruction node is highlighted, showing that those nodes do appear in the tree. The <code class="cCode">name</code> property contains the target-specification, which identifies the application that the instruction is directed to. The <code class="cCode">value</code> property contains the text of the instruction. </p><a name="wp64638"> </a><p class="pBody">Note that empty text nodes are also shown here, even though the DTD specifies that a <code class="cCode">slideshow</code> can contain <code class="cCode">slide</code> elements only, never text. Logically, then, you might think that these nodes would not appear. (When this file was run through the SAX parser, those elements generated <code class="cCode">ignorableWhitespace</code> events, rather than <code class="cCode">character</code> events.) </p><a name="wp64640"> </a><p class="pBody">Moving down to the second <code class="cCode">slide</code> element and opening the <code class="cCode">item</code> element under it brings up the display shown in <a  href="JAXPDOM5.html#wp73280">Figure 6-5</a>.</p><a name="wp73279"> </a><p class="pBody"></p><div align="left"><img src="images/pd-210e114.gif" height="283" width="447" alt="AXP 1.2 DOM Item Text Returned from an Entity Reference" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="73280"> </a><strong><font >Figure 6-5    JAXP 1.2 DOM -- Item Text Returned from an Entity Reference</font></strong></p><a name="wp73287"> </a><p class="pBody">Here, you can see that a text node containing the copyright text was inserted into the DOM, rather than the entity reference which pointed to it.</p><a name="wp75727"> </a><p class="pBody">For most applications, the insertion of the text is exactly what you want. That way, when you're looking for the text under a node, you don't have to worry about an entity references it might contain.</p><a name="wp75737"> </a><p class="pBody">For other applications, though, you may need the ability to reconstruct the original XML. For example, an editor application would need to save the result of user modifications without throwing away entity references in the process.</p><a name="wp75870"> </a><p class="pBody">Various <code class="cCode">DocumentBuilderFactory</code> APIs give you control over the kind of DOM structure that is created. For example, add the highlighted line below to produce the DOM structure shown in <a  href="JAXPDOM5.html#wp64649">Figure 6-6</a>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void main(String argv[]){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -