Using DOM4J to generate an XML file (Utf-8 encoding)

Source: Internet
Author: User
Tags string format

XML file Contents:

<?xml version= "1.0" encoding= "UTF-8"?> <result> <code>1</code> <data> <person&gt
      ; <name> John </name> <id>1</id> <url>http://192.168.191.1:9999/testweb/c7fe21616d2a5e 2bd1e84bd453a5b30f.jpg</url> <courses> <course> <courseName> Chinese </coursen ame> <courseMarks>90</courseMarks> <courseId>1</courseId> </cour se> <course> <courseName> Math </courseName> <coursemarks>80</course marks> <courseId>2</courseId> </course> <course> <course name> English </courseName> <courseMarks>70</courseMarks> <courseid>3</courseid > </course> </courses> </person> <person name= "Dick" id= "2" url= "http://192.16 8.191.1:9999/tesTweb/4052858c526002a712ef574ccae1948f.jpg "> <course coursename=" Chinese "coursemarks=" courseid= "1"/> & Lt;course coursename= "Mathematics" coursemarks= "courseid=" 1 "/> <course coursename=" English "coursemarks=" "courseId=" 1 "/> </person> </data> </result>


Java Code Implementation:

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");/write file (encoding utf-8 format), file path to confirm existence Createdo
			Cument (). write (out);  String console output OutputFormat format = Outputformat.createprettyprint ();
			Convert to String 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 ("John");
		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 ("language");
		Course1.addelement ("Coursemarks"). SetText ("90");
		Course1.addelement ("CourseID"). SetText ("1");
		Element Course2 = courses1.addelement ("course");
		Course2.addelement ("Coursename"). SetText ("mathematics");
		Course2.addelement ("Coursemarks"). SetText ("80");
		Course2.addelement ("CourseID"). SetText ("2");
		Element course3 = courses1.addelement ("course");
		Course3.addelement ("Coursename"). SetText ("English");
		Course3.addelement ("Coursemarks"). SetText ("70"); Course3.addelement ("CoursEId "). SetText (" 3 "); Element Person2 = data.addelement ("person"). AddAttribute ("name", "Dick"). AddAt Tribute ("id", "2"). AddAttribute ("url", "http://192.168.191.1:9999/TestWeb/4052858c526002a712ef574cc
		Ae1948f.jpg "); Person2.addelement ("course"). AddAttribute ("Coursename", "language"). AddAttribute ("Coursemarks", "a"). Addattribut
		E ("CourseID", "1"); Person2.addelement ("course"). AddAttribute ("Coursename", "mathematics"). AddAttribute ("Coursemarks", "the"). Addattribut
		E ("CourseID", "1"); Person2.addelement ("course"). AddAttribute ("Coursename", "English"). AddAttribute ("Coursemarks", "the"). Addattribut

		E ("CourseID", "1");
	return document; }

}

Reprint Address: http://www.cnblogs.com/zhi-hao/p/4016363.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.