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