java中用jdom 如何创建xml文档/将数据写入XML中

JDomOutput.java代码如下

----------------------------------------------------------------------------------------------------------------------

import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JDomOutput
{
    public static void main(String[] args) throws IOException
    {
        //创建文档
        Document document = new Document();
        //创建根元素
        Element people = new Element("people");
        //把根元素加入到document中
        document.addContent(people); 
        
        //创建注释
        Comment rootComment = new Comment("将数据从程序输出到XML中!");
        people.addContent(rootComment);
        
        //创建父元素
        Element person1 = new Element("person");
        //把元素加入到根元素中
        people.addContent(person1);
        //设置person1元素属性
        person1.setAttribute("id", "001");
        
        Attribute person1_gender = new Attribute("gender", "male");
        person1.setAttribute(person1_gender);
        
        Element person1_name = new Element("name");
        person1_name.setText("刘德华");
        person1.addContent(person1_name);
        
        Element person1_address = new Element("address");
        person1_address.setText("香港");
        person1.addContent(person1_address);
        
        
        Element person2 = new Element("person");
        people.addContent(person2);
        
        person2.setAttribute("id", "002").setAttribute("gender","male");//添加属性,可以一次添加多个属性
        
        Element person2_name = new Element("name");
        person2_name.setText("林志颖");
        person2.addContent(person2_name);
        
        Element person2_address = new Element("address");
        person2_address.setText("台湾");
        person2.addContent(person2_address);
        
        
        //设置xml输出格式
        Format format = Format.getPrettyFormat();
        format.setEncoding("utf-8");//设置编码
        format.setIndent("    ");//设置缩进
        
        
        //得到xml输出流
        XMLOutputter out = new XMLOutputter(format);
        //把数据输出到xml中
        out.output(document, new FileOutputStream("jdom.xml"));//或者FileWriter
        
    }

}

---------------------------------------------------------------------------------------------------------------------------


生成的xml内容如下:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<people>
    <!--将数据从程序输出到XML中!-->
    <person id="001" gender="male">
        <name>刘德华</name>
        <address>香港</address>
    </person>
    <person id="002" gender="male">
        <name>林志颖</name>
        <address>台湾</address>
    </person>
</people>

转载于:https://my.oschina.net/zhenguoguan/blog/138638

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值