使用Dom4j生成xml文件(utf-8编码)

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>


Java代码实现:

import java.io.*;
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) {
		OutputStreamWriter out = null;
		try {
			out = new OutputStreamWriter(new FileOutputStream("src/foo.xml"),"UTF-8");//写入文件(编码utf-8格式),文件路径要确认存在
			createDocument().write( out );
			// 字符串控制台输出
			OutputFormat format = OutputFormat.createPrettyPrint();  //转换成字符串
			format.setEncoding("UTF-8");
			XMLWriter writer = new XMLWriter( System.out, format );
			writer.write( createDocument() );
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out!=null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					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;
	}

}

转载地址:http://www.cnblogs.com/zhi-hao/p/4016363.html

posted on 2017-10-31 10:04  菜鸟Z  阅读(576)  评论(0编辑  收藏  举报

导航