Dom4j写XML

package com;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

public class Text {
  public static void main(String[] args) {
    //创建文档并设置文档根节点元素:第1种方式
//    Document document=DocumentHelper.createDocument();
//    Element root=DocumentHelper.createElement("student");
//    document.setRootElement(root);

    //创建文档并设置文档根节点元素:第2种方式
    Element root=DocumentHelper.createElement("student");
    Document document=DocumentHelper.createDocument(root);
    //在节点内添加属性
    root.addAttribute("name", "majun");
    //创建一个新的节点hello放在root下面
    Element  helloelement1=root.addElement("hello");
    //设置节点hello的内容为hello
    helloelement1.setText("hello");
    //设置输出的格式
    OutputFormat  format=new  OutputFormat("   ",true);
    XMLWriter writerxml;
    try {
        //输出
        writerxml = new XMLWriter(new FileOutputStream("mm.xml"),format);
        writerxml.write(document);

    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

Dom4j写XML

时间: 2024-08-14 21:28:25

Dom4j写XML的相关文章

使用dom4j写xml文件——源码

dom4j版本:2.1.1 使用dom4j生成xml文档步骤: 1.创建一个Document对象的空白文档. 2.向Document中添加根元素,返回的Element表示根元素,xml文档只有一个根元素. 3.添加子元素. 4.创建XmlWriter对象 5.输出Document对象 元素对象 1 package cnblogs.testCSV; 2 3 public class Person { 4 private String id; 5 private String name; 6 pri

转:在java中使用dom4j解析xml

在java中使用dom4j解析xml 虽然Java中已经有了Dom和Sax这两种标准解析方式 但其操作起来并不轻松,对于我这么一个初学者来说,其中部分代码是活生生的恶心 为此,伟大的第三方开发组开发出了Jdom和Dom4j等工具 鉴于目前的趋势,我们这里来讲讲Dom4j的基本用法,不涉及递归等复杂操作 Dom4j的用法很多,官网上的示例有那么点儿晦涩,这里就不写了 首先我们需要出创建一个xml文档,然后才能对其解析 xml文档: <?xml version="1.0" encod

使用 Dom4j 对XML操作!!!

转自:http://blog.csdn.net/redarmy_chen/article/details/12969219 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它. 对主流的Java XML API进行的性能.功能和易用性的评测,dom4j无论在那个方面都是非常出色的.如今你可以看到越来越

JAVA用dom4j对xml的解析

一.DOM4j中,获得Document对象的方式有三种: [java] view plain copy 1.读取XML文件,获得document对象 SAXReader reader = new SAXReader(); Document   document = reader.read(new File("csdn.xml")); 2.解析XML形式的文本,得到document对象. String text = "<csdn></csdn>"

使用dom4j解析XML文档

dom4j的包开源包,不属于JDK里面,在myeclipse中要单独导入在项目中,这里不累赘了 做这个过程,很慢,因为很多方法没用过不熟悉,自己得去查帮助文档,而且还得去试,因为没有中文版,英文翻译不准确,也会导致多个同名的方法理解和用法不同 XML: <?xml version="1.0" encoding="utf-8"?> <students> <student age="25"> <name>

java通过dom4j处理xml文档

最近在用dom4j处理xml的数据接口,将java利用dom4j处理xml的过程记录下来. 导入jar包:dom4j-1.6.jar 下载地址:http://www.java2s.com/Code/JarDownload/dom4j/dom4j-1.6.jar.zip 自己写了个xml文档 1 <school> 2 <student> 3 <name>老子</name> 4 <no>20140804</no> 5 <majors

XML学习笔记(五):使用 jdom和dom4j 解析XML

XML解析的详细分析与jaxp解析XML详见:XML学习笔记(四):使用 DOM和SAX 解析XML 一.JDom 1.创建XML文件: 1)Document类即代表整个XML文档,把生成的 Document 利用 XMLOutputter 类输出即可. 2)映射关系:元素:Element:属性:Attribute:注解:Comment:文本信息:Text: 3)注意:addContent()是追加,setContent()会覆盖. /** * 创建XML * * @throws IOExcep

在java项目中怎样利用Dom4j解析XML文件获取数据

在曾经的学习.net时常常会遇到利用配置文件来解决项目中一些须要常常变换的数据.比方数据库的连接字符串儿等.这个时候在读取配置文件的时候.我们一般会用到一个雷configuration,通过这个类来进行直接读取,能够说这是程序中直接封装好的,包含配置文件里书写的格式等等.那么,假设.想要在配置文件里写一些其它的数据,直接以XML文件的格式进行书写.这个时候在后台怎样读取XML文件里的数据呢?这里利用Dom4j来解析XML文件里连接oracle数据库的xml数据. 在使用之前,首先要在自己的项目中

Java xml 操作(Dom4J修改xml &#160;&#160;+ xPath技术 &#160;+ SAX解析 + XML约束)

1 XML基础 1)XML的作用 1.1 作为软件配置文件 1.2 作为小型的"数据库" 2)XML语法(由w3c组织规定的) 标签: 标签名不能以数字开头,中间不能有空格,区分大小写.有且仅有一个根标签. 属性: 可有多个属性,但属性值必须用引号(单引号或双引号)包含,但不能省略,也不能单 双混用. 文档声明: <?xml version="1.0" encoding="utf-8"?> encoding="utf-8&q