Java generates XML files using dom4j

Source: Internet
Author: User

Package Com.fq.fanqi;

Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.StringWriter;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.OutputFormat;
Import Org.dom4j.io.XMLWriter;

public class Taodashen {

public void analysis (file file) throws IOException {

Create a document model
Document document = Documenthelper.createdocument ();
Define a root node
Element root = document.addelement ("parameters");
Add child nodes under the root node
Element paramer1 = root.addelement ("Tao");
Node Add property
Paramer1.addattribute ("Taodashen", "Aunt Amoy");
Node Add text
Paramer1.addtext ("good aunt");
Element paramer2 = root.addelement ("Zhou");
Paramer2.addattribute ("Xiaozhou", "Mengwa");
Paramer2.addtext ("Moe Doll");
Element Paramer3 = root.addelement ("Xu");
Paramer3.addattribute ("Xinyi", "Teacher");
Element Guodaxia = paramer3.addelement ("Guowei");
Guodaxia.addattribute ("name", "Guo");
Guodaxia.addattribute ("Ming", "Daxia");
Guodaxia.addtext ("learned");

Creating XML Formats
StringWriter StringWriter = new StringWriter ();
OutputFormat Xmlformat = new OutputFormat ();
Xmlformat.setencoding ("UTF-8");
Xmlformat.setnewlines (TRUE);
Xmlformat.setindent ("");
FileWriter FileWriter = new FileWriter (file);
XMLWriter XMLWriter = new XMLWriter (Filewriter,xmlformat);
Xmlwriter.write (document);
Xmlwriter.close ();

}

public static void Main (string[] args) throws IOException {

Taodashen taodashen = new Taodashen ();
Taodashen.analysis (New File ("W:\\test.xml"));
}

}

Java generates XML files using dom4j

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.