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. Android按钮绑定四种方式

    public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...

  2. 使用Excel表格导入数据到Oracle表

    在工作中我们会遇到将通过数据手动录入到系统中的需求,如果数据量比较小,那么手动输入是可行的,倘若数据量很大,那么这些数据手动录入将会是一个很大的工作量,为了简化这个手动录入的操作流程,我们可以使用Ex ...

  3. 4_5.springboot2.x之Web开发RestfulCRUD操作

    1).RestfulCRUD:CRUD满足Rest风格 URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作 普通CRUD(uri来区分操作) RestfulCRUD 查询 getE ...

  4. 18-1-函数中this的指向

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 2019-8-31-dotnet-获取指定进程的输入命令行

    title author date CreateTime categories dotnet 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:58 +0800 2019-0 ...

  6. Leetcode143. Reorder List重排链表

    给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: ...

  7. 使用subprocessm模块管理进程

    subprocess被用来替换一些老的模块和函数,如:os.system.os.spawn*.os.popen*.popen2.*.commands.*. subprocess的目的就是启动一个新的进 ...

  8. data方法也是模型类的连贯操作方法之一,

    data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值. 写操作 通常情况下我们都是通过create方法或者赋值的方式生成数据对象,然后写入数据库,例如: $Model = D(' ...

  9. CentOS7使用firewalld打开关闭防火墙与端口(转)

    CentOS7使用firewalld打开关闭防火墙与端口       1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop ...

  10. jquery ajax 放在重复点击事件beforeSend方法

    防止重复数据在实际项目开发中,提交表单时常常由于网络或者其原因,用户点击提交按钮误认为自己没有操作成功,进而会重复提交按钮操作次数,如果页面前端代码没有做一些相应的处理,通常会导致多条同样的数据插入数 ...