Dom4j是一个易于使用的,开源的库,在Java平台上与XML,XPath,XSLT协同工作。使用Java集合框架,全面支持DOM,SAX,JAXP。 
官方网站:http://dom4j.org

1.将XML文件转换为一个Document对象

import java.net.URL;

import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.io.SAXReader;

public class Foo {

public Document parse(URL url) throws DocumentException { 
        SAXReader reader = new SAXReader(); 
        Document document = reader.read(url); 
        return document; 
    } 
}

2.很多方法用于操作Document,可以返回标准的Java迭代器

public void bar(Document document) throws DocumentException {

Element root = document.getRootElement();

// iterate through child elements of root 
        for ( Iterator i = root.elementIterator(); i.hasNext(); ) { 
            Element element = (Element) i.next(); 
            // do something 
        }

// iterate through child elements of root with element name "foo" 
        for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) { 
            Element foo = (Element) i.next(); 
            // do something 
        }

// iterate through attributes of root 
        for ( Iterator i = root.attributeIterator(); i.hasNext(); ) { 
            Attribute attribute = (Attribute) i.next(); 
            // do something 
        } 
     }

3.快速循环

如果你需要遍历一个很大的XML文档,可以使用快速循环(递归)来改善性能。可以避免为每一次循环都创建一个迭代器对象。

public void treeWalk(Document document) { 
        treeWalk( document.getRootElement() ); 
    }

public void treeWalk(Element element) { 
        for ( int i = 0, size = element.nodeCount(); i < size; i++ ) { 
            Node node = element.node(i); 
            if ( node instanceof Element ) { 
                treeWalk( (Element) node ); 
            } 
            else { 
                // do something.... 
            } 
        } 
    }

4.创建一个新的XML文档

import org.dom4j.Document; 
import org.dom4j.DocumentHelper; 
import org.dom4j.Element;

public class Foo {

public Document createDocument() { 
        Document document = DocumentHelper.createDocument(); 
        Element root = document.addElement( "root" );

Element author1 = root.addElement( "author" ) 
            .addAttribute( "name", "James" ) 
            .addAttribute( "location", "UK" ) 
            .addText( "James Strachan" ); 
        
        Element author2 = root.addElement( "author" ) 
            .addAttribute( "name", "Bob" ) 
            .addAttribute( "location", "US" ) 
            .addText( "Bob McWhirter" );

return document; 
    } 
}

5.将XML文档写入文件

通过write()方法将一个XML文档写入文件是最简单的方式。 
    FileWriter out = new FileWriter( "foo.xml" ); 
    document.write( out ); 
    
  如果你想改变输出的格式,比如美观的格式(含缩进)和压缩的格式(不含缩进),可以使用XMLWriter类。

import org.dom4j.Document; 
import org.dom4j.io.OutputFormat; 
import org.dom4j.io.XMLWriter;

public class Foo {

public void write(Document document) throws IOException {

// lets write to a file 
        XMLWriter writer = new XMLWriter( 
            new FileWriter( "output.xml" ) 
        ); 
        writer.write( document ); 
        writer.close();

// Pretty print the document to System.out 
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        writer = new XMLWriter( System.out, format ); 
        writer.write( document );

// Compact format to System.out 
        format = OutputFormat.createCompactFormat(); 
        writer = new XMLWriter( System.out, format ); 
        writer.write( document ); 
    } 
}

6.XML和String之间的相互转换

通过asXML()方法,你可以将一个Document,Attribute或Element对象转换成一个包含XML文本的字符串。 
        Document document = ...; 
        String text = document.asXML();

同样,通过DocumentHelper.parseText()方法,你也可以方便地将一个字符串形式的XML转换成一个Document对象。 
        String text = "<person> <name>James</name> </person>"; 
        Document document = DocumentHelper.parseText(text);

Dom4j官网解释实例的更多相关文章

  1. layui前端框架实例(修复官网数据接口异常问题)

    layui前端框架实例,官网的实例会提示数据接口异常,已修复. 主要是修改数据表格,做一个可以用的实例,可以选中,编辑,删除等. gitee地址:https://gitee.com/pingg2019 ...

  2. 【ABAP系列】SAP LSMW(摘自官网)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP LSMW(摘自官网)   前 ...

  3. [ActionScript 3.0] Away3D 官网实例

    /* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...

  4. activiti官网实例项目activiti-explorer之扩展流程节点属性2

    情景需求:需要查找activiti-explorer项目中获取流程id的方法,然后根据流程id获取相应字段在节点属性中添加内容. 大致流程:拿取整个流程id获取对应表单属性,在页面节点属性中展示对应表 ...

  5. Knockout官网实例在MVC下的实现-02,实现计次

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. 当次数达到3: View视图 页面包含三个部分:1.显示点击按钮的次数2.button按钮 ...

  6. Knockout官网实例在MVC下的实现-01,实现Hello world

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. View视图 Knockout的一个特点是:声明式绑定,即Declarative bind ...

  7. 官网实例详解-目录和实例简介-keras学习笔记四

    官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras   版权声明: ...

  8. vue3官网介绍,安装,创建一个vue实例

    前言:这一章主要是vue的介绍.安装.以及如何创建一个vue实例. 一.vue介绍 vue3中文官网:建议先自己看官网. https://v3.cn.vuejs.org/ vue是渐进式框架,渐进式指 ...

  9. BootStrap的一个标准框架的内容解释——来源于bootstrap官网

    <!DOCTYPE html><!--HTML5的定义--><html lang="zh-cn"> <head> <meta ...

随机推荐

  1. Spring Cloud Config的配置中心使用非对称性加密

    首先,我们需要通过keytool工具来生成密钥对. keytool是JDK中的一个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认 ...

  2. PAT甲级——A1124 Raffle for Weibo Followers

    John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...

  3. 实现ViewPager的联动效果

    参考链接:android - Synchronizing two ViewPagers using OnPageChangeListener - Stack Overflow 其中有个非常完美的解决方 ...

  4. 基于Swagger+SpringBoot快速构建javaweb项目

    章节导航 SpringBoot&Swagger简介 数据模型和接口定义 项目框架生成 业务逻辑实现 项目源码地址 github项目路径:https://github.com/Vikezhu/s ...

  5. Java系列笔记(4) - JVM监控与调优【转】

    Java系列笔记(4) - JVM监控与调优[转]   目录 参数设置收集器搭配启动内存分配监控工具和方法调优方法调优实例     光说不练假把式,学习Java GC机制的目的是为了实用,也就是为了在 ...

  6. Excel skill: 如何替换换行符,以及如何把一格转换成多行/多列

    http://blog.sciencenet.cn/blog-508298-695290.html 增加一辅助列,用替换函数替换掉软回车.比如A列是数据,从A1开始,则插入B列,B1输入公式=REPL ...

  7. call和apply的应用

    相同点 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 var arrayLike = { 0: 'item1', 1: 'item2', 2: 'ite ...

  8. 实例测试java的Integer转String的效率问题1.8

    原文链接:https://blog.csdn.net/chicaohun7473/article/details/100851373 查看String源码时,读到源码的toString方法时,打算探究 ...

  9. NoSQL与关系数据库的比较

  10. Gym - 100543L

    Gym - 100543Lhttps://vjudge.net/problem/153854/origin区间dp,要从区间长度为1开始dp #include<iostream> #inc ...