`

dom4j学习总结

    博客分类:
  • XML
阅读更多
dom4j学习总结
(一)移除节点及属性

    /** *//**移除节点和属性的操作
     * @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 the Node.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());
    }
分享到:
评论

相关推荐

    DOM4J很全的学习资料知识点讲解加上例子

    DOM4J很全的学习资料知识点讲解加上例子,总结的很详细。对学习DOM4J很有帮助,可作为入门 也可以作为参考资料

    Java+flex使用dom4j读写xml

    环境:MyEclipse6.0.1+Flex3插件版,jdk1.6,tomcat6.0,dom4j1.6.1,lcds.war, 使用技术:1.Java+flex,及其对象的转换。 2.Java方面,使用dom4j对xml进行操作,包括节点的读取,添加,修改,删除。 3.Flex方面...

    xml资料

    dom4j学习总结 以及练习

    xml笔记及操作代码

    自己总结的一份xml学习笔记。内容包括xml文件的书写格式,约束、jaxp及dom4j对xml解析的代码。

    Java语言基础下载

    DOM4J解析实例 412 JDOM解析实例 413 JAVA操纵XML 实例讲解 414 通过JAVA写数据到XML里面 415 内容总结 418 独立实践 418 第二十三章:HTML基础 419 学习目标 419 知识要点 420 HTML元素 420 标签属性 420 HTML基本...

    详细的XML解析笔记

    本文档详细的给出了XML的介绍和XML解析的实例。包括DOM4J和SAX解析,节点的名和值得读取,属性的读取。生成XML文件等

    JQuery学习总结【二】

    一:JQuery知识点 *:JQuery的dom操作 *:动态创建dom节点 比如动态创建表格等,在js里面进行完成。 *删除节点 这里面的删除就是将其放在了一个地方,并不是真的删除,之后可以使用。...script src="js/j

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    4.DOM则为脚本和对象的交流提供一个公共平台,并将结果显示在浏览器窗口。 如果任何一个部分发生错误,都不会得到正确结果。 好了,看到这里,我们已经对XML是如何工作的有一个整体的大致的概念。通过这一章的...

    Android中对xml文件解析的3种方式总结

    主要给大家介绍了关于Android中对xml文件解析的3种方式,分别是 Dom 、 SAX 和 dom4j,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。

    颜色分类leetcode-deep-learning-colonoscopy:结肠镜息肉检测和分类的深度学习综述

    颜色分类leetcode 结肠镜息肉检测和分类的深度学习 该存储库是根据以下评论论文创建的...最后,第四部分总结了每项研究报告的性能指标。 欢迎提出建议,请在提交拉取请求之前检查。 目录: 研究 息肉检测和定位 学习 日

    Maven权威指南 很精典的学习教程,比ANT更好用

    总结 2. 安装和运行Maven 2.1. 验证你的Java安装 2.2. 下载Maven 2.3. 安装Maven 2.3.1. 在Mac OSX上安装Maven 2.3.2. 在Microsoft Windows上安装Maven 2.3.3. 在Linux上安装Maven 2.3.4. 在FreeBSD或...

    前端笔记.zip

    这个文档包含了HTML/css的一些基础,还有JavaScript中的基础语法、DOM、BOM还有一些学习js中面向对象、和移动web开发、AJAX、jQuery的一些总结,还有些Web前端与移动开发面试宝典; 6、什么是事件冒泡/捕获? 事件...

    Java学习笔记-个人整理的

    {14.4}dom4j}{207}{section.14.4} {14.5}XPath}{210}{section.14.5} {14.6}apache.commons}{211}{section.14.6} {15}sqlite3}{213}{chapter.15} {16}Web基础}{215}{chapter.16} {16.1}...

Global site tag (gtag.js) - Google Analytics