﻿<?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-hbcui1984-文章分类-java</title><link>http://www.blogjava.net/hbcui1984/category/15275.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 18:42:52 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 18:42:52 GMT</pubDate><ttl>60</ttl><item><title>使用XStream进行POJO和xml之间的转换</title><link>http://www.blogjava.net/hbcui1984/articles/71865.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Mon, 25 Sep 2006 15:33:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/71865.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/71865.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/71865.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/71865.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/71865.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用XStream进行POJO和xml之间的转换		XStream是一个开源项目，利用它可以方便的进行java对象和xml之间进行转换，从而在测试，服务器设置等方面有很好的应用。		先建两个java对象						1.Adress类																				package						 xml_chb;						public						...&nbsp;&nbsp;<a href='http://www.blogjava.net/hbcui1984/articles/71865.html'>阅读全文</a><img src ="http://www.blogjava.net/hbcui1984/aggbug/71865.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-25 23:33 <a href="http://www.blogjava.net/hbcui1984/articles/71865.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> dom4j学习总结（二）</title><link>http://www.blogjava.net/hbcui1984/articles/71619.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Sun, 24 Sep 2006 15:41:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/71619.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/71619.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/71619.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/71619.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/71619.html</trackback:ping><description><![CDATA[
		<h4 align="center"> dom4j学习总结（二）</h4>
		<p>
				<strong>（一）移除节点及属性</strong>
		</p>
		<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
				<div>
						<img id="_1_48_Open_Image" onclick="this.style.display='none'; document.getElementById('_1_48_Open_Text').style.display='none'; document.getElementById('_1_48_Closed_Image').style.display='inline'; document.getElementById('_1_48_Closed_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" />
						<img id="_1_48_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_1_48_Closed_Text').style.display='none'; document.getElementById('_1_48_Open_Image').style.display='inline'; document.getElementById('_1_48_Open_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" />
						<span style="COLOR: #000000">    </span>
						<span id="_1_48_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/** */</span>
						<span id="_1_48_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000">移除节点和属性的操作<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />     * </span>
								<span style="COLOR: #808080">@throws</span>
								<span style="COLOR: #008000"> DocumentException<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />     </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img id="_104_1214_Open_Image" onclick="this.style.display='none'; document.getElementById('_104_1214_Open_Text').style.display='none'; document.getElementById('_104_1214_Closed_Image').style.display='inline'; document.getElementById('_104_1214_Closed_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" />
								<img id="_104_1214_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_104_1214_Closed_Text').style.display='none'; document.getElementById('_104_1214_Open_Image').style.display='inline'; document.getElementById('_104_1214_Open_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" />    </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> RemoveOperator() </span>
						<span style="COLOR: #0000ff">throws</span>
						<span style="COLOR: #000000"> DocumentException</span>
						<span id="_104_1214_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span>
						<span id="_104_1214_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">待生成xml的字符串</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        String str</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;book type='society'&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;&lt;/root&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">生成一个Document</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Document document </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> DocumentHelper.parseText(str);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        Element root</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">document.getRootElement();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">删除类型为society的book节点</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Element book_society</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">(Element)document.selectSingleNode(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">//book[@type='society']</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        root.remove(book_society);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">1。正确的删除了类型为society的book节点</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(document.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">删除sex节点</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Element sex</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">(Element)root.selectSingleNode(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">//sex</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">从root节点删除</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        root.remove(sex);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">2。这样是不能删除sex节点的</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(document.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">从author节点删除</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        root.element(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">author</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">).remove(sex);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">3。这样就可以正确删除sex节点</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(document.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">删除属性</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Attribute type</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">root.element(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">book</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">).attribute(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">type</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        root.element(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">book</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">).remove(type);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">4。正确删除book节点的type属性</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(document.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />    }</span>
						</span>
				</div>
		</div>
		<p>输出结果为：</p>
		<p>1。正确的删除了类型为society的book节点<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;&lt;book type="science"&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;&lt;/root&gt;<br />2。这样是不能删除sex节点的<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;&lt;book type="science"&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;&lt;/root&gt;<br />3。这样就可以正确删除sex节点<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;&lt;book type="science"&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;/author&gt;&lt;/root&gt;<br />4。正确删除book节点的type属性<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;&lt;book&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;/author&gt;&lt;/root&gt;<br /></p>
		<p>
				<strong>分析：</strong>
		</p>
		<p>第二个输出结果不能删除sex节点，我们需要看dom4j的API</p>
		<h3>remove</h3>
		<pre>public boolean <b>remove</b>(<a title="interface in org.dom4j" href="http://www.dom4j.org/apidocs/org/dom4j/Element.html">Element</a> element)</pre>
		<dl>
				<dd>Removes the given <code>Element</code> if the node is an <font color="#ff0000">immediate child</font> of this branch. If the given node is not an immediate child of this branch then the <a href="http://www.dom4j.org/apidocs/org/dom4j/Node.html#detach()"><code>Node.detach()</code></a>method should be used instead. 
<p></p></dd>
				<dd>
						<dl>
						</dl>
				</dd>
				<dd>
						<dl>
								<dt>
										<b>Parameters:</b>
								</dt>
								<dd>
										<code>element</code> - is the element to be removed 
</dd>
								<dt>
										<b>Returns:</b>
								</dt>
								<dd>true if the element was removed</dd>
						</dl>
				</dd>
		</dl>
		<p>从中我们可以看出，remove只能用在它自己的直接孩子节点上，不能用在孙子节点上，因为sex节点不是root节点的直接孩子节点，所以不能删除；而sex节点却是author节点的直接孩子节点，所以第三个输出可以删除。</p>
		<p>
				<strong>（二）将两个Document合并为一个Document</strong>
		</p>
		<p>先看一个错误的情况</p>
		<p>（1）使用add()方法添加</p>
		<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
				<div>
						<img id="_54_679_Open_Image" onclick="this.style.display='none'; document.getElementById('_54_679_Open_Text').style.display='none'; document.getElementById('_54_679_Closed_Image').style.display='inline'; document.getElementById('_54_679_Closed_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" />
						<img id="_54_679_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_54_679_Closed_Text').style.display='none'; document.getElementById('_54_679_Open_Image').style.display='inline'; document.getElementById('_54_679_Open_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" />
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> CombineDocument() </span>
						<span style="COLOR: #0000ff">throws</span>
						<span style="COLOR: #000000"> DocumentException</span>
						<span id="_54_679_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span>
						<span id="_54_679_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">待生成两个Document的字符串</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        String str_book</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;book type='society'&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;/root&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        String str_author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;root&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;&lt;/root&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">生成两个Document</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Document doc_book</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">DocumentHelper.parseText(str_book);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        Document doc_author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">DocumentHelper.parseText(str_author);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">取出doc_author的author节点，添加到doc_book的根结点</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Element author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">(Element)doc_author.selectSingleNode(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">//author</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        doc_book.getRootElement().add(author);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(doc_book.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />    }</span>
						</span>
				</div>
		</div>
		<p>调用CombineDocument函数，会出现以下错误：</p>
		<p>
				<font color="#ff0000">org.dom4j.IllegalAddException</font>: The node "<a href="mailto:org.dom4j.tree.DefaultElement@17bd6a1">org.dom4j.tree.DefaultElement@17bd6a1</a> [Element: &lt;author attributes: []/&gt;]" could not be added to the element "root" because: <font color="#ff0000">The Node already has an existing parent of "root"<br /></font> at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:1521)<br /> at org.dom4j.tree.AbstractElement.add(AbstractElement.java:1002)<br /> at xml_chb.dom4j_chb.CombineDocument(dom4j_chb.java:189)<br /> at xml_chb.dom4j_chb.main(dom4j_chb.java:199)<br />Exception in thread "main" </p>
		<p>即提示author节点已经有一个root节点了，不能再添加到另一个节点上去。</p>
		<p>（2）使用appendContent()方法</p>
		<p>即将doc_book.getRootElement().add(author);</p>
		<p>改为：doc_book.getRootElement().appendContent(author);</p>
		<p>输出结果为：</p>
		<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;<br />&lt;book type="science"&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;<br />&lt;book type="society"&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;<br /><font color="#0000ff">&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;</font><br />&lt;/root&gt;</p>
		<p>可以看出，缺少了author节点，只是把author节点的子节点添加上去了，但是由此可见，appendContent方法是有希望的。</p>
		<p>我们看一下dom4j的API:</p>
		<h3>appendContent</h3>
		<pre>public void <b>appendContent</b>(<a title="interface in org.dom4j" href="http://www.dom4j.org/apidocs/org/dom4j/Branch.html">Branch</a> branch)</pre>
		<dl>
				<dd>Appends<font color="#ff0000"> the content of the given branch</font> to this branch instance. This method behaves like the <a title="class or interface in java.util" href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html#addAll(java.util.Collection)"><code>Collection.addAll(java.util.Collection)</code></a> method. 
<p></p></dd>
				<dd>
						<dl>
						</dl>
				</dd>
				<dd>
						<dl>
								<dt>
										<b>Parameters:</b>
								</dt>
								<dd>
										<code>branch</code> - is the branch whose content will be added to me.</dd>
						</dl>
				</dd>
		</dl>
		<hr />
		<p>（3）使用正确的appendContent方法</p>
		<p>将：Element author=(Element)doc_author.selectSingleNode("//author");</p>
		<p>doc_book.getRootElement().appendContent(author);</p>
		<p>改为：doc_book.getRootElement().appendContent(doc_author.getRootElement());</p>
		<p>输出：</p>
		<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;<br />&lt;book type="science"&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;<br />&lt;book type="society"&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;<br />&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;<br />&lt;/root&gt;</p>
		<p>是正确结果</p>
		<p>（4）另一种可行的方法</p>
		<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
				<div>
						<img id="_54_752_Open_Image" onclick="this.style.display='none'; document.getElementById('_54_752_Open_Text').style.display='none'; document.getElementById('_54_752_Closed_Image').style.display='inline'; document.getElementById('_54_752_Closed_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" />
						<img id="_54_752_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_54_752_Closed_Text').style.display='none'; document.getElementById('_54_752_Open_Image').style.display='inline'; document.getElementById('_54_752_Open_Text').style.display='inline';" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" />
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> CombineDocument() </span>
						<span style="COLOR: #0000ff">throws</span>
						<span style="COLOR: #000000"> DocumentException</span>
						<span id="_54_752_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span>
						<span id="_54_752_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">待生成两个Document的字符串</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        String str_book</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;book type='society'&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />            </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;/root&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        String str_author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">&lt;root&gt;&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;sex&gt;boy&lt;/sex&gt;&lt;/author&gt;&lt;/root&gt;</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">生成两个Document</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Document doc_book</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">DocumentHelper.parseText(str_book);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        Document doc_author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">DocumentHelper.parseText(str_author);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        <br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">新生成一个Document</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        Element author</span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000">DocumentHelper.createElement(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">author</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        author.appendContent((Element)doc_author.selectSingleNode(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">//author</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">));<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">当前author尚无父节点，所以可以使用add方法添加</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />
								</span>
								<span style="COLOR: #000000">        doc_book.getRootElement().add(author);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />        System.out.println(doc_book.asXML());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" _fcksavedurl="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />    }</span>
						</span>
				</div>
		</div>
<img src ="http://www.blogjava.net/hbcui1984/aggbug/71619.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-24 23:41 <a href="http://www.blogjava.net/hbcui1984/articles/71619.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dom4j学习总结（一）</title><link>http://www.blogjava.net/hbcui1984/articles/71512.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Sat, 23 Sep 2006 18:10:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/71512.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/71512.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/71512.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/71512.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/71512.html</trackback:ping><description><![CDATA[
		<h4 align="center">dom4j学习总结（一） </h4>
		<p>
				<strong>（一）创建Document的基本操作</strong>
		</p>
		<p>
				<font color="#333399">/**<br />  * xml基本操作<br />  */</font>
				<br /> public void BaseOperation(){<br /> <font color="#99cc00"> //创建一个document<br /></font>  Document document=DocumentHelper.createDocument();<br />  <font color="#99cc00">//创建根结点<br /></font>  Element root=document.addElement("root");<br /> <font color="#99cc00"> //为根结点添加一个book节点<br /></font>  Element book1=root.addElement("book");<br />  <font color="#99cc00">//为book1添加属性type<br /></font>  book1.addAttribute("type","science");<br />  <font color="#99cc00">//为book1添加name子节点<br /></font>  Element name1=book1.addElement("Name");<br /> <font color="#99cc00"> //并设置其name为"Java"<br /></font>  name1.setText("Java");<br />  <font color="#99cc00">//为book1创建一个price节点,并设其价格为100<br /></font>  book1.addElement("price").setText("100");<br />  <br />  <font color="#99cc00">//为根结点添加第二个book节点，并设置该book节点的type属性<br /></font>  Element book2=root.addElement("book").addAttribute("type","science");<br /> <font color="#99cc00"> //为book1添加name子节点<br /></font>  Element name2=book2.addElement("Name");<br />  <font color="#99cc00">//并设置其name为"Oracle"<br /></font>  name2.setText("Oracle");<br /> <font color="#99cc00"> //为book1创建一个price节点,并设其价格为200<br /></font>  book2.addElement("price").setText("200");<br />  <br /> <font color="#99cc00"> //输出xml<br /></font>  System.out.println(document.asXML());<br /> }</p>
		<p>调用BaseOperation，输出结果为：</p>
		<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;<br /> &lt;book type="science"&gt;<br />  &lt;Name&gt;Java&lt;/Name&gt;<br />  &lt;price&gt;100&lt;/price&gt;<br /> &lt;/book&gt;<br /> &lt;book type="science"&gt;<br />  &lt;Name&gt;Oracle&lt;/Name&gt;<br />  &lt;price&gt;200&lt;/price&gt;<br /> &lt;/book&gt;<br />&lt;/root&gt;</p>
		<p>
				<strong>（二）根据一个符合Document格式的字符串来生成一个Document</strong>
		</p>
		<p>
				<font color="#333399"> /**将字符串转化为Document<br />  * @param str  输入的字符串<br />  * @return  生成的document<br />  * @throws DocumentException<br />  */</font>
				<br /> public Document parserStrtoDocument(String str) throws DocumentException{<br />  Document document=DocumentHelper.parseText(str);<br />  return document;<br /> }</p>
		<p>调用示例：</p>
		<p>String str="&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;/root&gt;";</p>
		<p>  Document document = parserStrtoDocument(str);<br />  System.out.println(document.asXML());</p>
		<p>输出结果为：</p>
		<p>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;root&gt;<br /> &lt;book type="science"&gt;<br />  &lt;Name&gt;Java&lt;/Name&gt;<br />  &lt;price&gt;100&lt;/price&gt;<br /> &lt;/book&gt;<br />&lt;/root&gt;</p>
		<p>
				<strong>（三）取得xml节点属性的基本方法</strong>
		</p>
		<p>
				<font color="#333399"> /**<br />  * 取得xml的节点和属性的值<br />  * @throws DocumentException <br />  */</font>
				<br /> public void getBaseInfofromDocument() throws DocumentException{<br />  String str="&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;&lt;/root&gt;";<br /> <font color="#99cc00"> //生成一个Document<br /></font>  Document document = DocumentHelper.parseText(str);<br />  <font color="#99cc00">//取得根结点<br /></font>  Element root=document.getRootElement();<br /> <font color="#99cc00"> //取得book节点<br /></font>  Element book=root.element("book");<br />  <font color="#99cc00">//取得book节点的type属性的值<br /></font>  String type=book.attributeValue("type");<br />  <font color="#99cc00">//取得Name节点<br /></font>  Element name=book.element("Name");<br /> <font color="#99cc00"> //取得书名<br /></font>  String bookname=name.getText();<br />  <font color="#99cc00">//取得书的价钱<br /></font>  int price=Integer.parseInt(book.element("price").getText());<br />  <br /> <font color="#808000"><font color="#99cc00"> //输出书目信息</font><br /></font>  System.out.println("书名："+bookname);<br />  System.out.println("所属类别："+type);<br />  System.out.println("价格："+price);<br /> }</p>
		<p>调用getBaseInfofromDocument，输出结果为：</p>
		<p>书名：Java<br />所属类别：science<br />价格：100</p>
		<p>
				<strong>（四）利用迭代，xpath取得节点及其属性值</strong>
		</p>
		<p>
				<font color="#333399"> /**利用迭代，xpath取得xml的节点及其属性值<br />  * @throws DocumentException<br />  */</font>
				<br /> public void getComplexInfofromDocument() throws DocumentException{</p>
		<p>
				<br />  String str="&lt;root&gt;&lt;book type='science'&gt;&lt;Name&gt;Java&lt;/Name&gt;&lt;price&gt;100&lt;/price&gt;&lt;/book&gt;"<br />   +"&lt;book type='science'&gt;&lt;Name&gt;Oracle&lt;/Name&gt;&lt;price&gt;120&lt;/price&gt;&lt;/book&gt;"<br />   +"&lt;book type='society'&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;"<br />   +"&lt;author&gt;&lt;name&gt;chb&lt;/name&gt;&lt;/author&gt;&lt;/root&gt;";<br /> <font color="#99cc00"> //生成一个Document<br /></font>  Document document = DocumentHelper.parseText(str);<br />  <br /> <font color="#99cc00"> //提取类型为"society"的书<br /></font><font color="#ff0000">  //此处需要添加支持xpath的jar包，详细见备注<br /></font>  Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']");<br />  System.out.println(society_book.asXML());<br />  <br />  <font color="#99cc00">//提取价格节点的列表<br /></font>  System.out.println("-----------价格列表-------------");<br />  List price=document.selectNodes("//price");<br />  for(int i=0;i&lt;price.size();i++){<br />   Element elem_price=(Element)price.get(i);<br />   System.out.println(elem_price.getText());<br />  }<br />  <br /><font color="#99cc00">  //循环根结点下的所有节点，若当前节点为book，则输出这本书的详细信息<br /></font>  System.out.println("-------------书目详情------------");<br />  System.out.println("书名\t\t类别\t\t价格");<br />  Element root=document.getRootElement();<br />  Iterator iterator=root.elementIterator();<br />  while(iterator.hasNext()){<br />   Element element=(Element)iterator.next();<br />   if(element.getName().equals("book")){<br />    System.out.print(element.element("Name").getText()+"\t");<br />    System.out.print(element.attributeValue("type")+"\t\t");<br />    System.out.print(element.element("price").getText()+"\n");<br />   }<br />  }</p>
		<p>  <font color="#99cc00">//查找作者姓名<br /></font>  Element author=(Element)document.selectSingleNode("//author");<br />  System.out.println("---------"+author.element("name").getText()+"----------");<br />  <font color="#99cc00">//提取作者的所有书目名称<br /></font>  Iterator iterator_book=root.elementIterator("book");  <br />  while(iterator_book.hasNext()){<br />   Element book=(Element)iterator_book.next();<br />   System.out.print(book.element("Name").getText()+"\t");<br />  }<br />  <br /> <font color="#99cc00"> //属性迭代<br /></font>  System.out.println("\n-------属性迭代--------");<br />  String str1="&lt;book type='science' name='Java' price='100'/&gt;";<br />  Document document1=DocumentHelper.parseText(str1);<br /> <font color="#99cc00"> //开始迭代<br /></font>  Iterator iterator_attribute=document1.getRootElement().attributeIterator();<br />  while(iterator_attribute.hasNext()){<br /> <font color="#99cc00">  //提取当前属性<br /></font>   Attribute attribute=(Attribute)iterator_attribute.next();<br />   System.out.println(attribute.getName()+":"+attribute.getValue());<br />  }<br /> }</p>
		<p>调用getComplexInfofromDocument，输出结果为：</p>
		<p>&lt;book type="society"&gt;&lt;Name&gt;Society security&lt;/Name&gt;&lt;price&gt;130&lt;/price&gt;&lt;/book&gt;<br />-----------价格列表-------------<br />100<br />120<br />130<br />-------------书目详情------------<br />书名  类别  价格<br />Java science  100<br />Oracle science  120<br />Society security society  130<br />---------chb----------<br />Java Oracle Society security <br />-------属性迭代--------<br />type:science<br />name:Java<br />price:100</p>
		<p>
				<font color="#ff0000">
						<strong>备注：调用该方法之前，应该先向工程中添加支持xpath的jar包，否则，会出现以下错误：</strong>
				</font>
		</p>
		<p>
				<font color="#ff0000">java.lang.NoClassDefFoundError: org/jaxen/JaxenException<br /></font> at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)<br /> at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)<br /> at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)<br /> at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82)<br /> at xml_chb.dom4j_chb.main(dom4j_chb.java:92)<br />Exception in thread "main" </p>
		<p>只需要引入jaxen包就行了，我使用的是hibernate包中的jaxen-1.1-beta-7.jar包。</p>
<img src ="http://www.blogjava.net/hbcui1984/aggbug/71512.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-24 02:10 <a href="http://www.blogjava.net/hbcui1984/articles/71512.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Hashtable对字符串进行碰撞</title><link>http://www.blogjava.net/hbcui1984/articles/71147.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Thu, 21 Sep 2006 08:25:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/71147.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/71147.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/71147.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/71147.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/71147.html</trackback:ping><description><![CDATA[1.在一些字符串数组中，常会有重复的记录，比如手机号码，我们可以通过Hashtable来对其进行过滤<br />public String[] checkArray(String[] str){<br />        Hashtable&lt;String, String&gt; hash=new Hashtable&lt;String, String&gt;();<br />        for(int i=0;i&lt;str.length;i++){<br />            if(!hash.containsKey(str[i]))<br />                hash.put(str[i], str[i]);<br />        }<br />        Enumeration enumeration=hash.keys();<br />        String[] str_new=new String[hash.size()];<br />        int i=0;<br />        while(enumeration.hasMoreElements()){<br />            str_new[i]=enumeration.nextElement().toString();<br />            i++;<br />        }<br />        return str_new;<br />    }<br />示例：<br />        String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};<br />        mobile=checkArray(mobile);<br />        for(int i=0;i&lt;mobile.length;i++)<br />            System.out.println(mobile[i]);<br />        输出结果为：<br />        13811071503<br />        13811071501<br />        13811071500<br />2.A,B均为字符串数组，找出在A中存在，而在B中不存在的字符串<br />    public String[] compareArray(String[] A,String[] B){<br />        Hashtable&lt;String, String&gt; hash=new Hashtable&lt;String, String&gt;();<br />        Hashtable&lt;String, String&gt; hash_new=new Hashtable&lt;String, String&gt;();<br />        for(int i=0;i&lt;B.length;i++)<br />            hash.put(B[i], B[i]);<br />        for(int i=0;i&lt;A.length;i++){<br />            if(!hash.containsKey(A[i]))<br />                hash_new.put(A[i], A[i]);<br />        }<br />        String[] C=new String[hash_new.size()];<br />        int i=0;<br />        Enumeration enumeration=hash_new.keys();<br />        while(enumeration.hasMoreElements()){<br />            C[i]=enumeration.nextElement().toString();<br />            i++;<br />        }<br />        return C;<br />    }<br />示例：<br />        String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};<br />        String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};<br />        String[] mobile3=compareArray(mobile1,mobile2);<br />        for(int i=0;i&lt;mobile3.length;i++)<br />            System.out.println(mobile[i]);<br />输出结果：<br />    13811071503<br />    13811071501<br />存在的问题：<br /><font color="#ff0000">每次都是倒序，可以再对程序稍加改动，变成正序。</font><br /><img src ="http://www.blogjava.net/hbcui1984/aggbug/71147.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-21 16:25 <a href="http://www.blogjava.net/hbcui1984/articles/71147.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>格式化输出数字</title><link>http://www.blogjava.net/hbcui1984/articles/70628.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Tue, 19 Sep 2006 14:29:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/70628.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/70628.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/70628.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/70628.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/70628.html</trackback:ping><description><![CDATA[
		<p>在实际工作中，常常需要设定数字的输出格式，如以百分比的形式输出，或者设定小数位数等，先稍微总结如下。<br />主要使用的类：java.text.DecimalFormat<br />1。实例化对象，可以用如下两种方法：<br />DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();<br />DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();<br />因为DecimalFormat继承自NumberFormat。<br />2。设定小数位数<br />系统默认小数位数为3，如：<br />  DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();<br />  System.out.println(df.format(12.3456789));<br />输出：12.346<br />现在可以通过如下方法把小数为设为两位：<br />  <font color="#ff0000">df.setMaximumFractionDigits(2);<br /></font>  System.out.println(df.format(12.3456789));<br />则输出为：12.35<br />3。将数字转化为百分比输出，有如下两种方法：<br />(1) <br />  <font color="#ff0000">df.applyPattern("##.##%");<br /></font>  System.out.println(df.format(12.3456789));<br />  System.out.println(df.format(1));<br />  System.out.println(df.format(0.015));<br />输出分别为：1234.57%  100%    1.5%<br />(2)<br />  df.setMaximumFractionDigits(2);<br />  System.out.println(df.format(12.3456789*100)+"%");<br />  System.out.println(df.format(1*100)+"%");<br />  System.out.println(df.format(0.015*100)+"%");<br />输出分别为：<br />1,234.57%   100%   1.5%<br />4。设置分组大小<br />   DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();<br /> <font color="#ff0000"> df1.setGroupingSize(2);<br /></font>  System.out.println(df1.format(123456789));<br />输出：1,23,45,67,89<br />还可以通过df1.setGroupingUsed(false);来禁用分组设置，如：<br />   DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();<br />  df1.setGroupingSize(2);<br /> <font color="#ff0000"> df1.setGroupingUsed(false);<br /></font>  System.out.println(df1.format(123456789));<br />输出：123456789<br />5。设置小数为必须为2位<br />  DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();<br />  <font color="#ff0000">df2.applyPattern("0.00");<br /></font>  System.out.println(df2.format(1.2));<br />输出：1.20</p>
<img src ="http://www.blogjava.net/hbcui1984/aggbug/70628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-19 22:29 <a href="http://www.blogjava.net/hbcui1984/articles/70628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java中关于时间日期操作的常用函数</title><link>http://www.blogjava.net/hbcui1984/articles/70269.html</link><dc:creator>扬州梦</dc:creator><author>扬州梦</author><pubDate>Mon, 18 Sep 2006 05:40:00 GMT</pubDate><guid>http://www.blogjava.net/hbcui1984/articles/70269.html</guid><wfw:comment>http://www.blogjava.net/hbcui1984/comments/70269.html</wfw:comment><comments>http://www.blogjava.net/hbcui1984/articles/70269.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hbcui1984/comments/commentRss/70269.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hbcui1984/services/trackbacks/70269.html</trackback:ping><description><![CDATA[1.计算某一月份的最大天数<br />Calendar time=Calendar.getInstance(); <br />time.clear(); <br />time.set(Calendar.YEAR,year); <br />time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0             <br /><font color="#ff3300">int day=time.getActualMaximum(Calendar.DAY_OF_MONTH)</font>;//本月份的天数<br />2.Calendar和Date的转化<br />(1) Calendar转化为Date<br />Calendar cal=Calendar.getInstance();<br />Date date=cal.getTime();<br />(2) Date转化为Calendar<br />Date date=new Date();<br />Calendar cal=Calendar.getInstance();<br />cal.setTime(date);<br />3.格式化输出日期时间<br />Date date=new Date();<br />SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");<br />System.out.println(df.format(date));<br />4.计算一年中的第几星期<br />(1)计算某一天是一年中的第几星期<br />Calendar cal=Calendar.getInstance();<br />cal.set(Calendar.YEAR, 2006);<br />cal.set(Calendar.MONTH, 8);<br />cal.set(Calendar.DAY_OF_MONTH, 3);<br /><font color="#ff3300">int weekno=cal.get(Calendar.WEEK_OF_YEAR);</font><br />(2)计算一年中的第几星期是几号<br />SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");<br />Calendar cal=Calendar.getInstance();<br />cal.set(Calendar.YEAR, 2006);<br />cal.set(Calendar.WEEK_OF_YEAR, 1);<br />cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);<br />System.out.println(df.format(cal.getTime()));<br />输出:<br />2006-01-02<br />5.add()和roll()的用法<br />(1)add()方法<br />SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");<br />Calendar cal=Calendar.getInstance();<br />cal.set(Calendar.YEAR, 2006);<br />cal.set(Calendar.MONTH, 8);<br />cal.set(Calendar.DAY_OF_MONTH, 3);<br />cal.add(Calendar.DATE, -4);<br />Date date=cal.getTime();<br />System.out.println(df.format(date));<br />cal.add(Calendar.DATE, 4);<br />date=cal.getTime();<br />System.out.println(df.format(date));<br />输出：<br />    2006-08-30<br />    2006-09-03<br />(2)roll方法<br />cal.set(Calendar.YEAR, 2006);<br />cal.set(Calendar.MONTH, 8);<br />cal.set(Calendar.DAY_OF_MONTH, 3);<br />cal.roll(Calendar.DATE, -4);<br />date=cal.getTime();<br />System.out.println(df.format(date));<br />cal.roll(Calendar.DATE, 4);<br />date=cal.getTime();<br />System.out.println(df.format(date));<br />输出：<br />    2006-09-29<br />    2006-09-03<br />可见，<font color="#ff3300">roll()方法在本月内循环，一般使用add()方法；</font><img src ="http://www.blogjava.net/hbcui1984/aggbug/70269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hbcui1984/" target="_blank">扬州梦</a> 2006-09-18 13:40 <a href="http://www.blogjava.net/hbcui1984/articles/70269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>