dom4j学习方法汇总
dom4j学习总结(二)
(一)移除节点及属性
*@throwsDocumentException
*/
publicvoidRemoveOperator()throwsDocumentException{
//待生成xml的字符串
Stringstr="<root><booktype='science'><Name>Java</Name><price>100</price></book>"
+"<booktype='society'><Name>Societysecurity</Name><price>130</price></book>"
+"<author><name>chb</name><sex>boy</sex></author></root>";
//生成一个Document
Documentdocument=DocumentHelper.parseText(str);
Elementroot=document.getRootElement();
//删除类型为society的book节点
Elementbook_society=(Element)document.selectSingleNode("//book[@type='society']");
root.remove(book_society);
System.out.println("1。正确的删除了类型为society的book节点");
System.out.println(document.asXML());
//删除sex节点
Elementsex=(Element)root.selectSingleNode("//sex");
//从root节点删除
root.remove(sex);
System.out.println("2。这样是不能删除sex节点的");
System.out.println(document.asXML());
//从author节点删除
root.element("author").remove(sex);
System.out.println("3。这样就可以正确删除sex节点");
System.out.println(document.asXML());
//删除属性
Attributetype=root.element("book").attribute("type");
root.element("book").remove(type);
System.out.println("4。正确删除book节点的type属性");
System.out.println(document.asXML());
}
输出结果为:
1。正确的删除了类型为society的book节点
<?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节点的
<?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节点
<?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节点的type属性
<?xml version="1.0" encoding="UTF-8"?>
<root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>
分析:
第二个输出结果不能删除sex节点,我们需要看dom4j的API
remove
public boolean remove(Elementelement)
-
Removes the given
Element
if the node is an immediate child of this branch. If the given node is not an immediate child of this branch then theNode.detach()
method should be used instead. -
-
- Parameters:
-
element
- is the element to be removed - Returns:
- true if the element was removed
从中我们可以看出,remove只能用在它自己的直接孩子节点上,不能用在孙子节点上,因为sex节点不是root节点的直接孩子节点,所以不能删除;而sex节点却是author节点的直接孩子节点,所以第三个输出可以删除。
(二)将两个Document合并为一个Document
先看一个错误的情况
(1)使用add()方法添加
//待生成两个Document的字符串
Stringstr_book="<root><booktype='science'><Name>Java</Name><price>100</price></book>"
+"<booktype='society'><Name>Societysecurity</Name><price>130</price></book>"
+"</root>";
Stringstr_author="<root><author><name>chb</name><sex>boy</sex></author></root>";
//生成两个Document
Documentdoc_book=DocumentHelper.parseText(str_book);
Documentdoc_author=DocumentHelper.parseText(str_author);
//取出doc_author的author节点,添加到doc_book的根结点
Elementauthor=(Element)doc_author.selectSingleNode("//author");
doc_book.getRootElement().add(author);
System.out.println(doc_book.asXML());
}
调用CombineDocument函数,会出现以下错误:
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节点已经有一个root节点了,不能再添加到另一个节点上去。
(2)使用appendContent()方法
即将doc_book.getRootElement().add(author);
改为:doc_book.getRootElement().appendContent(author);
输出结果为:
<?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节点,只是把author节点的子节点添加上去了,但是由此可见,appendContent方法是有希望的。
我们看一下dom4j的API:
appendContent
public void appendContent(Branchbranch)
-
Appends the content of the given branch to this branch instance. This method behaves like the
Collection.addAll(java.util.Collection)
method. -
-
- Parameters:
-
branch
- is the branch whose content will be added to me.
(3)使用正确的appendContent方法
将:Element author=(Element)doc_author.selectSingleNode("//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>
是正确结果
(4)另一种可行的方法
//待生成两个Document的字符串
Stringstr_book="<root><booktype='science'><Name>Java</Name><price>100</price></book>"
+"<booktype='society'><Name>Societysecurity</Name><price>130</price></book>"
+"</root>";
Stringstr_author="<root><author><name>chb</name><sex>boy</sex></author></root>";
//生成两个Document
Documentdoc_book=DocumentHelper.parseText(str_book);
Documentdoc_author=DocumentHelper.parseText(str_author);
//新生成一个Document
Elementauthor=DocumentHelper.createElement("author");
author.appendContent((Element)doc_author.selectSingleNode("//author"));
//当前author尚无父节点,所以可以使用add方法添加
doc_book.getRootElement().add(author);
System.out.println(doc_book.asXML());
}
本文地址:http://www.45fan.com/a/question/68335.html