(一)移除節(jié)點(diǎn)及屬性
輸出結(jié)果為:
1。正確的刪除了類型為society的book節(jié)點(diǎn)
<?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
2。這樣是不能刪除sex節(jié)點(diǎn)的
<?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
3。這樣就可以正確刪除sex節(jié)點(diǎn)
<?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>
4。正確刪除book節(jié)點(diǎn)的type屬性
<?xml version="1.0" encoding="UTF-8"?>
<root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>
分析:
第二個輸出結(jié)果不能刪除sex節(jié)點(diǎn),我們需要看dom4j的API
public boolean remove(Element?element)
Element
if the node is an immediate child of this branch. If the given node is not an immediate child of this branch then the Node.detach()
method should be used instead.
element
- is the element to be removed
從中我們可以看出,remove只能用在它自己的直接孩子節(jié)點(diǎn)上,不能用在孫子節(jié)點(diǎn)上,因?yàn)閟ex節(jié)點(diǎn)不是root節(jié)點(diǎn)的直接孩子節(jié)點(diǎn),所以不能刪除;而sex節(jié)點(diǎn)卻是author節(jié)點(diǎn)的直接孩子節(jié)點(diǎn),所以第三個輸出可以刪除。
(二)將兩個Document合并為一個Document
先看一個錯誤的情況
(1)使用add()方法添加
調(diào)用CombineDocument函數(shù),會出現(xiàn)以下錯誤:
org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultElement@17bd6a1 [Element: <author attributes: []/>]" could not be added to the element "root" because: The Node already has an existing parent of "root"
?at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:1521)
?at org.dom4j.tree.AbstractElement.add(AbstractElement.java:1002)
?at xml_chb.dom4j_chb.CombineDocument(dom4j_chb.java:189)
?at xml_chb.dom4j_chb.main(dom4j_chb.java:199)
Exception in thread "main"
即提示author節(jié)點(diǎn)已經(jīng)有一個root節(jié)點(diǎn)了,不能再添加到另一個節(jié)點(diǎn)上去。
(2)使用appendContent()方法
即將doc_book.getRootElement().add(author);
改為:doc_book.getRootElement().appendContent(author);
輸出結(jié)果為:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book type="science"><Name>Java</Name><price>100</price></book>
<book type="society"><Name>Society security</Name><price>130</price></book>
<name>chb</name><sex>boy</sex>
</root>
可以看出,缺少了author節(jié)點(diǎn),只是把a(bǔ)uthor節(jié)點(diǎn)的子節(jié)點(diǎn)添加上去了,但是由此可見,appendContent方法是有希望的。
我們看一下dom4j的API:
public void appendContent(Branch?branch)
Collection.addAll(java.util.Collection)
method.
branch
- is the branch whose content will be added to me.(3)使用正確的appendContent方法
將:Element author=(Element)doc_author.selectSingleNode("http://author");
doc_book.getRootElement().appendContent(author);
改為:doc_book.getRootElement().appendContent(doc_author.getRootElement());
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book type="science"><Name>Java</Name><price>100</price></book>
<book type="society"><Name>Society security</Name><price>130</price></book>
<author><name>chb</name><sex>boy</sex></author>
</root>
是正確結(jié)果
(4)另一種可行的方法