码迷,mamicode.com
首页 > 其他好文 > 详细

使用Dom4j生成xml文件

时间:2016-07-16 06:36:40      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

场景:使用dom4j生成以下xml文件

<?xml version="1.0" encoding="UTF-8"?>

<result>
  <code>1</code>
  <data>
    <person>
      <name>张三</name>
      <id>1</id>
      <url>http://192.168.191.1:9999/TestWeb/c7fe21616d2a5e2bd1e84bd453a5b30f.jpg</url>
      <courses>
        <course>
          <courseName>语文</courseName>
          <courseMarks>90</courseMarks>
          <courseId>1</courseId>
        </course>
        <course>
          <courseName>数学</courseName>
          <courseMarks>80</courseMarks>
          <courseId>2</courseId>
        </course>
        <course>
          <courseName>英语</courseName>
          <courseMarks>70</courseMarks>
          <courseId>3</courseId>
        </course>
      </courses>
    </person>
    <person name="李四" id="2" url="http://192.168.191.1:9999/TestWeb/4052858c526002a712ef574ccae1948f.jpg">
      <course courseName="语文" courseMarks="91" courseId="1"/>
      <course courseName="数学" courseMarks="82" courseId="1"/>
      <course courseName="英语" courseMarks="73" courseId="1"/>
    </person>
  </data>
</result>

 

 

以下是实现代码:

import java.io.FileWriter;
import java.io.IOException;

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

public class TestXML {

    public static void main(String[] args) {
        FileWriter out = null;
        try {
            out = new FileWriter( "src/foo.xml" );  //写入文件
            createDocument().write( out );
            
            OutputFormat format = OutputFormat.createPrettyPrint();  //转换成字符串
            format.setEncoding("UTF-8");
            XMLWriter writer = new XMLWriter( System.out, format );
            writer.write( createDocument() );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (out!=null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static Document createDocument() {
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement( "result" );
        root.addElement("code").addText("1");
        Element data = root.addElement("data");

        Element person1 = data.addElement( "person" );
        person1.addElement( "name" ).setText("张三");
        person1.addElement( "id" ).setText("1");
        person1.addElement( "url" ).setText("http://192.168.191.1:9999/TestWeb/c7fe21616d2a5e2bd1e84bd453a5b30f.jpg");
        Element courses1 =person1.addElement( "courses" );
        Element course1 = courses1.addElement( "course" );
        course1.addElement( "courseName" ).setText("语文");
        course1.addElement( "courseMarks" ).setText("90");
        course1.addElement( "courseId" ).setText("1");
        Element course2 = courses1.addElement( "course" );
        course2.addElement( "courseName" ).setText("数学");
        course2.addElement( "courseMarks" ).setText("80");
        course2.addElement( "courseId" ).setText("2");
        Element course3 = courses1.addElement( "course" );
        course3.addElement( "courseName" ).setText("英语");
        course3.addElement( "courseMarks" ).setText("70");
        course3.addElement( "courseId" ).setText("3");

        Element person2 = data.addElement( "person" )
                .addAttribute( "name", "李四" )
                .addAttribute( "id", "2" )
                .addAttribute("url", "http://192.168.191.1:9999/TestWeb/4052858c526002a712ef574ccae1948f.jpg");
        person2.addElement( "course" )
        .addAttribute( "courseName", "语文" )
        .addAttribute( "courseMarks", "91" )
        .addAttribute( "courseId", "1" );
        person2.addElement( "course" )
        .addAttribute( "courseName", "数学" )
        .addAttribute( "courseMarks", "82" )
        .addAttribute( "courseId", "1" );
        person2.addElement( "course" )
        .addAttribute( "courseName", "英语" )
        .addAttribute( "courseMarks", "73" )
        .addAttribute( "courseId", "1" );

        return document;
    }

}

 

使用Dom4j生成xml文件

标签:

原文地址:http://www.cnblogs.com/xiezie/p/5675054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!