📄 jaxpdom7.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>Creating and Manipulating 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="JAXPDOM6.html" /> <link rel="Next" href="JAXPDOM8.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="JAXPDOM6.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="JAXPDOM8.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="wp65002"> </a><h2 class="pHeading1">Creating and Manipulating a DOM</h2><a name="wp65003"> </a><p class="pBody">By now, you understand the structure of the nodes that make up a DOM. A DOM is actually very easy to create. This section of the DOM tutorial is going to take much less work than anything you've see up to now. All the foregoing work, however, generated the basic understanding that will make this section a piece of cake. </p><a name="wp65005"> </a><h3 class="pHeading2">Obtaining a DOM from the Factory</h3><a name="wp65006"> </a><p class="pBody">In this version of the application, you're still going to create a document builder factory, but this time you're going to tell it create a new DOM instead of parsing an existing XML document. You'll keep all the existing functionality intact, however, and add the new functionality in such a way that you can "flick a switch" to get back the parsing behavior. </p><hr><a name="wp65007"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a href="../examples/jaxp/dom/samples/DomEcho05.java" target="_blank">DomEcho05.java</a></code>.</p><hr><a name="wp65009"> </a><h4 class="pHeading3">Modify the Code</h4><a name="wp65010"> </a><p class="pBody">Start by turning off the compression feature. As you work with the DOM in this section, you're going to want to see all the nodes:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho05 extends JPanel{ ...<code class="cCodeStruck"> boolean compress = true;</code><code class="cCodeBold"> boolean compress = false;</code><a name="wp65011"> </a></pre></div><a name="wp65012"> </a><p class="pBody">Next, you need to create a <code class="cCode">buildDom</code> method that creates the <code class="cCode">document</code> object. The easiest way to do that is to create the method and then copy the DOM-construction section from the <code class="cCode">main</code> method to create the <code class="cCode">buildDom</code>. The modifications shown below show you the changes you need to make to make that code suitable for the <code class="cCode">buildDom</code> method. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho05 extends JPanel{ ... public static void makeFrame() { ... }<code class="cCodeBold"> public static void buildDom() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder();</code><code class="cCodeStruck"> document = builder.parse( new File(argv[0]) );</code><code class="cCodeBold"> document = builder.newDocument();</code> <code class="cCodeStruck">} catch (SAXException sxe) { ...</code> } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); <code class="cCodeStruck">} catch (IOException ioe) { ...</code> } }<a name="wp65013"> </a></pre></div><a name="wp65014"> </a><p class="pBody">In this code, you replaced the line that does the parsing with one that creates a DOM. Then, since the code is no longer parsing an existing file, you removed exceptions which are no longer thrown: <code class="cCode">SAXException</code> and <code class="cCode">IOException</code>. </p><a name="wp65015"> </a><p class="pBody">And since you are going to be working with <code class="cCode">Element</code> objects, add the statement to import that class at the top of the program:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import org.w3c.dom.Document;import org.w3c.dom.DOMException;<code class="cCodeBold">import org.w3c.dom.Element;</code><a name="wp65016"> </a></pre></div><a name="wp65018"> </a><h4 class="pHeading3">Create Element and Text Nodes</h4><a name="wp65019"> </a><p class="pBody">Now, for your first experiment, add the <code class="cCode">Document</code> operations to create a root node and several children:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho05 extends JPanel{ ... public static void buildDom() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.newDocument(); // Create from whole cloth<code class="cCodeBold"> Element root = (Element) document.createElement("rootElement"); document.appendChild(root); root.appendChild( document.createTextNode("Some") ); root.appendChild( document.createTextNode(" ") ); root.appendChild( document.createTextNode("text") );</code> } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } }<a name="wp65020"> </a></pre></div><a name="wp65021"> </a><p class="pBody">Finally, modify the argument-list checking code at the top of the <code class="cCode">main</code> method so you invoke <code class="cCode">buildDom</code> and <code class="cCode">makeFrame</code> instead of generating an error, as shown below:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class DomEcho05 extends JPanel{ ... public static void main(String argv[]) { if (argv.length != 1) {<code class="cCodeStruck"> System.err.println("..."); System.exit(1);</code> <code class="cCodeBold"> buildDom(); makeFrame(); return;</code> } <a name="wp65022"> </a></pre></div><a name="wp65023"> </a><p class="pBody">That's all there is to it! Now, if you supply an argument the specified file is parsed and, if you don't, the experimental code that builds a DOM is executed. </p><a name="wp65025"> </a><h4 class="pHeading3">Run the App</h4><a name="wp65026"> </a><p class="pBody">Compile and run the program with no arguments produces the result shown in <a href="JAXPDOM7.html#wp65035">Figure 6-13</a>: </p><a name="wp65033"> </a><p class="pBody"></p><div align="left"><img src="images/pd-500a12.gif" height="238" width="375" alt="Element Node and Text Nodes Created" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="65035"> </a><strong><font >Figure 6-13 Element Node and Text Nodes Created</font></strong></p><a name="wp65037"> </a><h3 class="pHeading2"> Normalizing the DOM</h3><a name="wp65038"> </a><p class="pBody">In this experiment, you'll manipulate the DOM you created by normalizing it after it has been constructed.</p><hr><a name="wp65039"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a href="../examples/jaxp/dom/samples/DomEcho06.java" target="_blank">DomEcho06.java</a></code>.</p><hr><a name="wp65040"> </a><p class="pBody">Add the code highlighted below to normalize the DOM:. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void buildDom(){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { ... root.appendChild( document.createTextNode("Some") ); root.appendChild( document.createTextNode(" ") ); root.appendChild( document.createTextNode("text") );<code class="cCodeBold"> document.getDocumentElement().normalize();</code> } catch (ParserConfigurationException pce) { ...<a name="wp65041"> </a></pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -