﻿<?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-扁担领地-文章分类-开源框架</title><link>http://www.blogjava.net/weiyc-16000/category/27616.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 08 Jan 2008 01:42:13 GMT</lastBuildDate><pubDate>Tue, 08 Jan 2008 01:42:13 GMT</pubDate><ttl>60</ttl><item><title>XPath 教程</title><link>http://www.blogjava.net/weiyc-16000/articles/173343.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Mon, 07 Jan 2008 06:44:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/173343.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/173343.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/173343.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/173343.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/173343.html</trackback:ping><description><![CDATA[<div id="intro">
<p><strong>XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。</strong></p>
<p><strong>XPath 是 W3C XSLT 标准的主要元素，并且 XQuery 和 XPointer 同时被构建于 XPath 表达之上。</strong></p>
<p><strong>因此，对 XPath 的理解是很多高级 XML 应用的基础。</strong></p>
<p><a title="XPath 简介" href="http://www.w3school.com.cn/xpath/xpath_intro.asp">开始学习 XPath</a> ！</p>
</div>
<div id="referencelist">
<h2>XPath 参考手册</h2>
<p>在 W3School，我们提供完整的 XPath 2.0、XQuery 1.0 和 XSLT 2.0 的内置函数参考手册。</p>
<p><a title="XPath 函数" href="http://www.w3school.com.cn/xpath/xpath_functions.asp">XPath 函数</a></p>
</div>
<h2>内容目录</h2>
<div>
<dl>
<dt><a title="XPath简介" href="http://www.w3school.com.cn/xpath/xpath_intro.asp">XPath 简介</a>
<dd>本章讲解 XPath 的概念。
<dt><a title="XPath节点" href="http://www.w3school.com.cn/xpath/xpath_nodes.asp">XPath 节点</a>
<dd>本章详细介绍 XPath 中不同类型的节点，以及节点之间的关系。
<dt><a title="XPath语法" href="http://www.w3school.com.cn/xpath/xpath_syntax.asp">XPath 语法</a>
<dd>本章讲解 XPath 的语法。
<dt><a title="XPath Axes" href="http://www.w3school.com.cn/xpath/xpath_axes.asp">XPath 轴</a>
<dd>本章讲解 XPath axes（轴）。
<dt><a title="XPath运算符" href="http://www.w3school.com.cn/xpath/xpath_operators.asp">XPath 运算符</a>
<dd>本章列出了可以用于 XPath 表达式的运算符。
<dt><a title="XPath实例" href="http://www.w3school.com.cn/xpath/xpath_examples.asp">XPath 实例</a>
<dd>本章使用 "books.xml" 文档来演示一些 XPath 实例。
<dt><a title="XPath教程总结" href="http://www.w3school.com.cn/xpath/xpath_summary.asp">XPath 摘要</a>
<dd>本文内容包括在本教程所学知识的一个总结，以及我们向你推荐的下一步应该学习的内容。 </dd></dl></div>
<h3>XPath 参考手册</h3>
<dl>
<dt><a title="XPath函数参考手册" href="http://www.w3school.com.cn/xpath/xpath_functions.asp">XPath 函数</a>
<dd>XPath 2.0、XQuery 1.0 和 XSLT 2.0 的内置函数。</dd></dl>
<p><br />
<br />
<br />
<br />
&nbsp;</p>
<div id="intro">
<p><strong>XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。</strong></p>
</div>
<div>
<h3>在学习之前应该具备的知识：</h3>
<p>在您继续学习之前，应该对下面的知识有基本的了解：</p>
<ul>
    <li>HTML / XHTML
    <li>XML / XML 命名空间 </li>
</ul>
<p>如果您希望首先学习这些项目，请在我们的 <a title="W3School在线教程" href="http://www.w3school.com.cn/index.html">首页</a> 访问这些教程。</p>
</div>
<div>
<h3>什么是 XPath?</h3>
<ul>
    <li>XPath 使用路径表达式在 XML 文档中进行导航
    <li>XPath 包含一个标准函数库
    <li>XPath 是 XSLT 中的主要元素
    <li>XPath 是一个 W3C 标准 </li>
</ul>
</div>
<div>
<h3>XPath 路径表达式</h3>
<p>XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似。</p>
</div>
<div>
<h3>XPath 标准函数</h3>
<p>XPath 含有超过 100 个内建的函数。这些函数用于字符串值、数值，日期和时间比较、节点和 QName 处理、序列处理、逻辑值等等。</p>
</div>
<div>
<h3>XPath 在 XSLT 中使用</h3>
<p>XPath 是 XSLT 标准中的主要元素。如果没有 XPath 方面的知识，您就无法创建 XSLT 文档。</p>
<p>您可以在我们的《<a title="XSLT教程" href="http://www.w3school.com.cn/xsl/index.asp">XSLT 教程</a>》中阅读更多的内容。</p>
<p>XQuery 和 XPointer 均构建于 XPath 表达式之上。XQuery 1.0 和 XPath 2.0 共享相同的数据模型，并支持相同的函数和运算符。</p>
<p>您可以在我们的《<a title="XSLT教程" href="http://www.w3school.com.cn/xquery/index.asp">XQuery 教程</a>》中阅读更多有关 XQuery 的知识。</p>
</div>
<div>
<h3>XPath 是 W3C 标准</h3>
<p>XPath 于 1999年11月16日 成为 W3C 标准。</p>
<p>XPath 被设计供 XSLT、XPointer 以及其他 XML 解析软件使用。</p>
<p>您可以在我们的《<a title="W3C教程" href="http://www.w3school.com.cn/w3c/index.asp">W3C 教程</a>》中阅读更多有关 XPath 标准的信息。<br />
<br />
<br />
<br />
<br />
</p>
<div id="intro">
<p><strong>在 XPath 中，有七种类型的节点：元素、属性、文本、命名空间、处理指令、注释以及文档节点（或成为根节点）。</strong></p>
</div>
<div>
<h2>XPath 术语</h2>
<h3>节点（Node）</h3>
<p>在 XPath 中，有七种类型的节点：元素、属性、文本、命名空间、处理指令、注释以及文档（根）节点。XML 文档是被作为节点树来对待的。树的根被称为文档节点或者根节点。</p>
<p>请看下面这个 XML 文档：</p>
<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;bookstore&gt;
&lt;book&gt;
&lt;title lang="en"&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
<p>上面的XML文档中的节点例子：</p>
<pre>&lt;bookstore&gt; （文档节点）
&lt;author&gt;J K. Rowling&lt;/author&gt; （元素节点）
lang="en" （属性节点）
</pre>
<h3>基本值（或称原子值，Atomic value）</h3>
<p>基本值是无父或无子的节点。</p>
<p>基本值的例子：</p>
<pre>J K. Rowling
"en"</pre>
<h3>项目（Item）</h3>
<p>项目是基本值或者节点。</p>
</div>
<div>
<h2>节点关系</h2>
<h3>父（Parent）</h3>
<p>每个元素以及属性都有一个父。</p>
<p>在下面的例子中，book 元素是 title、author、year 以及 price 元素的父：</p>
<pre>&lt;book&gt;
&lt;title&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;</pre>
<h3>子（Children）</h3>
<p>元素节点可有零个、一个或多个子。</p>
<p>在下面的例子中，title、author、year 以及 price 元素都是 book 元素的子：</p>
<pre>&lt;book&gt;
&lt;title&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;</pre>
<h3>同胞（Sibling）</h3>
<p>拥有相同的父的节点</p>
<p>在下面的例子中，title、author、year 以及 price 元素都是同胞：</p>
<pre>&lt;book&gt;
&lt;title&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;</pre>
<h3>先辈（Ancestor）</h3>
<p>某节点的父、父的父，等等。</p>
<p>在下面的例子中，title 元素的先辈是 book 元素和 bookstore 元素：</p>
<pre>&lt;bookstore&gt;
&lt;book&gt;
&lt;title&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
<h3>后代（Descendant）</h3>
<p>某个节点的子，子的子，等等。</p>
<p>在下面的例子中，bookstore的后代是 book、title、author、year 以及 price 元素：</p>
<pre>&lt;bookstore&gt;
&lt;book&gt;
&lt;title&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
</div>
<br />
<br />
<br />
<br />
<div id="intro">
<p><strong>XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。</strong></p>
</div>
<div>
<h2>XML 实例文档</h2>
<p>我们将在下面的例子中使用这个 XML 文档。</p>
<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;bookstore&gt;
&lt;book&gt;
&lt;title lang="eng"&gt;Harry Potter&lt;/title&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;book&gt;
&lt;title lang="eng"&gt;Learning XML&lt;/title&gt;
&lt;price&gt;39.95&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
</div>
<div>
<h2>选取节点</h2>
<p>XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。</p>
<h3>下面列出了最有用的路径表达式：</h3>
<table class="dataintable">
    <tbody>
        <tr>
            <th>表达式</th>
            <th>描述</th>
        </tr>
        <tr>
            <td>nodename</td>
            <td>选取此节点的所有子节点</td>
        </tr>
        <tr>
            <td>/</td>
            <td>从根节点选取</td>
        </tr>
        <tr>
            <td>//</td>
            <td>从匹配选择的当前节点选择文档中的节点，而不考虑它们的位置</td>
        </tr>
        <tr>
            <td>.</td>
            <td>选取当前节点</td>
        </tr>
        <tr>
            <td>..</td>
            <td>选取当前节点的父节点</td>
        </tr>
        <tr>
            <td>@</td>
            <td>选取属性</td>
        </tr>
    </tbody>
</table>
<h3>实例</h3>
<p>在下面的表格中，我们已列出了一些路径表达式以及表达式的结果：</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>路径表达式</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>bookstore</td>
            <td>选取 bookstore 元素的所有子节点</td>
        </tr>
        <tr>
            <td>/bookstore</td>
            <td>
            <p>选取根元素 bookstore</p>
            <p>注释：假如路径起始于正斜杠( / )，则此路径始终代表到某元素的绝对路径！</p>
            </td>
        </tr>
        <tr>
            <td>bookstore/book</td>
            <td>选取所有属于 bookstore 的子元素的 book 元素。</td>
        </tr>
        <tr>
            <td>//book</td>
            <td>选取所有 book 子元素，而不管它们在文档中的位置。</td>
        </tr>
        <tr>
            <td>bookstore//book</td>
            <td>选择所有属于 bookstore 元素的后代的 book 元素，而不管它们位于 bookstore 之下的什么位置。</td>
        </tr>
        <tr>
            <td>//@lang</td>
            <td>选取所有名为 lang 的属性。</td>
        </tr>
    </tbody>
</table>
</div>
<div>
<h2>谓语（Predicates）</h2>
<p>谓语用来查找某个特定的节点或者包含某个指定的值的节点。</p>
<p>谓语被嵌在方括号中。</p>
<h3>实例</h3>
<p>在下面的表格中，我们列出了带有谓语的一些路径表达式，以及表达式的结果：</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>路径表达式</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>/bookstore/book[1]</td>
            <td>选取属于 bookstore 子元素的第一个 book 元素。</td>
        </tr>
        <tr>
            <td>/bookstore/book[last()]</td>
            <td>选取属于 bookstore 子元素的最后一个 book 元素。</td>
        </tr>
        <tr>
            <td>/bookstore/book[last()-1]</td>
            <td>选取属于 bookstore 子元素的倒数第二个 book 元素。</td>
        </tr>
        <tr>
            <td>/bookstore/book[position()&lt;3]</td>
            <td>选取最前面的两个属于 bookstore 元素的子元素的 book 元素。</td>
        </tr>
        <tr>
            <td>//title[@lang]</td>
            <td>选取所有拥有名为 lang 的属性的 title 元素。</td>
        </tr>
        <tr>
            <td>//title[@lang='eng']</td>
            <td>选取所有 title 元素，且这些元素拥有值为 eng 的 lang 属性。</td>
        </tr>
        <tr>
            <td>/bookstore/book[price&gt;35.00]</td>
            <td>选取所有 bookstore 元素的 book 元素，且其中的 price 元素的值须大于 35.00。</td>
        </tr>
        <tr>
            <td>/bookstore/book[price&gt;35.00]/title</td>
            <td>选取所有 bookstore 元素中的 book 元素的 title 元素，且其中的 price 元素的值须大于 35.00。</td>
        </tr>
    </tbody>
</table>
</div>
<div>
<h2>选取未知节点</h2>
<p>XPath 通配符可用来选取未知的 XML 元素。</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>通配符</th>
            <th>描述</th>
        </tr>
        <tr>
            <td>*</td>
            <td>匹配任何元素节点</td>
        </tr>
        <tr>
            <td>@*</td>
            <td>匹配任何属性节点</td>
        </tr>
        <tr>
            <td>node()</td>
            <td>匹配任何类型的节点</td>
        </tr>
    </tbody>
</table>
<h3>实例</h3>
<p>在下面的表格中，我们列出了一些路径表达式，以及这些表达式的结果：</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>路径表达式</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>/bookstore/*</td>
            <td>选取 bookstore 元素的所有子节点</td>
        </tr>
        <tr>
            <td>//*</td>
            <td>选取文档中的所有元素</td>
        </tr>
        <tr>
            <td>//title[@*]</td>
            <td>选取所有带有属性的 title 元素。</td>
        </tr>
    </tbody>
</table>
</div>
<div>
<h2>选取若干路径</h2>
<p>通过在路径表达式中使用&#8220;|&#8221;运算符，您可以选取若干个路径。</p>
<h3>实例</h3>
<p>在下面的表格中，我们列出了一些路径表达式，以及这些表达式的结果：</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>路径表达式</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>//book/title | //book/price</td>
            <td>选取所有 book 元素的 tilte 和 price 元素。</td>
        </tr>
        <tr>
            <td>//title | //price</td>
            <td>选取所有文档中的 title 和 price 元素。</td>
        </tr>
        <tr>
            <td>/bookstore/book/title | //price</td>
            <td>选取所有属于 bookstore 元素的 book 元素的 title 元素，以及文档中所有的 price 元素。</td>
        </tr>
    </tbody>
</table>
</div>
</div>
<br />
<br />
<br />
<h1>XPath Axes（坐标轴）</h1>
<div>
<h2>XML 实例文档</h2>
<p>我们将在下面的例子中使用此 XML 文档：</p>
<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;bookstore&gt;
&lt;book&gt;
&lt;title lang="eng"&gt;Harry Potter&lt;/title&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;book&gt;
&lt;title lang="eng"&gt;Learning XML&lt;/title&gt;
&lt;price&gt;39.95&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
</div>
<div>
<h2>XPath 轴</h2>
<p>轴可定义某个相对于当前节点的节点集。</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>轴名称</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>ancestor</td>
            <td>选取当前节点的所有先辈（父、祖父等）</td>
        </tr>
        <tr>
            <td>ancestor-or-self</td>
            <td>选取当前节点的所有先辈（父、祖父等）以及当前节点本身</td>
        </tr>
        <tr>
            <td>attribute</td>
            <td>选取当前节点的所有属性</td>
        </tr>
        <tr>
            <td>child</td>
            <td>选取当前节点的所有子元素。</td>
        </tr>
        <tr>
            <td>descendant</td>
            <td>选取当前节点的所有后代元素（子、孙等）。</td>
        </tr>
        <tr>
            <td>descendant-or-self</td>
            <td>选取当前节点的所有后代元素（子、孙等）以及当前节点本身。</td>
        </tr>
        <tr>
            <td>following</td>
            <td>选取文档中当前节点的结束标签之后的所有节点。</td>
        </tr>
        <tr>
            <td>namespace</td>
            <td>选取当前节点的所有命名空间节点</td>
        </tr>
        <tr>
            <td>parent</td>
            <td>选取当前节点的父节点。</td>
        </tr>
        <tr>
            <td>preceding</td>
            <td>选取文档中当前节点的开始标签之前的所有节点。</td>
        </tr>
        <tr>
            <td>preceding-sibling</td>
            <td>选取当前节点之前的所有同级节点。</td>
        </tr>
        <tr>
            <td>self</td>
            <td>选取当前节点。</td>
        </tr>
    </tbody>
</table>
</div>
<div>
<h2>位置路径表达式</h2>
<p>位置路径可以是绝对的，也可以是相对的。</p>
<p>绝对路径起始于正斜杠( / )，而相对路径不会这样。在两种情况中，位置路径均包括一个或多个步，每个步均被斜杠分割：</p>
<h3>绝对位置路径：</h3>
<pre>/step/step/...</pre>
<h3>相对位置路径：</h3>
<pre>step/step/...</pre>
<p>每个步均根据当前节点集之中的节点来进行计算。</p>
<h3>步（step）包括：</h3>
<dl class="define">
<dt>轴（axis）
<dd>定义所选节点与当前节点之间的树关系
<dt>节点测试（node-test）
<dd>识别某个轴内部的节点
<dt>零个或者更多谓语（predicate）
<dd>更深入地提炼所选的节点集 </dd></dl>
<h3>步的语法：</h3>
<pre>轴名称::节点测试[谓语]</pre>
<h3>实例</h3>
<table class="dataintable">
    <tbody>
        <tr>
            <th>例子</th>
            <th>结果</th>
        </tr>
        <tr>
            <td>child::book</td>
            <td>选取所有属于当前节点的子元素的 book 节点</td>
        </tr>
        <tr>
            <td>attribute::lang</td>
            <td>选取当前节点的 lang 属性</td>
        </tr>
        <tr>
            <td>child::*</td>
            <td>选取当前节点的所有子元素</td>
        </tr>
        <tr>
            <td>attribute::*</td>
            <td>选取当前节点的所有属性</td>
        </tr>
        <tr>
            <td>child::text()</td>
            <td>选取当前节点的所有文本子节点</td>
        </tr>
        <tr>
            <td>child::node()</td>
            <td>选取当前节点的所有子节点</td>
        </tr>
        <tr>
            <td>descendant::book</td>
            <td>选取当前节点的所有 book 后代</td>
        </tr>
        <tr>
            <td>ancestor::book</td>
            <td>选择当前节点的所有 book 先辈</td>
        </tr>
        <tr>
            <td>ancestor-or-self::book</td>
            <td>选取当前节点的所有book先辈以及当前节点（假如此节点是book节点的话）</td>
        </tr>
        <tr>
            <td>child::*/child::price</td>
            <td>选取当前节点的所有 price 孙。</td>
        </tr>
    </tbody>
</table>
</div>
<br />
<br />
XPath 运算符<br />
<div id="intro">
<p><strong>XPath 表达式可返回节点集、字符串、逻辑值以及数字。</strong></p>
</div>
<div>
<h2>XPath 运算符</h2>
<p>下面列出了可用在 XPath 表达式中的运算符：</p>
<table class="dataintable">
    <tbody>
        <tr>
            <th>运算符</th>
            <th>描述</th>
            <th>实例</th>
            <th>返回值</th>
        </tr>
        <tr>
            <td>|</td>
            <td>计算两个节点集</td>
            <td>//book | //cd</td>
            <td>返回所有带有 book 和 ck 元素的节点集</td>
        </tr>
        <tr>
            <td>+</td>
            <td>加法</td>
            <td>6 + 4</td>
            <td>10</td>
        </tr>
        <tr>
            <td>-</td>
            <td>减法</td>
            <td>6 - 4</td>
            <td>2</td>
        </tr>
        <tr>
            <td>*</td>
            <td>乘法</td>
            <td>6 * 4</td>
            <td>24</td>
        </tr>
        <tr>
            <td>div</td>
            <td>除法</td>
            <td>8 div 4</td>
            <td>2</td>
        </tr>
        <tr>
            <td>=</td>
            <td>等于</td>
            <td>price=9.80</td>
            <td>
            <p>如果 price 是9.80，则返回 true。</p>
            <p>如果 price 是9.90，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>!=</td>
            <td>不等于</td>
            <td>price!=9.80</td>
            <td>
            <p>如果 price 是 9.90，则返回 true。</p>
            <p>如果 price 是 9.98，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>&lt;</td>
            <td>小于</td>
            <td>price&lt;9.80</td>
            <td>
            <p>如果price是9.00，则返回true</p>
            <p>如果price是9.98，则返回fasle</p>
            </td>
        </tr>
        <tr>
            <td>&lt;=</td>
            <td>小于或等于</td>
            <td>price&lt;=9.80</td>
            <td>
            <p>如果 price 是9.00，则返回 true。</p>
            <p>如果 price 是9.90，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>&gt;</td>
            <td>大于</td>
            <td>price&gt;9.80</td>
            <td>
            <p>如果 price 是 9.90，则返回 true。</p>
            <p>如果 price 是 9.80，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>&gt;=</td>
            <td>大于或等于</td>
            <td>price&gt;=9.80</td>
            <td>
            <p>如果 price 是 9.90，则返回 true。</p>
            <p>如果 price 是 9.70，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>or</td>
            <td>或</td>
            <td>price=9.80 or price=9.70</td>
            <td>
            <p>如果 price 是 9.80，则返回 true。</p>
            <p>如果 price 是 9.50，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>and</td>
            <td>与</td>
            <td>price&gt;9.00 and price&lt;9.90</td>
            <td>
            <p>如果 price 是 9.80，则返回 true。</p>
            <p>如果 price 是 8.50，则返回 fasle。</p>
            </td>
        </tr>
        <tr>
            <td>mod</td>
            <td>计算除法的余数</td>
            <td>5 mod 2</td>
            <td>1</td>
        </tr>
    </tbody>
</table>
</div>
<br />
<br />
<div id="intro">
<p><strong>在本节，让我们通过实例来学习一些基础的 XPath 语法。</strong></p>
</div>
<div>
<h2>XML实例文档</h2>
<p>我们将在下面的例子中使用这个 XML 文档：</p>
<h3>"books.xml" :</h3>
<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;bookstore&gt;
&lt;book category="COOKING"&gt;
&lt;title lang="en"&gt;Everyday Italian&lt;/title&gt;
&lt;author&gt;Giada De Laurentiis&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;30.00&lt;/price&gt;
&lt;/book&gt;
&lt;book category="CHILDREN"&gt;
&lt;title lang="en"&gt;Harry Potter&lt;/title&gt;
&lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;year&gt;2005&lt;/year&gt;
&lt;price&gt;29.99&lt;/price&gt;
&lt;/book&gt;
&lt;book category="WEB"&gt;
&lt;title lang="en"&gt;XQuery Kick Start&lt;/title&gt;
&lt;author&gt;James McGovern&lt;/author&gt;
&lt;author&gt;Per Bothner&lt;/author&gt;
&lt;author&gt;Kurt Cagle&lt;/author&gt;
&lt;author&gt;James Linn&lt;/author&gt;
&lt;author&gt;Vaidyanathan Nagarajan&lt;/author&gt;
&lt;year&gt;2003&lt;/year&gt;
&lt;price&gt;49.99&lt;/price&gt;
&lt;/book&gt;
&lt;book category="WEB"&gt;
&lt;title lang="en"&gt;Learning XML&lt;/title&gt;
&lt;author&gt;Erik T. Ray&lt;/author&gt;
&lt;year&gt;2003&lt;/year&gt;
&lt;price&gt;39.95&lt;/price&gt;
&lt;/book&gt;
&lt;/bookstore&gt;</pre>
<p><a href="http://www.w3school.com.cn/xpath/books.xml">在您的浏览器中查看此 "books.xml" 文件</a>。</p>
</div>
<div>
<h2>节点选取</h2>
<p>我们将使用微软的 XML DOM 对象来载入 XML 文档，并使用 selectNodes() 函数从 XML 文档选取节点：</p>
<pre>set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("books.xml")
xmlDoc.selectNodes(路径表达式)</pre>
</div>
<div>
<h2>选取所有的 book 节点</h2>
<p>下面的这个例子选取了 bookstore 元素下所有的 book 节点：</p>
<pre>xmlDoc.selectNodes("/bookstore/book")</pre>
<p>假如您正在使用 IE 5 或更高的版本，可以<a href="http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_cdnodes">亲自试一试</a>。</p>
</div>
<div>
<h2>选取第一个 book 节点</h2>
<p>下面的例子仅选取 bookstore 元素下第一个 book 节点：</p>
<pre>xmlDoc.selectNodes("/bookstore/book[0]")</pre>
<p>假如您正在使用 IE 5或更高的版本，可以<a href="http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_cdnodes_first">亲自试一试</a>。</p>
<p class="note"><span>注释：</span>IE 5 和 6 会把 [0] 作为第一个节点来执行，但是根据 W3C 的标准，应该使用 [1] ！！</p>
<p class="note"><span>注释：</span>这个问题在 IE 6 SP2 中被纠正了！</p>
</div>
<div>
<h2>选取 price</h2>
<p>下面的例子从所有的 price 节点选取文本：</p>
<pre>xmlDoc.selectNodes("/bookstore/book/price/text()")</pre>
<p>假如您正在使用 IE 5 或更高的版本，可以<a href="http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_pricenodes_text">亲自试一试</a>。</p>
</div>
<div>
<h2>选取价格高于 35 的 price 价格</h2>
<p>下面的例子会选取所有价格高于 35 的 price 节点：</p>
<pre>xmlDoc.selectNodes("/bookstore/book[price&gt;35]/price")</pre>
<p>假如您正在使用 IE 5 或更高的版本，可以<a href="http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_pricenodes_35">亲自试一试</a>。</p>
</div>
<div>
<h2>选取价格高于 35 的 title 节点</h2>
<p>下面的例子会选取所有价格高于 35 的 title 节点：</p>
<pre>xmlDoc.selectNodes("/bookstore/book[price&gt;35]/title")</pre>
<p>假如您正在使用 IE 5 或更高的版本，可以<a href="http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_pricenodes_high">亲自试一试</a>。</p>
</div>
<br />
<br />
您已经学习了 XPath，下一步呢？<br />
<div>
<h2>XPath 概要</h2>
<p>本教程已经向您讲解了如何在 XML 文档中查找信息。</p>
<p>您已经学习了如何使用 XPath 通过元素和属性在 XML 文档中进行导航。</p>
<p>你也学习了如何使用 XPath 中内建的某些标准函数。</p>
<p>如需更多有关 XPath 的信息，请阅读我们的《<a title="XPath参考手册" href="http://www.w3school.com.cn/xpath/xpath_functions.asp">XPath 参考手册</a>》。</p>
</div>
<div>
<h2>您已经学习了 XPath，下一步应当学习什么呢？</h2>
<p>您下一步应该学习 XSLT、XQuery、XLink 以及 XPointer。</p>
<h3>XSLT</h3>
<p>XSLT 是针对 XML 文件的样式表语言。</p>
<p>通过 XSLT，您可以把 XML 文件转换为其他的格式，比如 XHTML。</p>
<p>如果您希望学习更多有关 XSLT 的知识，请访问我们的《<a title="XSLT教程" href="http://www.w3school.com.cn/xsl/index.asp">XSLT 教程</a>》。</p>
<h3>XQuery</h3>
<p>XQuery 和 XML 数据查询有关。</p>
<p>XQuery 被设计用来查询任何可作为 XML 形态呈现的数据，包括数据库。</p>
<p>如果您希望学习更多有关 XQuery 的知识，请访问我们的《<a title="XQuery教程" href="http://www.w3school.com.cn/xquery/index.asp">XQuery 教程</a>》。</p>
<h3>XLink 和 XPointer</h3>
<p>XML 中的链接被分为两个部分：XLink 和 XPointer。</p>
<p>XLink 和 XPointer 定义了在 XML 文档中创建超级链接的标准方法。</p>
<p>如果你希望学习更多有关 XLink 和 XPointer 的知识，请访问我们的《<a title="XLink教程" href="http://www.w3school.com.cn/xlink/index.asp">XLink 教程和 XPointer教程</a>》。</p>
</div>
<br />
<br />
<img src ="http://www.blogjava.net/weiyc-16000/aggbug/173343.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2008-01-07 14:44 <a href="http://www.blogjava.net/weiyc-16000/articles/173343.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 domj4 API 创建与修改 XML 文档</title><link>http://www.blogjava.net/weiyc-16000/articles/173333.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Mon, 07 Jan 2008 06:00:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/173333.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/173333.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/173333.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/173333.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/173333.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用&nbsp;domj4&nbsp;API&nbsp;创建与修改&nbsp;XML&nbsp;文档级别:&nbsp;初级&nbsp;Deepak&nbsp;VohraWeb&nbsp;开发人员2004&nbsp;年&nbsp;4&nbsp;月&nbsp;dom4j&nbsp;是一种解析&nbsp;XML&nbsp;文档的开放源代码&nbsp;XML&nbsp;...&nbsp;&nbsp;<a href='http://www.blogjava.net/weiyc-16000/articles/173333.html'>阅读全文</a><img src ="http://www.blogjava.net/weiyc-16000/aggbug/173333.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2008-01-07 14:00 <a href="http://www.blogjava.net/weiyc-16000/articles/173333.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Dom4j下载及使用Dom4j读写XML简介 (转)</title><link>http://www.blogjava.net/weiyc-16000/articles/173323.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Mon, 07 Jan 2008 05:33:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/173323.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/173323.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/173323.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/173323.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/173323.html</trackback:ping><description><![CDATA[<span style="color: #000000">原文地址：http://tb.blog.csdn.net/TrackBack.aspx?PostId=1965629<br />
<br />
要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在&nbsp;http://www.dom4j.org/<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />目前最新dom4j包下载地址:http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />解开后有两个包,仅操作XML文档的话把dom4j-1.6.1.jar加入工程就可以了,如果需要使用XPath的话还需要加入包jaxen-1.1-beta-7.jar.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />以下是相关操作:<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />一.Document对象相关<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />1.读取XML文件,获得document对象.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SAXReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;SAXReader();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document&nbsp;&nbsp;document&nbsp;=&nbsp;reader.read(new&nbsp;File("input.xml"));<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />2.解析XML形式的文本,得到document对象.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;text&nbsp;=&nbsp;"</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">members</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">members</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">";<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document&nbsp;document&nbsp;=&nbsp;DocumentHelper.parseText(text);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />3.主动创建document对象.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document&nbsp;document&nbsp;=&nbsp;DocumentHelper.createDocument();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;root&nbsp;=&nbsp;document.addElement("members");//&nbsp;创建根节点<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />二.节点相关<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />1.获取文档的根节点.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />Element&nbsp;rootElm&nbsp;=&nbsp;document.getRootElement();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />2.取得某节点的单个子节点.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />Element&nbsp;memberElm=root.element("member");//&nbsp;"member"是节点名<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />3.取得节点的文字<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />String&nbsp;text=memberElm.getText();也可以用:<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />String&nbsp;text=root.elementText("name");这个是取得根节点下的name字节点的文字.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />4.取得某节点下名为"member"的所有字节点并进行遍历.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />List&nbsp;nodes&nbsp;=&nbsp;rootElm.elements("member");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />for&nbsp;(Iterator&nbsp;it&nbsp;=&nbsp;nodes.iterator();&nbsp;it.hasNext();)&nbsp;{<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;Element&nbsp;elm&nbsp;=&nbsp;(Element)&nbsp;it.next();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />5.对某节点下的所有子节点进行遍历.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Iterator&nbsp;it=root.elementIterator();it.hasNext();){<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;element&nbsp;=&nbsp;(Element)&nbsp;it.next();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />6.在某节点下添加子节点.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />Element&nbsp;ageElm&nbsp;=&nbsp;newMemberElm.addElement("age");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />7.设置节点文字.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />ageElm.setText("29");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />8.删除某节点.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />parentElm.remove(childElm);//&nbsp;childElm是待删除的节点,parentElm是其父节点<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />三.属性相关.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />1.取得某节点下的某属性<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;root=document.getRootElement();&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Attribute&nbsp;attribute=root.attribute("size");//&nbsp;属性名name<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />2.取得属性的文字<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;text=attribute.getText();也可以用:<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />String&nbsp;text2=root.element("name").attributeValue("firstname");这个是取得根节点下name字节点的属性firstname的值.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />3.遍历某节点的所有属性<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;root=document.getRootElement();&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Iterator&nbsp;it=root.attributeIterator();it.hasNext();){<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Attribute&nbsp;attribute&nbsp;=&nbsp;(Attribute)&nbsp;it.next();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;text=attribute.getText();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(text);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />4.设置某节点的属性和文字.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />newMemberElm.addAttribute("name",&nbsp;"sitinspring");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />5.设置属性的文字<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Attribute&nbsp;attribute=root.attribute("name");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute.setText("sitinspring");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />6.删除某属性<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Attribute&nbsp;attribute=root.attribute("size");//&nbsp;属性名name<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root.remove(attribute);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />四.将文档写入XML文件.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />1.文档中全为英文,不设置编码,直接写入的形式.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />XMLWriter&nbsp;writer&nbsp;=&nbsp;new&nbsp;XMLWriter(new&nbsp;FileWriter("output.xml"));<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />writer.write(document);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />writer.close();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />2.文档中含有中文,设置编码格式写入的形式.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputFormat&nbsp;format&nbsp;=&nbsp;OutputFormat.createPrettyPrint();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.setEncoding("GBK");&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;指定XML编码&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XMLWriter&nbsp;writer&nbsp;=&nbsp;new&nbsp;XMLWriter(new&nbsp;FileWriter("output.xml"),format);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(document);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.close();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />五.字符串与XML的转换<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />1.将字符串转化为XML<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />String&nbsp;text&nbsp;=&nbsp;"</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">members</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">member</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">sitinspring</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">member</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">members</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">";<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />Document&nbsp;document&nbsp;=&nbsp;DocumentHelper.parseText(text);<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />2.将文档或节点的XML转化为字符串.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SAXReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;SAXReader();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document&nbsp;&nbsp;document&nbsp;=&nbsp;reader.read(new&nbsp;File("input.xml"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;root=document.getRootElement();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;docXmlText=document.asXML();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;rootXmlText=root.asXML();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;memberElm=root.element("member");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;memberXmlText=memberElm.asXML();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />六.使用XPath快速找到节点.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />读取的XML文档示例<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;?</span><span style="color: #ff00ff">xml&nbsp;version="1.0"&nbsp;encoding="UTF-8"</span><span style="color: #0000ff">?&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;</span><span style="color: #800000">projectDescription</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">MemberManagement</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">comment</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">comment</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">projects</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">PRJ1</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">PRJ2</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">PRJ3</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">PRJ4</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">project</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">projects</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">buildSpec</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">buildCommand</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">org.eclipse.jdt.core.javabuilder</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">arguments</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">arguments</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">buildCommand</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">buildSpec</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">natures</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">nature</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">org.eclipse.jdt.core.javanature</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">nature</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">natures</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">projectDescription</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" /><br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />使用XPath快速找到节点project.<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;SAXReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;SAXReader();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;try{<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document&nbsp;&nbsp;doc&nbsp;=&nbsp;reader.read(new&nbsp;File("sample.xml"));<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&nbsp;projects=doc.selectNodes("/projectDescription/projects/project");<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator&nbsp;it=projects.iterator();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(it.hasNext()){<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;elm=(Element)it.next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(elm.getText());<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception&nbsp;ex){<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />&nbsp;&nbsp;}<br />
</span>
<img src ="http://www.blogjava.net/weiyc-16000/aggbug/173323.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2008-01-07 13:33 <a href="http://www.blogjava.net/weiyc-16000/articles/173323.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Castor功能与应用五</title><link>http://www.blogjava.net/weiyc-16000/articles/163656.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Wed, 28 Nov 2007 03:48:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/163656.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/163656.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/163656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/163656.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/163656.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 索引1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JDO映射概述2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 映射定义详解2.1&nbsp;&nbsp; Mapping文件的根节点2.2&nbsp;&nbsp; 节点定义">&lt;class&gt;节点定义2.2.1...&nbsp;&nbsp;<a href='http://www.blogjava.net/weiyc-16000/articles/163656.html'>阅读全文</a><img src ="http://www.blogjava.net/weiyc-16000/aggbug/163656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2007-11-28 11:48 <a href="http://www.blogjava.net/weiyc-16000/articles/163656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Castor功能与应用参考四</title><link>http://www.blogjava.net/weiyc-16000/articles/163655.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Wed, 28 Nov 2007 03:46:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/163655.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/163655.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/163655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/163655.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/163655.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 索引1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Castor JDO 概述2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 功能特性3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 简单示例4.&nbsp;&nbsp;&n...&nbsp;&nbsp;<a href='http://www.blogjava.net/weiyc-16000/articles/163655.html'>阅读全文</a><img src ="http://www.blogjava.net/weiyc-16000/aggbug/163655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2007-11-28 11:46 <a href="http://www.blogjava.net/weiyc-16000/articles/163655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Castor功能与应用参考二</title><link>http://www.blogjava.net/weiyc-16000/articles/163652.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Wed, 28 Nov 2007 03:42:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/163652.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/163652.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/163652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/163652.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/163652.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 索引1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例需求2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Castor XML映射概念2.1&nbsp;&nbsp; 概述2.2&nbsp;&nbsp; 编组（Marshall）过程2.3&nbsp;&nbsp; 解编（U...&nbsp;&nbsp;<a href='http://www.blogjava.net/weiyc-16000/articles/163652.html'>阅读全文</a><img src ="http://www.blogjava.net/weiyc-16000/aggbug/163652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2007-11-28 11:42 <a href="http://www.blogjava.net/weiyc-16000/articles/163652.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Castor功能与应用参考一</title><link>http://www.blogjava.net/weiyc-16000/articles/163643.html</link><dc:creator>IT扁担</dc:creator><author>IT扁担</author><pubDate>Wed, 28 Nov 2007 03:17:00 GMT</pubDate><guid>http://www.blogjava.net/weiyc-16000/articles/163643.html</guid><wfw:comment>http://www.blogjava.net/weiyc-16000/comments/163643.html</wfw:comment><comments>http://www.blogjava.net/weiyc-16000/articles/163643.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weiyc-16000/comments/commentRss/163643.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weiyc-16000/services/trackbacks/163643.html</trackback:ping><description><![CDATA[<div id="div_content">转自：http://dev2dev.bea.com.cn/blog/chrisrc/200711/24_706.html
<div style="margin: 10pt 0cm 0pt"><strong><span style="font-size: 1.5em"><span style="font-size: 1.2em; color: #17365d">索引</span></span></strong></div>
<div style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt"><span>1.</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E9%A1%B9%E7%9B%AE%E7%AE%80%E4%BB%8B"><span><span>项目简介</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt"><span>2.</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_Castor%E5%8A%9F%E8%83%BD%E7%89%B9%E6%80%A7">Castor<span><span>功能特性</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt"><span>3.</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_Castor_XML">Castor XML</a></div>
<div style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt"><span>3.1</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_XML%E7%BC%96%E7%BB%84%EF%BC%88Marshalling%EF%BC%89%E6%A1%86%E6%9E%B6">XML<span><span>编组框架</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 36pt; text-indent: -36pt"><span>3.1.1</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E8%87%AA%E7%9C%81%E6%96%B9%E5%BC%8F%EF%BC%88introspection%EF%BC%89%E7%9A%84%E7%BC%96%E7%BB%84%E4%B8%8E%E8%A7%A3%E7%BC%96"><span><span>自省方式（</span>introspection<span>）的编组与解编</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 36pt; text-indent: -36pt"><span>3.1.2</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E4%BD%BF%E7%94%A8%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%E6%96%B9%E5%BC%8F%E7%9A%84%E7%BC%96%E7%BB%84%E4%B8%8E%E8%A7%A3%E7%BC%96"><span><span>使用映射文件方式的编组与解编</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 36pt; text-indent: -36pt"><span>3.1.3</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E5%B0%8F%E7%BB%93"><span><span>小结</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt"><span>3.2</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_Castor_XML%E7%9A%84%E7%BC%96%E7%BB%84/%E8%A7%A3%E7%BC%96%E6%9C%BA%E5%88%B6">Castor XML<span><span>的编组</span>/<span>解编机制</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 36pt; text-indent: -36pt"><span>3.2.1</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E7%B1%BB%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span><span>类描述符</span></span></a></div>
<div style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt"><span>3.3</span> <a href="http://writeblog.csdn.net/Editor/FCKeditor/editor/fckblank.html#_%E4%BD%BF%E7%94%A8XMLContext%E8%8E%B7%E5%8F%96%E7%BC%96%E7%BB%84%E5%99%A8%E5%92%8C%E8%A7%A3%E7%BC%96%E5%99%A8"><span><span>使用</span>XMLContext<span>获取编组器和解编器</span></span></a></div>
<div style="margin: 0cm 0cm 10pt 18pt; text-indent: -18pt"><span>3.4</span>总结</div>
<div style="margin: 10pt 0cm 0pt 21pt; text-indent: -21pt"><strong><span style="font-size: 1.5em"><span style="color: #17365d"><span><span style="font-size: 1.2em">1.</span></span> <span style="font-size: 1.2em">项目简介</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 22pt">Castor是一个开源的Java项目。其主要目标是在XML数据、Java对象和数据库关系数据之间提供一种直接的映射，使得这三种对象数据可以相互之间自由转换。Castor项目在2000年3月发布第一个0.8版本，项目几经重构设计目前最新版本为1.1.2.1。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 22pt">Castor项目主要包括XML与Java对象的映射（Castor XML），Java对象与关系数据库表映射（Castor JDO）两个关键功能。在Castor中，我们通过定义数据映射（Mapping）文件，在XML文档元素（节点、属性、文本等），Java类（类、字段属性等）和数据库表（关联表、表、字段）之间建立一一映射。通过XML Schema定义可以自动生成实体Java类定义，通过实体Java类定义可以自动生成XML元素与Java类之间的映射文件。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 22pt">目前，优秀的Java对象关系映射（O/R Mapping）框架已经很多，商业化的如Oracle的TopLink，开源的如Hibernate，iBatis，Cayenne等,这些O/R框架的实现各有特点。Sun针对对象关系映射提出了JDO规范，针对XML与Java对象之间的绑定提出了JAXB规范。Castor的优势在于其提供O/R映射功能的同时提供了XML与Java对象间数据映射功能，通过使用集成的映射定义文件， XML数据，Java对象和关系数据之间可以无缝的相互转换。</div>
<div style="margin: 10pt 0cm 0pt 21pt; text-indent: -21pt"><strong><span style="font-size: 1.5em"><span style="color: #17365d"><span><span style="font-size: 1.2em">2.</span></span> <span style="font-size: 1.2em">Castor</span><span style="font-size: 1.2em">功能特性</span></span></span></strong></div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>1.</span>Castor XML在Java对象模型与XML InfoSet之间建立相互转换和数据绑定功能；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>2.</span>提供从XML文档定义自动是生成Java类定义文件的功能（Code Generator）；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>3.</span>通过自省方式在XML文档与Java对象间建立映射（不需要提供XML Schema和XML Mapping文件）；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>4.</span>通过定义映射文件在XML文档实体与Java对象实体间建立映射；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>5.</span>Castor JDO 为Java对象与数据库关系数据之间提供一种序列化与反序列化的框架。Castor JDO不同于Sun公司提出的JDO概念，但两者具有相似的目标；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>6.</span>Castor JDO以映射文件的方式定义Java对象模型与关系数据库表模型之间的映射；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>7.</span>基于XML映射文件的方式定义三种不同数据对象模型之间的映射；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>8.</span>提供内存缓存和提交写入（write at commit）方式，减少数据库JDBC操作；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>9.</span>支持两阶段事务，对象操作回滚和数据库锁定侦测；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>10.</span>支持OQL与标准SQL之间的映射，Castor JDO对象查询采用OQL语言；</div>
<div style="margin: 0cm 0cm 0pt 42pt; text-indent: -21pt"><span>11.</span>提供基于Java实体对象类定义生成映射文件的功能；</div>
<div style="margin: 0cm 0cm 10pt 42pt; text-indent: -21pt"><span>12.</span>提供基于XML输入文档生成Schema定义文件的功能；</div>
<div style="margin: 0cm 0cm 10pt">我们将在下述章节中详细介绍Castor的上述功能，并给出操作示例。</div>
<div style="margin: 10pt 0cm 0pt 21pt; text-indent: -21pt"><strong><span style="font-size: 1.5em"><span style="color: #17365d"><span><span style="font-size: 1.2em">3.</span></span> <span style="font-size: 1.2em">Castor XML</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 21pt">Castor XML作为一个数据绑定框架，可以实现任何类似Java Bean的对象和XML文档表示之间的相互转换。通常情况下，Castor使用一组类描述符(ClassDescriptor)和字段描述符(FieldDescriptor)集来定义一个Java对象如何编组（Marshall）成XML数据或者从XML数据解编（Unmarshall）为Java对象。</div>
<div style="margin: 10pt 0cm 0pt 18pt; text-indent: -18pt"><strong><span style="font-size: 1.2em"><span style="color: #17365d"><span><span style="font-size: 1em">3.1</span></span><span style="font-size: 1em">XML</span><span style="font-size: 1em">编组（</span><span style="font-size: 1em">Marshalling</span><span style="font-size: 1em">）框架</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 18pt">XML编组框架实现Java对象与XML文档间的相互转换。在下文中，除非显式提及，否则编组框架指编组和解编两种功能。Castor XML编组框架包含在两个主要的类org.exolab.castor.xml.Marshaller和org.exolab.castor.xml.Unmarshaller中。Castor XML编组解编功能示意如下图所示：</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 18pt"><img height="238" alt="Castor XML Databinding" src="http://p.blog.csdn.net/images/p_blog_csdn_net/chrisrc/XML%20Databinding.JPG" width="428" /></div>
<div style="margin: 10pt 0cm 0pt 36pt; text-indent: -36pt"><strong><span style="font-size: 1em"><span style="color: #17365d"><span><span style="font-size: 0.9em">3.1.1</span></span> <span style="font-size: 0.9em">自省方式（</span><span style="font-size: 0.9em">introspection</span><span style="font-size: 0.9em">）的编组与解编</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">下面我们通过一个简单的示例展示XML编组框架以自省的方式在Java对象与XML文档之间转换的功能。首先，定义一个简单的Person类，该类包含两个字段：name和 birthday，分别指定义某个人的名字和出生日期。Person类定义如下：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><strong><span style="font-size: 10pt; color: #7f0055">import</span></strong> <span style="font-size: 10pt; color: black">java.util.Date;</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span><span style="font-size: 10pt; color: #3f5fbf">An</span><span style="font-size: 10pt; color: #3f5fbf">simple</span><span style="font-size: 10pt; color: #3f5fbf">person</span><span style="font-size: 10pt; color: #3f5fbf">class</span><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong><strong><span style="font-size: 10pt; color: #7f0055">class</span></strong> <span style="font-size: 10pt; color: black"><span style="background: silver">Person</span></span> <strong><span style="font-size: 10pt; color: #7f0055">implements</span></strong> <span style="font-size: 10pt; color: black">java.io.Serializable {</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span><span style="font-size: 10pt; color: #3f5fbf">The</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">private</span></strong> <span style="font-size: 10pt; color: black">String</span> <span style="font-size: 10pt; color: #0000c0">name</span> <span style="font-size: 10pt; color: black">=</span> <strong><span style="font-size: 10pt; color: #7f0055">null</span></strong><span style="font-size: 10pt; color: black">;</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span><span style="font-size: 10pt; color: #3f5fbf">The</span><span style="font-size: 10pt; color: #3f5fbf">Date</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">birth</span><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">private</span></strong> <span style="font-size: 10pt; color: black">Date</span> <span style="font-size: 10pt; color: #0000c0">birthday</span> <span style="font-size: 10pt; color: black">=</span> <strong><span style="font-size: 10pt; color: #7f0055">null</span></strong><span style="font-size: 10pt; color: black">;</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span><span style="font-size: 10pt; color: #3f5fbf">Creates</span><span style="font-size: 10pt; color: #3f5fbf">a</span><span style="font-size: 10pt; color: #3f5fbf">Person</span><span style="font-size: 10pt; color: #3f5fbf">with</span><span style="font-size: 10pt; color: #3f5fbf">no</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong> <span style="font-size: 10pt; color: black">Person() {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">super</span></strong><span style="font-size: 10pt; color: black">();</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span><span style="font-size: 10pt; color: #3f5fbf">Creates</span><span style="font-size: 10pt; color: #3f5fbf">a</span><span style="font-size: 10pt; color: #3f5fbf">Person</span><span style="font-size: 10pt; color: #3f5fbf">with</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">given</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong> <span style="font-size: 10pt; color: black">Person(String name) {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">this</span></strong><span style="font-size: 10pt; color: black">.</span><span style="font-size: 10pt; color: #0000c0">name</span> <span style="font-size: 10pt; color: black">= name;</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><strong><span style="font-size: 10pt; color: #7f9fbf">@return</span></strong><span style="font-size: 10pt; color: #3f5fbf">date</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">birth</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong> <span style="font-size: 10pt; color: black">Date getDateOfBirth() {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">return</span></strong><span style="font-size: 10pt; color: #0000c0">birthday</span><span style="font-size: 10pt; color: black">;</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><strong><span style="font-size: 10pt; color: #7f9fbf">@return</span></strong><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong> <span style="font-size: 10pt; color: black">String getName() {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">return</span></strong><span style="font-size: 10pt; color: #0000c0">name</span><span style="font-size: 10pt; color: black">;</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><span style="font-size: 10pt; color: #3f5fbf">Sets</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">date</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">birth</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><strong><span style="font-size: 10pt; color: #7f9fbf">@param</span></strong><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong><strong><span style="font-size: 10pt; color: #7f0055">void</span></strong> <span style="font-size: 10pt; color: black">setDateOfBirth(Date</span> <span style="font-size: 10pt; color: #0000c0">birthday</span><span style="font-size: 10pt; color: black">) {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">this</span></strong><span style="font-size: 10pt; color: black">.</span> <span style="font-size: 10pt; color: #0000c0">birthday</span> <span style="font-size: 10pt; color: black">=</span> <span style="font-size: 10pt; color: #0000c0">birthday</span><span style="font-size: 10pt; color: black">;</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">/**</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><span style="font-size: 10pt; color: #3f5fbf">Sets</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*</span><strong><span style="font-size: 10pt; color: #7f9fbf">@param</span></strong><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">name</span><span style="font-size: 10pt; color: #3f5fbf">of</span><span style="font-size: 10pt; color: #3f5fbf">the</span><span style="font-size: 10pt; color: #3f5fbf">person</span></div>
                        <div><span style="font-size: 10pt; color: #3f5fbf">*/</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">public</span></strong><strong><span style="font-size: 10pt; color: #7f0055">void</span></strong> <span style="font-size: 10pt; color: black">setName(String name) {</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">this</span></strong><span style="font-size: 10pt; color: black">.</span><span style="font-size: 10pt; color: #0000c0">name</span> <span style="font-size: 10pt; color: black">= name;</span></div>
                        <div><span style="font-size: 10pt; color: black">}</span></div>
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">}</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">然后，我们定义一个Person类的实例并将其编组输出为XML，示例代码如下：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">创建一个新的</span><span style="font-size: 10pt; color: #3f7f5f">Person</span><span style="font-size: 10pt; color: #3f7f5f">对象</span></div>
                        <div><span style="font-size: 10pt; color: black">Person person =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">Person(</span><span style="font-size: 10pt; color: #2a00ff">"Ryan 'Mad Dog' Madden"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <div><span style="font-size: 10pt; color: black">Calendar birthday =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">GregorianCalendar(1955, 8, 15);</span></div>
                        <div><span style="font-size: 10pt; color: black">person.setDateOfBirth(birthday.getTime());</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建编组后的</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档输出</span></div>
                        <div><span style="font-size: 10pt; background: silver; color: black">FileWriter</span> <span style="font-size: 10pt; color: black">writer =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black"><span style="background: silver">FileWriter</span>(</span><span style="font-size: 10pt; color: #2a00ff">"d:\\test.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">编组</span><span style="font-size: 10pt; color: #3f7f5f">Person</span><span style="font-size: 10pt; color: #3f7f5f">实例为</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档</span></div>
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">Marshaller.<em>marshal</em>(person, writer);</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div style="text-indent: 11pt">上述代码输出结果为：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><span style="font-size: 12pt; background: white; color: teal">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></div>
                        <div><span style="font-size: 12pt; background: white; color: blue">&lt;</span><span style="font-size: 12pt; background: white; color: maroon">person</span><span style="font-size: 12pt; background: white; color: blue">&gt;</span></div>
                        <div><span style="font-size: 12pt; background: white; color: blue">&lt;</span><span style="font-size: 12pt; background: white; color: maroon">name</span><span style="font-size: 12pt; background: white; color: blue">&gt;</span><span style="font-size: 12pt; background: white; color: black">Ryan 'Mad Dog' Madden</span><span style="font-size: 12pt; background: white; color: blue">&lt;/</span><span style="font-size: 12pt; background: white; color: maroon">name</span><span style="font-size: 12pt; background: white; color: blue">&gt;</span></div>
                        <div><span style="font-size: 12pt; background: white; color: blue">&lt;</span><span style="font-size: 12pt; background: white; color: maroon">date-of-birth</span><span style="font-size: 12pt; background: white; color: blue">&gt;</span><span style="font-size: 12pt; background: white; color: black">3855-09-15T00:00:00.000+08:00</span><span style="font-size: 12pt; background: white; color: blue">&lt;/</span><span style="font-size: 12pt; background: white; color: maroon">date-of-birth</span><span style="font-size: 12pt; background: white; color: blue">&gt;</span></div>
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="background: white; color: blue">&lt;/</span><span style="background: white; color: maroon">person</span><span style="background: white; color: blue">&gt;</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div style="text-indent: 11pt">下面的代码读取上述输出的XML文档并将解编为Java对象：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建输入</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档的</span><span style="font-size: 10pt; color: #3f7f5f">Reader</span></div>
                        <div><span style="font-size: 10pt; color: black">FileReader reader =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">FileReader(</span><span style="font-size: 10pt; color: #2a00ff">"d:\\test.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">解编</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档为</span><span style="font-size: 10pt; color: #3f7f5f">Person</span><span style="font-size: 10pt; color: #3f7f5f">对象</span></div>
                        <div style="margin: 0cm 0cm 10pt"><span style="font-size: 10pt; background: silver; color: black">person</span> <span style="font-size: 10pt; color: black">= (Person) Unmarshaller.<em>unmarshal</em>(Person.</span><strong><span style="font-size: 10pt; color: #7f0055">class</span></strong><span style="font-size: 10pt; color: black">, reader);</span></div>
                        <br />
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">System.</span><em><span style="font-size: 10pt; color: #0000c0">out</span></em><span style="font-size: 10pt; color: black">.println(</span><span style="font-size: 10pt; color: #2a00ff">"Name="</span><span style="font-size: 10pt; color: black">+<span style="background: silver">person</span>.getName());</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">输出结果为：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">Name=Ryan 'Mad Dog' Madden</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br />
<div style="margin: 10pt 0cm 0pt 36pt; text-indent: -36pt"><strong><span style="font-size: 1em"><span style="color: #17365d"><span><span style="font-size: 0.9em">3.1.2</span></span> <span style="font-size: 0.9em">使用映射文件方式的编组与解编</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">调用Marshaller和Unmarshaller的实例方法时，必须提供XML文档结构和Java类模型间的映射定义文件。关于Mapping映射文件的定义我们会在下述章节中给出详细介绍。下面是调用Marshaller和Unmarshaller的实例方法的代码示例：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">加载映射文件</span></div>
                        <div><span style="font-size: 10pt; color: black">Mapping mapping =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">Mapping();</span></div>
                        <div><span style="font-size: 10pt; color: black">mapping.loadMapping(</span><span style="font-size: 10pt; color: #2a00ff">"mapping.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">为待解编的</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档构建一个</span><span style="font-size: 10pt; color: #3f7f5f">Reader</span></div>
                        <div><span style="font-size: 10pt; color: black">reader =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">FileReader(</span><span style="font-size: 10pt; color: #2a00ff">"test.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建一个解编器</span> <span style="font-size: 10pt; color: #3f7f5f">Unmarshaller</span></div>
                        <div><span style="font-size: 10pt; color: black">Unmarshaller unmarshaller =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">Unmarshaller(Person.</span><strong><span style="font-size: 10pt; color: #7f0055">class</span></strong><span style="font-size: 10pt; color: black">);</span></div>
                        <div><span style="font-size: 10pt; color: black">unmarshaller.setMapping(mapping);</span></div>
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">解编</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档为</span><span style="font-size: 10pt; color: #3f7f5f">Person</span><span style="font-size: 10pt; color: #3f7f5f">对象</span></div>
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">Person person = (Person) unmarshaller.unmarshal(reader);</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br />
<div style="margin: 10pt 0cm 0pt 36pt; text-indent: -36pt"><strong><span style="font-size: 1em"><span style="color: #17365d"><span><span style="font-size: 0.9em">3.1.3</span></span> <span style="font-size: 0.9em">小结</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">上述示例展示了使用Marshaller和Unmarshaller类的静态方法以自省的方式实现XML文档与Java对象之间的编组和解编，以及使用Mapping映射文件通过调用Marshaller和Unmarshaller的实例方法实现XML文档与Java对象之间的编组和解编两种方式。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">前一种方式不需要构造Marshaller和Unmarshaller的实例；后一种方式需要构造Marshaller和Unmarshaller的实例，调用非静态方法完成上述功能。调用Marshaller和Unmarshaller的实例方法时，必须提供XML文档结构和Java类模型间的映射定义文件。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11.05pt"><strong><span style="color: red">注意：当使用</span><span style="color: red">Marshaller</span></strong><strong><span style="color: red">和</span><span style="color: red">Unmarshaller</span></strong><strong><span style="color: red">的静态方法时，即使指定了映射定义文件，此映射定义也会被忽略。</span></strong></div>
<div style="margin: 10pt 0cm 0pt 18pt; text-indent: -18pt"><strong><span style="font-size: 1.2em"><span style="color: #17365d"><span><span style="font-size: 1em">3.2</span></span><span style="font-size: 1em">Castor XML</span><span style="font-size: 1em">的编组</span><span style="font-size: 1em">/</span><span style="font-size: 1em">解编机制</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">Castor通过类描述符和字段描述符几乎可以编组/解编所有的Java对象。当某个Java对象类描述符不存在时，编组框架通过使用反射（Reflection）方式获取对象的结构信息，并在内存中为此对象构造类描述符和字段描述符。当实体类的描述符存在时，Castor使用这些描述符提供的Java对象模型信息用来处理编组和解编。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">编组与解编过程中，Castor要求实体类定义必须提供"public"类型的默认构造函数（不含参数）声明以及必要的"getter"和"setter"方法。</div>
<div style="margin: 10pt 0cm 0pt 36pt; text-indent: -36pt"><strong><span style="font-size: 1em"><span style="color: #17365d"><span><span style="font-size: 0.9em">3.2.1</span></span> <span style="font-size: 0.9em">类描述符</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">类描述符(<span style="font-size: 10pt; color: #00b0f0">org.exolab.castor.xml.XMLClassDescriptor</span>)使得编组框架具有足够的信息以正确的处理编组/解编过程。在Castor中，类描述符由XML编组框架和JDO框架（后文详细描述）共享。每个类描述符包含一组字段描述符(<span style="font-size: 10pt; color: #00b0f0">org.exolab.castor.xml.XMLFieldDescriptor</span>)。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">XML 类描述符具有两种构建方式：编译时构建和运行时构建。</div>
<div style="margin: 0cm 0cm 10pt 32pt; text-indent: -21pt"><span>l</span> <strong>编译时构建描述符</strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">在编译时构建类描述符，一种方法是为每一个需要编组的实体类实现<span style="font-size: 10pt; color: #00b0f0">org.exolab.castor.xml.XMLClassDescriptor</span>接口；另一种方式是使用Castor提供的源代码生成器通过XML Schema定义文档在生成Java实体类定义的同时，为Java实体类生成相应的类描述符。</div>
<div style="margin: 0cm 0cm 10pt">编译时构建类描述符的方式的主要优点是其执行速度快于运行时构建方式。</div>
<div style="margin: 0cm 0cm 10pt 32pt; text-indent: -21pt"><span>l</span> <strong>运行时构建描述符</strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">使用运行时构建类描述符时，Castor可以直接通过反射自省方式Java实体类，也可以提供映射定义文件。这两种方式可以结合使用。对于默认的自省方式，实体类必须为必要的字段提供适当的setter/getter方法；如果没有给编组的字段数据指定setter/getter方法，Castor只能访问类型为"public"的字段。如果上述条件都不满足，Castor将不处理上述字段。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">在Castor中，自省方式默认启动，不需要做任何设置。用户可以在castor.properties文件中设定必要的参数来控制自省方式的行为，如字段命名惯例，原始类型默认看作XML属性还是节点（Element）。用户可以查看castor.properties文件（在Castor发布Jar包中的"org/exolab/castor"路径下）获取详细信息。</div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">使用映射文件描述实体类时，用户需要在编组/解编操作前加载映射定义。映射定义参考(<span style="font-size: 10pt; color: #00b0f0">org.exolab.castor.mapping.Mapping</span>)。</div>
<div style="margin: 10pt 0cm 0pt 18pt; text-indent: -18pt"><strong><span style="font-size: 1.2em"><span style="color: #17365d"><span><span style="font-size: 1em">3.3</span></span><span style="font-size: 1em">使用</span><span style="font-size: 1em">XMLContext</span><span style="font-size: 1em">获取编组器和解编器</span></span></span></strong></div>
<div style="margin: 0cm 0cm 10pt; text-indent: 11pt">Castor自 V1.1.2版本开始在XML编组框架中提供了XMLContext类，用于方便高效的获取Marshaller和Unmarshaller实例。上述3.1节中编组解编Person类实例的例子使用XMLContext实现的方式如下：</div>
<table style="background: #7270c2; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td style="padding-right: 0.75pt; padding-left: 0.75pt; border-left-color: #e0dfe3; border-bottom-color: #e0dfe3; padding-bottom: 0.75pt; border-top-color: #e0dfe3; padding-top: 0.75pt; background-color: transparent; border-right-color: #e0dfe3">
            <table style="background: #ededed; width: 100%" cellspacing="1" cellpadding="0" width="100%" border="0">
                <tbody>
                    <tr>
                        <td style="padding-right: 3pt; padding-left: 3pt; border-left-color: #ffffff; border-bottom-color: #ffffff; padding-bottom: 3pt; border-top-color: #ffffff; padding-top: 3pt; background-color: transparent; border-right-color: #ffffff">
                        <div><strong><span style="font-size: 10pt; color: #7f0055">import</span></strong> <span style="font-size: 10pt; color: black">org.exolab.castor.xml.XMLContext;</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">import</span></strong> <span style="font-size: 10pt; color: black">org.exolab.castor.mapping.Mapping;</span></div>
                        <div><strong><span style="font-size: 10pt; color: #7f0055">import</span></strong> <span style="font-size: 10pt; color: black">org.exolab.castor.xml.Unmarshaller;</span></div>
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建</span><span style="font-size: 10pt; color: #3f7f5f">Mapping</span><span style="font-size: 10pt; color: #3f7f5f">实例</span></div>
                        <div><span style="font-size: 10pt; color: black">Mapping mapping = XMLContext.createMapping();</span></div>
                        <div><span style="font-size: 10pt; color: black">mapping.loadMapping(</span><span style="font-size: 10pt; color: #2a00ff">"mapping.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建一个</span><span style="font-size: 10pt; color: #3f7f5f">XMLContext</span><span style="font-size: 10pt; color: #3f7f5f">实例并设定映射</span></div>
                        <div><span style="font-size: 10pt; color: black">XMLContext context =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">XMLContext();</span></div>
                        <div><span style="font-size: 10pt; color: black">context.addMapping(mapping);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">构建一个</span><span style="font-size: 10pt; color: #3f7f5f">Unmarshaller</span></div>
                        <div><span style="font-size: 10pt; color: black">Unmarshaller unmarshaller = context.createUnmarshaller();</span></div>
                        <div><span style="font-size: 10pt; color: black">unmarshaller.setClass(Person.</span><strong><span style="font-size: 10pt; color: #7f0055">class</span></strong><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">为待解编的</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档构建一个</span><span style="font-size: 10pt; color: #3f7f5f">Reader</span></div>
                        <div><span style="font-size: 10pt; color: black">Reader reader =</span> <strong><span style="font-size: 10pt; color: #7f0055">new</span></strong> <span style="font-size: 10pt; color: black">FileReader(</span><span style="font-size: 10pt; color: #2a00ff">"test.xml"</span><span style="font-size: 10pt; color: black">);</span></div>
                        <br />
                        <div><span style="font-size: 10pt; color: #3f7f5f">//</span> <span style="font-size: 10pt; color: #3f7f5f">从</span><span style="font-size: 10pt; color: #3f7f5f">XML</span><span style="font-size: 10pt; color: #3f7f5f">文档中解编</span><span style="font-size: 10pt; color: #3f7f5f">person</span><span style="font-size: 10pt; color: #3f7f5f">对象</span></div>
                        <pre style="line-height: 12pt" xml:space="preserve"><span style="font-size: 10pt; color: black">Person person = (Person) unmarshaller.unmarshal(reader);</span>
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div style="margin: 0cm 0cm 10pt">如上述代码所示，XMLContext提供了不同的工厂方法用于获取新的Marshaller，Unmarshaller和Mapping实例。当在一个应用中需要使用不止一个Unmarshaller实例时，可以根据需要调用XMLContext的createUnmarshaller实例方法。从同一个XMLContext实例获取多个Unmarshaller实例时开销很小。需要注意的是，Unmarshaller不是线程安全的。</div>
<div style="margin: 0cm 0cm 10pt">（未完待续）</div>
</div>
<img src ="http://www.blogjava.net/weiyc-16000/aggbug/163643.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weiyc-16000/" target="_blank">IT扁担</a> 2007-11-28 11:17 <a href="http://www.blogjava.net/weiyc-16000/articles/163643.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>