//first download and add Dom4j.jar package (Download yourself) import java.io.File; import
Java.io.FileWriter;
Import Java.io.PrintWriter;
Import Javax.swing.JOptionPane;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element; /** * @author WSJ * * * public class Test1 {public static void main (string[] args) throws Exception {Document DOM=DOCU Menthelper.createdocument ()//create XML file Element root=dom.addelement ("Xval");//Add root element, Xval root.addattribute ("X", "
The value of the X string ");
Root.settext ("Middle value of xval label");
String Xml=dom.asxml ();
SYSTEM.OUT.PRINTLN (XML);
File F=new file ("D:/xml.xml");
PrintWriter pw=new PrintWriter (f);
Pw.write (XML);
Pw.close (); } OR:
/**
* Make XML file
* @throws Exception
/public static void Makexml () throws exception{
//Get Document Object
Document document =documenthelper.createdocument ();
Add root node
Element root=document.addelement ("hibernate-mapping");
Adds a node class with the attribute name name and table assigned
Element classelement=root.addelement ("class")
. AddAttribute ("name", " Com.aptech.User ").
AddAttribute ("table", "T_user"); text
//Add Property child nodes in//addtext node, add attribute Name value to "username"
Classelement.addelement ("Property"). AddAttribute ("name", "username");
Format output file
OutputFormat format=outputformat.createprettyprint ();
Declare the file
XMLWriter writer=new XMLWriter (New FileWriter ("User2.xml"), format);
Write out document
writer.write (documents);
Close file stream
writer.close ();
}