??xml version="1.0" encoding="utf-8" standalone="yes"?>999久久久久久久久6666,亚洲精品视频在线,欧美精品久久一区二区三区http://www.aygfsteel.com/hbcui1984/category/15275.htmlzh-cnTue, 27 Feb 2007 18:42:52 GMTTue, 27 Feb 2007 18:42:52 GMT60使用XStreamq行POJO和xml之间的{?/title><link>http://www.aygfsteel.com/hbcui1984/articles/71865.html</link><dc:creator>扬州?/dc:creator><author>扬州?/author><pubDate>Mon, 25 Sep 2006 15:33:00 GMT</pubDate><guid>http://www.aygfsteel.com/hbcui1984/articles/71865.html</guid><wfw:comment>http://www.aygfsteel.com/hbcui1984/comments/71865.html</wfw:comment><comments>http://www.aygfsteel.com/hbcui1984/articles/71865.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/hbcui1984/comments/commentRss/71865.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/hbcui1984/services/trackbacks/71865.html</trackback:ping><description><![CDATA[     摘要: 使用XStreamq行POJO和xml之间的{? XStream是一个开源项目,利用它可以方便的q行java对象和xml之间q行转换Q从而在试Q服务器讄{方面有很好的应用? 先徏两个java对象 1.Adressc? package  xml_chb; public ...  <a href='http://www.aygfsteel.com/hbcui1984/articles/71865.html'>阅读全文</a><img src ="http://www.aygfsteel.com/hbcui1984/aggbug/71865.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/hbcui1984/" target="_blank">扬州?/a> 2006-09-25 23:33 <a href="http://www.aygfsteel.com/hbcui1984/articles/71865.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> dom4j学习ȝQ二Q?/title><link>http://www.aygfsteel.com/hbcui1984/articles/71619.html</link><dc:creator>扬州?/dc:creator><author>扬州?/author><pubDate>Sun, 24 Sep 2006 15:41:00 GMT</pubDate><guid>http://www.aygfsteel.com/hbcui1984/articles/71619.html</guid><wfw:comment>http://www.aygfsteel.com/hbcui1984/comments/71619.html</wfw:comment><comments>http://www.aygfsteel.com/hbcui1984/articles/71619.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/hbcui1984/comments/commentRss/71619.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/hbcui1984/services/trackbacks/71619.html</trackback:ping><description><![CDATA[ <h4 align="center"> dom4j学习ȝQ二Q?/h4> <p> <strong>Q一Q移除节点及属?/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">U除节点和属性的操作<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的字W串</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"><root><book type='science'><Name>Java</Name><price>100</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"><book type='society'><Name>Society security</Name><price>130</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"><author><name>chb</name><sex>boy</sex></author></root></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">删除cd为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。这h不能删除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>输出l果为:</p> <p>1。正的删除了类型ؓsociety的book节点<br /><?xml version="1.0" encoding="UTF-8"?><br /><root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root><br />2。这h不能删除sex节点?br /><?xml version="1.0" encoding="UTF-8"?><br /><root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root><br />3。这样就可以正确删除sex节点<br /><?xml version="1.0" encoding="UTF-8"?><br /><root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root><br />4。正删除book节点的type属?br /><?xml version="1.0" encoding="UTF-8"?><br /><root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root><br /></p> <p> <strong>分析Q?/strong> </p> <p>W二个输出结果不能删除sex节点Q我们需要看dom4j的API</p> <h3>remove</h3> <pre>public boolean <b>remove</b>(<a title="interface in org.dom4j" >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 ><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>从中我们可以看出Qremove只能用在它自q直接孩子节点上,不能用在孙子节点上,因ؓsex节点不是root节点的直接孩子节点,所以不能删除;而sex节点却是author节点的直接孩子节点,所以第三个输出可以删除?/p> <p> <strong>Q二Q将两个Document合ƈZ个Document</strong> </p> <p>先看一个错误的情况</p> <p>Q?Q用add()Ҏd</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的字W串</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"><root><book type='science'><Name>Java</Name><price>100</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"><book type='society'><Name>Society security</Name><price>130</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"></root></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"><root><author><name>chb</name><sex>boy</sex></author></root></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节点Q添加到doc_book的根l点</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函数Q会出现以下错误Q?/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: <author attributes: []/>]" 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>xCauthor节点已经有一个root节点了,不能再添加到另一个节点上厅R?/p> <p>Q?Q用appendContent()Ҏ</p> <p>卛_doc_book.getRootElement().add(author);</p> <p>改ؓQdoc_book.getRootElement().appendContent(author);</p> <p>输出l果为:</p> <p><?xml version="1.0" encoding="UTF-8"?><br /><root><br /><book type="science"><Name>Java</Name><price>100</price></book><br /><book type="society"><Name>Society security</Name><price>130</price></book><br /><font color="#0000ff"><name>chb</name><sex>boy</sex></font><br /></root></p> <p>可以看出Q缺了author节点Q只是把author节点的子节点d上去了,但是由此可见QappendContentҎ是有希望的?/p> <p>我们看一下dom4j的API:</p> <h3>appendContent</h3> <pre>public void <b>appendContent</b>(<a title="interface in org.dom4j" >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" ><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>Q?Q用正的appendContentҎ</p> <p>:Element author=(Element)doc_author.selectSingleNode("http://author");</p> <p>doc_book.getRootElement().appendContent(author);</p> <p>改ؓQdoc_book.getRootElement().appendContent(doc_author.getRootElement());</p> <p>输出Q?/p> <p><?xml version="1.0" encoding="UTF-8"?><br /><root><br /><book type="science"><Name>Java</Name><price>100</price></book><br /><book type="society"><Name>Society security</Name><price>130</price></book><br /><author><name>chb</name><sex>boy</sex></author><br /></root></p> <p>是正结?/p> <p>Q?Q另一U可行的Ҏ</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的字W串</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"><root><book type='science'><Name>Java</Name><price>100</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"><book type='society'><Name>Society security</Name><price>130</price></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" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"></root></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"><root><author><name>chb</name><sex>boy</sex></author></root></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Ҏd</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.aygfsteel.com/hbcui1984/aggbug/71619.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/hbcui1984/" target="_blank">扬州?/a> 2006-09-24 23:41 <a href="http://www.aygfsteel.com/hbcui1984/articles/71619.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dom4j学习ȝQ一Q?/title><link>http://www.aygfsteel.com/hbcui1984/articles/71512.html</link><dc:creator>扬州?/dc:creator><author>扬州?/author><pubDate>Sat, 23 Sep 2006 18:10:00 GMT</pubDate><guid>http://www.aygfsteel.com/hbcui1984/articles/71512.html</guid><wfw:comment>http://www.aygfsteel.com/hbcui1984/comments/71512.html</wfw:comment><comments>http://www.aygfsteel.com/hbcui1984/articles/71512.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/hbcui1984/comments/commentRss/71512.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/hbcui1984/services/trackbacks/71512.html</trackback:ping><description><![CDATA[ <h4 align="center">dom4j学习ȝQ一Q?/h4> <p> <strong>Q一Q创建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"> //为根l点d一个book节点<br /></font>  Element book1=root.addElement("book");<br />  <font color="#99cc00">//为book1d属性type<br /></font>  book1.addAttribute("type","science");<br />  <font color="#99cc00">//为book1dname子节?br /></font>  Element name1=book1.addElement("Name");<br /> <font color="#99cc00"> //q设|其name?Java"<br /></font>  name1.setText("Java");<br />  <font color="#99cc00">//为book1创徏一个price节点,q设其hgؓ100<br /></font>  book1.addElement("price").setText("100");<br />  <br />  <font color="#99cc00">//为根l点dW二个book节点Qƈ讄该book节点的type属?br /></font>  Element book2=root.addElement("book").addAttribute("type","science");<br /> <font color="#99cc00"> //为book1dname子节?br /></font>  Element name2=book2.addElement("Name");<br />  <font color="#99cc00">//q设|其name?Oracle"<br /></font>  name2.setText("Oracle");<br /> <font color="#99cc00"> //为book1创徏一个price节点,q设其hgؓ200<br /></font>  book2.addElement("price").setText("200");<br />  <br /> <font color="#99cc00"> //输出xml<br /></font>  System.out.println(document.asXML());<br /> }</p> <p>调用BaseOperationQ输出结果ؓQ?/p> <p><?xml version="1.0" encoding="UTF-8"?><br /><root><br /> <book type="science"><br />  <Name>Java</Name><br />  <price>100</price><br /> </book><br /> <book type="science"><br />  <Name>Oracle</Name><br />  <price>200</price><br /> </book><br /></root></p> <p> <strong>Q二Q根据一个符合Document格式的字W串来生成一个Document</strong> </p> <p> <font color="#333399"> /**字W串转化为Document<br />  * @param str  输入的字W串<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>调用CZQ?/p> <p>String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";</p> <p>  Document document = parserStrtoDocument(str);<br />  System.out.println(document.asXML());</p> <p>输出l果为:</p> <p><?xml version="1.0" encoding="UTF-8"?><br /><root><br /> <book type="science"><br />  <Name>Java</Name><br />  <price>100</price><br /> </book><br /></root></p> <p> <strong>Q三Q取得xml节点属性的基本Ҏ</strong> </p> <p> <font color="#333399"> /**<br />  * 取得xml的节点和属性的?br />  * @throws DocumentException <br />  */</font> <br /> public void getBaseInfofromDocument() throws DocumentException{<br />  String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";<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("书名Q?+bookname);<br />  System.out.println("所属类别:"+type);<br />  System.out.println("hQ?+price);<br /> }</p> <p>调用getBaseInfofromDocumentQ输出结果ؓQ?/p> <p>书名QJava<br />所属类别:science<br />hQ?00</p> <p> <strong>Q四Q利用P代,xpath取得节点及其属性?/strong> </p> <p> <font color="#333399"> /**利用q代Qxpath取得xml的节点及其属性?br />  * @throws DocumentException<br />  */</font> <br /> public void getComplexInfofromDocument() throws DocumentException{</p> <p> <br />  String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"<br />   +"<book type='science'><Name>Oracle</Name><price>120</price></book>"<br />   +"<book type='society'><Name>Society security</Name><price>130</price></book>"<br />   +"<author><name>chb</name></author></root>";<br /> <font color="#99cc00"> //生成一个Document<br /></font>  Document document = DocumentHelper.parseText(str);<br />  <br /> <font color="#99cc00"> //提取cd?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">//提取h节点的列?br /></font>  System.out.println("-----------h列表-------------");<br />  List price=document.selectNodes("http://price");<br />  for(int i=0;i<price.size();i++){<br />   Element elem_price=(Element)price.get(i);<br />   System.out.println(elem_price.getText());<br />  }<br />  <br /><font color="#99cc00">  //循环根结点下的所有节点,若当前节点ؓbookQ则输出q本书的详细信息<br /></font>  System.out.println("-------------书目详情------------");<br />  System.out.println("书名\t\tcd\t\th");<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("http://author");<br />  System.out.println("---------"+author.element("name").getText()+"----------");<br />  <font color="#99cc00">//提取作者的所有书目名U?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"> //属性P?br /></font>  System.out.println("\n-------属性P?-------");<br />  String str1="<book type='science' name='Java' price='100'/>";<br />  Document document1=DocumentHelper.parseText(str1);<br /> <font color="#99cc00"> //开始P?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>调用getComplexInfofromDocumentQ输出结果ؓQ?/p> <p><book type="society"><Name>Society security</Name><price>130</price></book><br />-----------h列表-------------<br />100<br />120<br />130<br />-------------书目详情------------<br />书名  cd  h<br />Java science  100<br />Oracle science  120<br />Society security society  130<br />---------chb----------<br />Java Oracle Society security <br />-------属性P?-------<br />type:science<br />name:Java<br />price:100</p> <p> <font color="#ff0000"> <strong>备注Q调用该Ҏ之前Q应该先向工E中d支持xpath的jar包,否则Q会出现以下错误Q?/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包就行了Q我使用的是hibernate包中的jaxen-1.1-beta-7.jar包?/p> <img src ="http://www.aygfsteel.com/hbcui1984/aggbug/71512.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/hbcui1984/" target="_blank">扬州?/a> 2006-09-24 02:10 <a href="http://www.aygfsteel.com/hbcui1984/articles/71512.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Hashtable对字W串q行撞http://www.aygfsteel.com/hbcui1984/articles/71147.html扬州?/dc:creator>扬州?/author>Thu, 21 Sep 2006 08:25:00 GMThttp://www.aygfsteel.com/hbcui1984/articles/71147.htmlhttp://www.aygfsteel.com/hbcui1984/comments/71147.htmlhttp://www.aygfsteel.com/hbcui1984/articles/71147.html#Feedback0http://www.aygfsteel.com/hbcui1984/comments/commentRss/71147.htmlhttp://www.aygfsteel.com/hbcui1984/services/trackbacks/71147.htmlpublic String[] checkArray(String[] str){
        Hashtable<String, String> hash=new Hashtable<String, String>();
        for(int i=0;i<str.length;i++){
            if(!hash.containsKey(str[i]))
                hash.put(str[i], str[i]);
        }
        Enumeration enumeration=hash.keys();
        String[] str_new=new String[hash.size()];
        int i=0;
        while(enumeration.hasMoreElements()){
            str_new[i]=enumeration.nextElement().toString();
            i++;
        }
        return str_new;
    }
CZQ?br />        String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
        mobile=checkArray(mobile);
        for(int i=0;i<mobile.length;i++)
            System.out.println(mobile[i]);
       输出l果为:
        13811071503
        13811071501
        13811071500
2.A,B均ؓ字符串数l,扑և在A中存在,而在B中不存在的字W串
    public String[] compareArray(String[] A,String[] B){
        Hashtable<String, String> hash=new Hashtable<String, String>();
        Hashtable<String, String> hash_new=new Hashtable<String, String>();
        for(int i=0;i<B.length;i++)
            hash.put(B[i], B[i]);
        for(int i=0;i<A.length;i++){
            if(!hash.containsKey(A[i]))
                hash_new.put(A[i], A[i]);
        }
        String[] C=new String[hash_new.size()];
        int i=0;
        Enumeration enumeration=hash_new.keys();
        while(enumeration.hasMoreElements()){
            C[i]=enumeration.nextElement().toString();
            i++;
        }
        return C;
    }
CZQ?br />        String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
        String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
        String[] mobile3=compareArray(mobile1,mobile2);
        for(int i=0;i<mobile3.length;i++)
            System.out.println(mobile[i]);
输出l果Q?br />    13811071503
    13811071501
存在的问题:
每次都是倒序Q可以再对程序稍加改动,变成正序?/font>


]]>
格式化输出数?/title><link>http://www.aygfsteel.com/hbcui1984/articles/70628.html</link><dc:creator>扬州?/dc:creator><author>扬州?/author><pubDate>Tue, 19 Sep 2006 14:29:00 GMT</pubDate><guid>http://www.aygfsteel.com/hbcui1984/articles/70628.html</guid><wfw:comment>http://www.aygfsteel.com/hbcui1984/comments/70628.html</wfw:comment><comments>http://www.aygfsteel.com/hbcui1984/articles/70628.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/hbcui1984/comments/commentRss/70628.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/hbcui1984/services/trackbacks/70628.html</trackback:ping><description><![CDATA[ <p>在实际工作中Q常帔R要设定数字的输出格式Q如以百分比的Ş式输出,或者设定小C数等Q先E微ȝ如下?br />主要使用的类Qjava.text.DecimalFormat<br />1。实例化对象Q可以用如下两种ҎQ?br />DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();<br />DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();<br />因ؓDecimalFormatl承自NumberFormat?br />2。设定小C?br />pȝ默认数位数?Q如Q?br />  DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();<br />  System.out.println(df.format(12.3456789));<br />输出Q?2.346<br />现在可以通过如下Ҏ把小Cؓ设ؓ两位Q?br />  <font color="#ff0000">df.setMaximumFractionDigits(2);<br /></font>  System.out.println(df.format(12.3456789));<br />则输ZؓQ?2.35<br />3。将数字转化为百分比输出Q有如下两种ҎQ?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。设|分l大?br />   DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();<br /> <font color="#ff0000"> df1.setGroupingSize(2);<br /></font>  System.out.println(df1.format(123456789));<br />输出Q?,23,45,67,89<br />q可以通过df1.setGroupingUsed(false);来禁用分l设|,如:<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 />输出Q?23456789<br />5。设|小Cؓ必须??br />  DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();<br />  <font color="#ff0000">df2.applyPattern("0.00");<br /></font>  System.out.println(df2.format(1.2));<br />输出Q?.20</p> <img src ="http://www.aygfsteel.com/hbcui1984/aggbug/70628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/hbcui1984/" target="_blank">扬州?/a> 2006-09-19 22:29 <a href="http://www.aygfsteel.com/hbcui1984/articles/70628.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java中关于时间日期操作的常用函数http://www.aygfsteel.com/hbcui1984/articles/70269.html扬州?/dc:creator>扬州?/author>Mon, 18 Sep 2006 05:40:00 GMThttp://www.aygfsteel.com/hbcui1984/articles/70269.htmlhttp://www.aygfsteel.com/hbcui1984/comments/70269.htmlhttp://www.aygfsteel.com/hbcui1984/articles/70269.html#Feedback0http://www.aygfsteel.com/hbcui1984/comments/commentRss/70269.htmlhttp://www.aygfsteel.com/hbcui1984/services/trackbacks/70269.htmlCalendar time=Calendar.getInstance();
time.clear();
time.set(Calendar.YEAR,year);
time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月ؓ0            
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
2.Calendar和Date的{?br />(1) Calendar转化为Date
Calendar cal=Calendar.getInstance();
Date date=cal.getTime();
(2) Date转化为Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
3.格式化输出日期时?br />Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(df.format(date));
4.计算一q中的第几星?br />(1)计算某一天是一q中的第几星?br />Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 3);
int weekno=cal.get(Calendar.WEEK_OF_YEAR);
(2)计算一q中的第几星期是几号
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println(df.format(cal.getTime()));
输出:
2006-01-02
5.add()和roll()的用?br />(1)add()Ҏ
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.add(Calendar.DATE, -4);
Date date=cal.getTime();
System.out.println(df.format(date));
cal.add(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
输出Q?br />    2006-08-30
    2006-09-03
(2)rollҎ
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.roll(Calendar.DATE, -4);
date=cal.getTime();
System.out.println(df.format(date));
cal.roll(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
输出Q?br />    2006-09-29
    2006-09-03
可见Q?font color="#ff3300">roll()Ҏ在本月内循环Q一般用add()ҎQ?/font>

]]>
վ֩ģ壺 | »| ϵ| | | ƽ| ԭ| | ־| ߷| Ȫ| | | | ͷ| | ɽ| ܿ| | ڶ| | | | | | ͸| | | ˮ| Ͻ| ʷ| | | | | | | | ƽ| | |