首页 > 代码库 > documeant 学习总结(二)

documeant 学习总结(二)

(一)移除节点及属性

技术分享    /**移除节点和属性的操作
技术分享     * @throws DocumentException
技术分享     */
技术分享    public void RemoveOperator() throws DocumentException{
技术分享        //待生成xml的字符串
技术分享        String str="<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>";
技术分享        //生成一个Document
技术分享        Document document = DocumentHelper.parseText(str);
技术分享        
技术分享        Element root=document.getRootElement();
技术分享        //删除类型为society的book节点
技术分享        Element book_society=(Element)document.selectSingleNode("//book[@type=‘society‘]");
技术分享        root.remove(book_society);
技术分享        System.out.println("1。正确的删除了类型为society的book节点");
技术分享        System.out.println(document.asXML());
技术分享        
技术分享        //删除sex节点
技术分享        Element sex=(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());
技术分享        
技术分享        //删除属性
技术分享        Attribute type=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(Element element)
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()方法添加

技术分享public void CombineDocument() throws DocumentException{
技术分享        //待生成两个Document的字符串
技术分享        String str_book="<root><book type=‘science‘><Name>Java</Name><price>100</price></book>"
技术分享            +"<book type=‘society‘><Name>Society security</Name><price>130</price></book>"
技术分享            +"</root>";
技术分享        String str_author="<root><author><name>chb</name><sex>boy</sex></author></root>";
技术分享        
技术分享        //生成两个Document
技术分享        Document doc_book=DocumentHelper.parseText(str_book);
技术分享        Document doc_author=DocumentHelper.parseText(str_author);
技术分享        
技术分享        //取出doc_author的author节点,添加到doc_book的根结点
技术分享        Element author=(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(Branch branch)
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)另一种可行的方法

技术分享public void CombineDocument() throws DocumentException{
技术分享        //待生成两个Document的字符串
技术分享        String str_book="<root><book type=‘science‘><Name>Java</Name><price>100</price></book>"
技术分享            +"<book type=‘society‘><Name>Society security</Name><price>130</price></book>"
技术分享            +"</root>";
技术分享        String str_author="<root><author><name>chb</name><sex>boy</sex></author></root>";
技术分享        
技术分享        //生成两个Document
技术分享        Document doc_book=DocumentHelper.parseText(str_book);
技术分享        Document doc_author=DocumentHelper.parseText(str_author);
技术分享        
技术分享        //新生成一个Document
技术分享        Element author=DocumentHelper.createElement("author");
技术分享        author.appendContent((Element)doc_author.selectSingleNode("//author"));
技术分享        //当前author尚无父节点,所以可以使用add方法添加
技术分享        doc_book.getRootElement().add(author);
技术分享
技术分享        System.out.println(doc_book.asXML());
技术分享    }
 
 

documeant 学习总结(二)