﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-实践者-随笔分类-ext</title><link>http://www.blogjava.net/ThinkingTalking/category/31751.html</link><description>Do what I can do!</description><language>zh-cn</language><lastBuildDate>Tue, 27 Apr 2010 04:36:47 GMT</lastBuildDate><pubDate>Tue, 27 Apr 2010 04:36:47 GMT</pubDate><ttl>60</ttl><item><title>DOM树的结构</title><link>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319091.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Thu, 22 Apr 2010 04:32:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319091.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/319091.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319091.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/319091.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/319091.html</trackback:ping><description><![CDATA[转载于：http://hi.baidu.com/leexper/blog/item/1c47f1112c1d3676ca80c4a9.html<br />
<br />
一．引言
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">XML
            是eXtensible Markup Language的缩写，它是一种可扩展性标识语言,
            能够让你自己创造标识，标识你所表示的内容。DOM全称是Document Object
            Model(文档对象模型)，定义了一组与平台和语言无关的接口，以便程序和脚本能够动态访问和修改XML文档内容、结构及样式。XML创建了标识，而
            DOM的作用就是告诉程序如何操作和显示这些标识。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">二．<strong style="color: black; background-color: #ffff66;">DOM树结构</strong></td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">实际上XML将数据组织成为一棵树，DOM通过解析XML文档，为XML文档在逻辑上建立一个树模型，树的节点是一个个的对象。这样通过操作这棵树和这些对象就可以完成对XML文档的操作，为处理文档的所有方面提供了一个完美的概念性框架。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">如下XML文档：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;line id=&#8221;1&#8221;&gt; the &lt;bold&gt;First&lt;/bold&gt;line&lt;/line&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">DOM的结构表示如下：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">
            <p align="center"><span style="font-size: 10.5pt;"><img src="http://www.ccw.com.cn/htm/center/prog/02_4_29_11.jpg" height="363" width="531"  alt="" /></span></p>
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">由于DOM&#8220;一切都是节点（everything-is-a-node）&#8221;，XML树的每个 Document、Element、Text 、Attr和Comment都是 DOM Node。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">由
            上面例子可知， DOM
            实质上是一些节点的集合。由于文档中可能包含有不同类型的信息，所以定义了几种不同类型的节点，如：Document、Element、Text、
            Attr 、CDATASection、ProcessingInstruction、Notation
            、EntityReference、Entity、DocumentType、DocumentFragment等。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">在创建XML文件时，如定义如下的XML文档：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;students&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;!--this is an example--&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;student&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;first-name&gt;Mike&lt;/first-name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;last-name&gt;Silver&lt;/last-name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;sex&gt;male&lt;/sex&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;class studentid="15"&gt;98211&lt;/class&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;birthday&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;day&gt;3&lt;/day&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;month&gt;3&lt;/month&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;year&gt;1979&lt;/year&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/birthday&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/student&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;student&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;first-name&gt;Ben&lt;/first-name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;last-name&gt;Silver&lt;/last-name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/name&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;sex&gt;male&lt;/sex&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;class studentid="16"&gt;98211&lt;/class&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;birthday&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;day&gt;3&lt;/day&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;month&gt;3&lt;/month&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;year&gt;1980&lt;/year&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/birthday&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/student&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">&lt;/students&gt;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">我们很自然想象到能得到如下图的结构，但是这只是数据的描述，而不是DOM树的结构。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">
            <p align="center"><span style="font-size: 10.5pt;"><img src="http://www.ccw.com.cn/htm/center/prog/02_4_39_12.jpg" height="286" width="411"  alt="" /></span></p>
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">我们可以通过以下的代码得到上面的XML文档的根结点和根结点下孩子节点的数目。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">import javax.xml.parsers.*;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">import org.w3c.dom.*;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">import java.io.File;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">import xmlwriter.XMLProperties;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">public class Xml</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">public static void main(String args[])</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{ try</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{ File file=new File("links.xml");</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">DocumentBuilder builder=factory.newDocumentBuilder();</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">Document doc=builder.parse(file);</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">doc.normalize();</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">Element theRoot = doc.getDocumentElement();</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">NodeList theList=theRoot.getChildNodes();</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">System.out.println("the students root has "+theList.getLength()+" children");</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">结果显示为：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14"><span style="font-size: 10.5pt;"><img src="http://www.ccw.com.cn/htm/center/prog/02_4_29_13.gif" height="24" width="291"  alt="" /></span></td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">但
            是从上面XML文档可以看出，students一共只有3个孩子节点（包括注释），但程序得到的结果确实有7个孩子结点。为什么呢？因为在DOM中节点
            和元素不是等价的，它的7个节点包括：两个student元素、注释及它们周围的文本节点。这些文本节点有可能是回车换行、空格或者退格，假如把这些回车
            换行、空格和退格都删除，那么DOM解释的时候就没有这些文本节点，孩子节点就真的只有3个了。下图是DOM树的精确描述：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">
            <p align="center"><span style="font-size: 10.5pt; color: black;"><img src="http://www.ccw.com.cn/htm/center/prog/02_4_39_14.jpg" height="444" width="506"  alt="" /></span></p>
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14"> <br />
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">三．常见的基本节点类型：文档、元素、属性、文本和注释</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">XML 中共有12种节点类型，其中最常见的节点类型有5种：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">元素：元素是 XML 的基本组成单元。，描述XML的基本信息。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">属性：属性节点包含关于元素节点的信息，通常包含在元素里面，描述元素的属性。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">文本：包含许多文本信息或者只是空白。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">文档：文档节点是整个文档中所有其它节点的父节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">注释：注释是对相关的信息进行描述、注释。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">四．常见的基本方法：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">通过Jaxp包来解释XML文档后，对DOM的节点对象的基本操作有:</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">appendChild(Node newChild)：在本节点上增加一个新的孩子到孩子列表的后面。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getAttributes()：得到本节点的属性列表，返回类型为NamedNodeMap。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getChildNodes()：得到本节点的孩子列表，返回类型为NodeList。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getFirstChild()、getLastChild()：得到第一个和最后一个孩子节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getNextSibling()、getPreviousSibling()：得到本节点的下一个和前一个兄弟节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getNodeName()、getNodeType()、getNodeValue()：得到本节点的名称、类型和值。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">getParentNode()：得到本节点的父亲节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">insertBefore(Node newChild, Node refChild)：在本节点的refChild孩子节点前插入一个新的节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">removeChild(Node oldChild)：删除oldChild孩子节点。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">以上列出对DOM树操作的一些常用的基本的方法，还有很多其他的方法，读者可以参考有关的规范。</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">五．递归遍历DOM树</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14"><strong style="color: black; background-color: #ffff66;">DOM树结构</strong>和二叉树很相似，元素的孩子节点集合就是该元素的分支，但是属性节点不是元素的子节点，它只是描述该元素节点的一些性质而已，属于元素节点结构内部的一部分。以下是用java编写的一段遍历DOM的程序：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">public class RecurDOM（NodeList nodelist）</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">Node node;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">int i;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">if(nodelist.getLength() == 0)</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">// 该节点没有子节点返回</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">return;</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">for(i=0;i&lt;nodelist.getLength();i++)</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">{</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">node = nodelist.item(i);</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">if(node.getNodeType() == Node.ELEMENT_NODE)</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">RecurDOM（node.getChildNodes()）；//递归调用</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">}</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" height="27" width="640">
    <tbody>
        <tr>
            <td class="a14"> <br />
            </td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">六．小结：</td>
        </tr>
    </tbody>
</table>
<table class="FCK__ShowTableBorders" width="100%">
    <tbody>
        <tr>
            <td class="a14">DOM
            是一种在浏览器内编程的模型，同时也是XML的主要接口，它与语言和平台无关，它是基于树的
            API，它把所有的数据以父子的节点层次结构装入内存构成一棵树，这些节点的类型可以是元素、文本、属性、注释或其它。它允许开发者读取、创建、删除和编
            辑 XML 数据。在这再次强调，DOM的&#8220;一切都是节点（everything-is-a-node）&#8221;。</td>
        </tr>
    </tbody>
</table>
本文的程序是用java编写的，使用Jaxp包来解释XML文档。本文的适用对象是理解 XML 基本概念并开始准备用DOM编写应用程序来操纵 XML文档 的开发者。<br />
<br />
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/319091.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2010-04-22 12:32 <a href="http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319091.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DOM的部分对象的层次结构</title><link>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319090.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Thu, 22 Apr 2010 04:31:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319090.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/319090.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319090.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/319090.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/319090.html</trackback:ping><description><![CDATA[转载于:http://hi.baidu.com/shtronger/blog/item/7dcf306174aa724deaf8f8f4.html<br />
<br />
DOM的部分对象的层次结构。
<div align="center"> </div>
<p class="normal">在该层次结构中，最顶级的对象是<strong>窗口对象（window）</strong>，它代表当前的浏览器窗口；之下是<strong>文档对象（document）、事件对象（event）、框架对象（frame）、历史对象（history）、地址对象（location）、浏览器对象（navigator）</strong>和<strong>屏幕对象（screen）</strong>；在文档对象之下包括<strong>表单（form）、图像（image）</strong>和<strong>链接（link）</strong>等多种对象；在浏览器对象navigator之下包括<strong>MIME类型对象（mimeType）</strong>和<strong>插件对象（plugin）</strong>；在表单对象之下还包括<strong>按钮（button）、复选框（checkbox）、文件选择框（fileUpload）</strong>等多种对象。</p>
<p class="normal"><font face="楷体_GB2312" size="3">注意：这个图只包括了基本的<font face="Times New Roman">DOM</font>对象，本教程中使用的对象都在其范围内。这实际上只是<font face="Times New Roman">DOM</font>中很小的一部分。<font face="Times New Roman">DOM</font>的内容很丰富，可以写一本厚厚的专著。</font></p>
<p class="normal">既然都是对象，所以每一个DOM对象都拥有属性（描述Web页面或文档的变量）和方法（允许操作部分Web页面）。复杂的对象拥有的属性和方法相当多，如document对象。<br />
注意：这些对象形成的是嵌套关系，而不是继承关系。所谓嵌套，就是高层对象包含低层对象，低层对象的属性和方法，和高层对象是相互独立的，它们之间不
是父对象和子对象的关系。如同大盒子里可以放各种东西和小盒子、小盒子里还可以放各种东西和更小的盒子一样。所谓继承，就是子对象拥有父对象的一切属性和
方法，同时可能还有自己的属性和方法，类似于父子关系（当然还不完全一样）。<br />
但在DOM中，也经常把下层对象说成是&#8220;子对象&#8221;，把上层对象说成是&#8220;父对象&#8221;。这种说法是不严格的，因为它们之间并不是继承关系。但是，这种说法已经约定俗称，因此我们后面也这样说。</p>
<p class="normal"><font face="楷体_GB2312" size="3">需要注意的是：<font face="Times New Roman">DOM</font>不是<font face="Times New Roman">JavaScript</font>的一部分，而是内置在浏览器中的一个对象模型，所有语言都可以利用和操纵它，如<font face="Times New Roman">VBScript、VB.NET、C#</font>和<font face="Times New Roman">Java</font>等。<font face="Times New Roman">JavaScript</font>只是最早使用<font face="Times New Roman">DOM</font>的语言而已，当然也是人们用得最多的网页编程语言。因此，<font face="Times New Roman">DOM</font>可以看成应用程序接口（<font face="Times New Roman">API</font>）。<font face="Times New Roman">DOM</font>的基本内容已经标准化了，形成了<font face="Times New Roman">DOM 1</font>和<font face="Times New Roman">DOM 2</font>规范，并被越来越多的浏览器所支持。<font face="Times New Roman">DOM</font>的高级部分仍在不断发展中。</font></p>
<p class="normal"><strong>对象引用方法</strong></p>
<p class="normal">既然是嵌套关系，当引用一个低层对象时，应该先写父对象名，后面接着是子对象名，对象名之间用小圆点&#8220;.&#8221;隔开。例
如，document是最顶层对象window的下层对象，如果要表示文档对象document的一个图像子对象image9，因此引用image9对象
可以这样写：</p>
<pre>
<p class="code">&nbsp;&nbsp;     window.document.image9</p>
</pre>
<p class="normal"><font face="楷体_GB2312" size="3">其实，这种引用方法在上一章的例子中已经多次使用。又如，如果要引用<font face="Times New Roman">document</font>对象，应使用<font face="Times New Roman">window.document</font>；如果要引用<font face="Times New Roman">location</font>对象，应使用<font face="Times New Roman">window.location</font>。由于<font face="Times New Roman">window</font>对象是默认的最上层对象，因此引用它的子对象时，可以不使用<font face="Times New Roman">window.</font>，也就是说，可以直接用<font face="Times New Roman">document</font>引用<font face="Times New Roman">document</font>对象，用<font face="Times New Roman">location</font>引用<font face="Times New Roman">location</font>对象。这就是为什么我们用了那么多&#8220;<font face="Times New Roman">document.write</font>&#8221;而不必用&#8220;<font face="Times New Roman">window.document.write</font>&#8221;的原因。</font></p>
<p class="normal">当引用较低层次的对象时，一般有两种方式 ---- 使用对象索引或使用对象名（对象的name属性），也可以使用对象的id属性。例如，如果要引用文档中的第一个表单对象，则可以用：</p>
<pre>
<p class="code">&nbsp;&nbsp;     document.forms[0]</p>
</pre>
<p style="text-indent: 0px;" class="normal">来引用，因为一个document中的各个form对象会形成一个对象数组，form[0]当然是这个数组的第一个form对象即表单对象。如果该表单的name属性为form1（或者id属性为form1），则可以用：</p>
<pre>
<p class="code">&nbsp;&nbsp;     document.forms["form1"]</p>
</pre>
<p style="text-indent: 0px;" class="normal">或直接用：</p>
<pre>
<p class="code">&nbsp;&nbsp;     document.form1</p>
</pre>
<p style="text-indent: 0px;" class="normal">来引用该表单。同样，如果在名称为form1的表单中包括一个名称为myText的文本框，则可以用：</p>
<pre>
<p class="code">&nbsp;&nbsp;     document.form1.myText</p>
</pre>
<p style="text-indent: 0px;" class="normal">来引用该文本框对象。</p>
<p class="normal"><font face="楷体_GB2312" size="3">后面我们将会看到：最好使用<font face="Times New Roman">id</font>属性。要解释原因，需要了解更多的知识，我们在本章后半部分会明白的。但在<a target="_blank" href="http://www.itsway.net/html/htmlindex.aspx">HTML教程</a>中，主要介绍的是传统的<font face="Times New Roman">name</font>属性，因此现在我们两个属性都会使用的。</font></p>
<p class="normal">对应于不同的对象，通常还有一些特殊的引用方法。例如，如果要引用表单对象中包含的对象，可以使用elements数组；如果要引用文档对象中包含的某个标记符对象（例如&lt;p&gt;对象），可以使用document对象的all属性，等等</p>
<br />
<br />
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/319090.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2010-04-22 12:31 <a href="http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319090.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DOM的结构</title><link>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319089.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Thu, 22 Apr 2010 04:30:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319089.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/319089.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319089.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/319089.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/319089.html</trackback:ping><description><![CDATA[转载于：http://hi.baidu.com/3305128/blog/item/1b683af39ea818c30a46e0b5.html<br />
<br />
<span class="top11">
<p>在DOM 中，我们将代表XML 文件的程序设计对象，称为节点（nodes）。当Internet
Explorer 5 处理被链接的XML 文件并储存于DOM 中时，它会为XML
文件的每一个基本组件建立一个节点。这些基本组件包括了元素、属性，与处理指令DOM 会使用不同形态的节点来代表不同形态的XML
组件。例如，元素是储存在Element 节点中，而属性则是储存在Attribute 节点中。表格9-1 列出了这些节点类型最重要的部分。</p>
<table width="100%">
    <tbody>
        <tr>
            <td width="17%">节点形态</td>
            <td width="28%">节点对象所代表的XML文件组件</td>
            <td width="28%">节点名称（nodeName 对象属性）</td>
            <td width="27%">节点的值（nodeValue 对象属性）</td>
        </tr>
        <tr>
            <td>文件（Document）</td>
            <td>文件阶层中的根节点（代表整个XML 文件）</td>
            <td>#document</td>
            <td>Null</td>
        </tr>
        <tr>
            <td>元素（Element）</td>
            <td>元素</td>
            <td>元素形态名称(例如，BOOK)</td>
            <td>null（包含在元素中的（Element）任何字符数据，是位在一个或多个子文字节点中）</td>
        </tr>
        <tr>
            <td>文字（Text）</td>
            <td>属于由这个节点的父节点所代表的元素，属性及实体的文字。</td>
            <td>#text</td>
            <td>父XML 组件的文字</td>
        </tr>
        <tr>
            <td>属性（Attribute）</td>
            <td>属性 (以及其它的名值对，像处理指令中的名字与值)</td>
            <td>属性名称 (如Binding)</td>
            <td>属性值（例如hardcover）</td>
        </tr>
        <tr>
            <td>处理指令 （Processing Instruction ）</td>
            <td>处理指令（XML 宣告或自订的处理指令）</td>
            <td>处理指令的目标（例如xml）</td>
            <td>除了目标之外整个处理指令的内容（例如，Version "1.0"）</td>
        </tr>
        <tr>
            <td>批注（Comment）</td>
            <td>批注</td>
            <td>#comment</td>
            <td>在批注符号中的文字</td>
        </tr>
        <tr>
            <td>CDATA 区段（CDATASection）</td>
            <td>CDATA 区段</td>
            <td>#cdata-section</td>
            <td>CDATA 区段中的内容</td>
        </tr>
        <tr>
            <td>文件类型（Document <br />
            <br />
            Type）</td>
            <td>文件形态宣告</td>
            <td>出现在DOCTYPE 宣告中的根元素的名字(例如INVENTORY)</td>
            <td>Null</td>
        </tr>
        <tr>
            <td>实体（Entity）</td>
            <td>DTD 中的实体宣告</td>
            <td>实体名称（例如image）</td>
            <td>null （实体值是位在子文字节点中）</td>
        </tr>
        <tr>
            <td>标签</td>
            <td>DTD 中的标签宣告</td>
            <td>标签名称（例如BMP）</td>
            <td>&gt; null （卷标的系统literal（Notation）是位在名为SYSTEM 的 子Attribute node中）</td>
        </tr>
    </tbody>
</table>
<p>表格9-1 用来表示不同XML 文件组件的基本节点形态。这些类型的每一个节点都是一个程序设计对象，提供了存取相关组件的属性与方法。</p>
<p>你可以从节点中的nodeName 属性获得每个节点的名称（详列于表格9-1
中的第三栏）。这个名称是以字符#起始，代表那些未在文件中命名的XML 组件节点的标准名称。（例如，在XML文件中的批注并未命名。因此，DOM
将使用标准名称#comment。）其它节点的名称则是由指定到XML 文件中相对应组件的名称衍生而来。（例如，代表形态BOOK
元素的元素节点也可以命名为BOOK。</p>
<p>你可以从节点的nodeValue 属性取得每个节点的节点值（列于表格9-1
中最后一栏）。如果XML组件拥有一个相关的值（例如，属性），该值将会被储存于节点的节点值中。如果XML
组件并没有节点值（例如，元素），则DOM 将会把节点值设成null。在本章稍后，你将学到更多有关列于表格9.1 中各种节点类型的相关知识。</p>
<p>DOM 会将XML 文件的节点建构成树状的阶层结构，反映出XML 文件本身的阶层结构。DOM将会建立一个单一文件节点来表示整个XML
文件，并将其视为阶层结构的根节点。注意，XML元素的逻辑阶层结构，包含了整个XML 文件，结构中的根节点，只是DOM 节点的阶层结构的一个分枝。</p>
<p>参考 列表9-1 中的XML 文件范例。（你可以在随书光盘的 Dom.xml 档案中找到列表9-1。）这份文件包含了XML 宣告、批注，以及包含子元素与属性的节点元素。</p>
<p>下图展示了节点的阶层组织，该节点是DOM 所建立用来呈现这份范例文件。对于范例文件中的每个组件，该图指出了用来表现每个组件的节点形态（例如，文件、批注和元素）以及节点的名称（下图括号中所示－例如#document、#comment 与INVENTORY）。</p>
<p> </p>
<p forimg="1" align="center"><img class="blogimg" small="0" src="http://hiphotos.baidu.com/3305128/pic/item/7656df24bb202d234d088d26.jpg" border="0"  alt="" /></p>
<p align="left">Inventory Dom.xml <br />
<br />
&lt;?xml version="1.0"?&gt; <br />
<br />
&lt;!--File Name:Inventory Dom.xml --&gt; <br />
<br />
&lt;INVENTORY&gt; <br />
<br />
&lt;BOOK Binding="mass market paperback"&gt; <br />
<br />
&lt;TITLE&gt; The Adventures of Huckleberry Finn <br />
<br />
&lt;AUTHOR Born="1835"&gt; Mark Twain <br />
<br />
&lt;PAGES&gt; 298 <br />
<br />
&lt;PRICE&gt; $5.49 <br />
<br />
&lt;/BOOK&gt; <br />
<br />
&lt;BOOK Binding="trade paperback"&gt; <br />
<br />
&lt;TITLE&gt; The Marble Faun <br />
<br />
&lt;AUTHOR Born="1804"&gt; Nathaniel Hawthorne <br />
<br />
&lt;PAGES&gt; 473 <br />
<br />
&lt;PRICE&gt; $10.95 <br />
<br />
&lt;/BOOK&gt; <br />
<br />
&lt;BOOK Binding="hardcover"&gt; <br />
<br />
&lt;TITLE&gt; Moby-Dick <br />
<br />
&lt;AUTHOR Born="1819"&gt; Herman Melville <br />
<br />
&lt;PAGES&gt; 724 <br />
<br />
&lt;PRICE&gt; $9.95 <br />
<br />
&lt;/BOOK&gt; <br />
<br />
&lt;INVENTORY&gt;</p>
<p>每个节点，就像可程序化的对象，提供了属性和方法，让你可以存取、显示、管理，和取得对应到XML 组件上的信息。例如，nodeName 和nodeValue 属性（表9-1 所示）提供了元素的名称及内含值。</p>
<p>所有形态的节点共同分享一组公共的属性与方法。这些属性与方法一般是设计来偕同节点一起运作。表格9-2 列出了一些比较有用的共同属性。在本章稍后你将获得有关这些属性的更多信息及范例。</p>
<p align="left">
<table width="100%">
    <tbody>
        <tr align="center">
            <td>属性</td>
            <td>描述</td>
            <td>范例</td>
        </tr>
        <tr>
            <td>attributes</td>
            <td>该节点的所有非属性的子节点的NamedNodeMap 集合</td>
            <td>AttributeNode =Element.attributes.getNamedItem ("Binding");</td>
        </tr>
        <tr>
            <td>childNodes</td>
            <td>该节点的所有非属性的子节点的NodeList 集合</td>
            <td>FirstNode =Element.childNodes (0);</td>
        </tr>
        <tr>
            <td>dataType</td>
            <td>该节点的数据类型（只适用于某些类型Attribute 节点）</td>
            <td>AttributeType =Attribute.dataType;</td>
        </tr>
        <tr>
            <td>firstChild</td>
            <td>该节点的第一个非属性的子节点</td>
            <td>FirstChildNode =Element.firstChild;</td>
        </tr>
        <tr>
            <td>lastChild</td>
            <td>该节点的最后一个非属性的子节点</td>
            <td>LastChildNode =Element.lastChild;</td>
        </tr>
        <tr>
            <td>nextSibling</td>
            <td>与本节点位于同一层级的后继前一节点</td>
            <td>NextElement =Element.nextSibling;</td>
        </tr>
        <tr>
            <td>nodeName</td>
            <td>节点的名称</td>
            <td>ElementName =Element.nodeName;</td>
        </tr>
        <tr>
            <td>nodeType</td>
            <td>表示该节点类型的数值码</td>
            <td>NodeTypeCode =Node.nodeType;</td>
        </tr>
        <tr>
            <td>nodeTypeString</td>
            <td>包含该节点类型的字符串，以小写字母撰写（例如，"element"或"attribute"）</td>
            <td>NodeTypeString=Node.nodeTypeString;</td>
        </tr>
        <tr>
            <td>nodeValue</td>
            <td>该节点的值（如果不含值则为null）</td>
            <td>AttributeValue =Attribute.nodeValue;</td>
        </tr>
        <tr>
            <td>ownerDocument</td>
            <td>包含本节点的文件的根Document 节点</td>
            <td>Document =Node.ownerDocument;</td>
        </tr>
        <tr>
            <td>parentNode</td>
            <td>该节点的父节点（并不适用于Attribute 节点）</td>
            <td>ParentElement =Element.parentNode;</td>
        </tr>
        <tr>
            <td>previousSibling</td>
            <td>与本节点位于同一层级的先前节点</td>
            <td>PreviousElement =Element.previousSibling;</td>
        </tr>
        <tr>
            <td>text</td>
            <td>该节点与其后裔节点的全部文字内容</td>
            <td>AllCharacterData =Element.text;</td>
        </tr>
        <tr>
            <td>xml</td>
            <td>该节点与其后裔节点的全部XML 内容</td>
            <td>XMLContent =Element.xml;</td>
        </tr>
    </tbody>
</table>
</p>
<p>提示</p>
<p>有关属于不同形态节点对象的所有属性、方法，与事件的详细叙述，请参阅由MSDN 所提供的信息，网址为
http://msdn.microsoft.com/xml/reference/scriptref/xmldom_Objects.asp
。注意在该网页中，前置字符串XMLDOM 作为表格9-1 中每个节点名称的开端－例如，XMLDOMDocument、XMLDOMElement
与XMLDOMText.（这些是每个节点形态的程序接口的名称。） 也注意共同的节点属性和方法被列于XMLDOMNode 的后面。</p>
<p>除了共同的属性和方法外，每种类型的节点都提供了附加的属性和方法，来处理该节点类型所代表的特殊XML
组件。例如，文件节点（Document node）提供了parseError
属性，内含任何处理文件时所产生错误的相关信息。只有文件节点拥有这种属性。在本章稍后，你将看到一份描述某些节点类型的有用节点特殊属性和方法的表格。</p>
<p>提示 <br />
<br />
如果某个属性不适用于特定节点，则该属性将包含null 值。例如，如果某节点代表一个不包含任何属性的XML 文件（如Document
或Comment node）时，其attributes
属性将被设为null。如果某节点代表某个不含有任何数据类型（只有某些属性拥有数据类型）的XML 组件时，该节点的dataType
属性将被设为null。如果节点并没有任何非属性的子节点，其firstChild 属性将为null。而如果节点为不含值的类型（如Document
或Element node），其nodeValue 属性将被设为null。</p>
<p>注意，表格9-2 中，每个节点拥有一组属性，让你可以浏览节点的阶层架构，来从目前节点存取其它节点的资料。例如，在列表9-1
中的文件，如变量Document 包含了文件的根Document节点，后续的程序代码将会显示最接近文件起始部分批注的内容，而DOM
会将批注内容储存在Document 节点的第二个子节点中。</p>
<p>alert (Document.childNodes(1).nodeValue);</p>
<p>这行程序代码将会显示一个消息框，内含文字「File Name: In-ventory Dom.xml」。透过XML data
island 取得DSO 的XMLDocument 成员对象，来存取根Document 节点。此Document 节点被视为存取XML
文件的网关（gateway）。 <br />
<br />
你使用该节点来存取其它的节点。</p>
</span><br />
<br />
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/319089.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2010-04-22 12:30 <a href="http://www.blogjava.net/ThinkingTalking/archive/2010/04/22/319089.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ext中文乱码解决方法</title><link>http://www.blogjava.net/ThinkingTalking/archive/2008/05/26/202898.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Mon, 26 May 2008 05:50:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2008/05/26/202898.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/202898.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2008/05/26/202898.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/202898.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/202898.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;只有注册用户登录后才能阅读该文。<a href='http://www.blogjava.net/ThinkingTalking/archive/2008/05/26/202898.html'>阅读全文</a><img src ="http://www.blogjava.net/ThinkingTalking/aggbug/202898.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2008-05-26 13:50 <a href="http://www.blogjava.net/ThinkingTalking/archive/2008/05/26/202898.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>