Dom4j is an easy-to-use and open-source library for XML, XPath, and XSLT parsing. It is applied to the Java platform. It adopts the Java Collection framework and fully supports Dom, sax, and JAXP.Official Website: http://www.dom4j.org /.
Dom4j has more powerful functions in many aspects. Well-known hibernate also uses it for parsing XML configuration files.
Import java. Io. filewriter;
Import java. Io. ioexception;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. documenthelper;
Import org. dom4j. element;
Import org. dom4j. Io. outputformat;
Import org. dom4j. Io. saxreader;
Import org. dom4j. Io. xmlwriter;
Public class domcreator ...{
Public static void main (string [] ARGs) throws ioexception, disable entexception ...{
Document Table = incluenthelper. createdocument ();
Element root = table. addelement (constants. data_table );
Element ROW = root. addelement (constants. data_row );
Element item = row. addelement (constants. data_item );
Item. addtext ("data ");
Outputformat format = outputformat. createprettyprint ();
Filewriter writer = new filewriter ("dom4j. xml ");
Xmlwriter outputter = new xmlwriter (writer, format );
Outputter. Write (table );
Outputter. Close ();
Saxreader reader = new saxreader ();
Document document = reader. Read ("dom4j. xml ");
Xmlwriter domstream = new xmlwriter (system. Out, format );
Domstream. Write (document );
Domstream. Close ();
}
}
Constant declaration file:
Public interface constants ...{
Public static string data_table = "table ";
Public static string data_row = "row ";
Public static string data_item = "item ";
}
For more information about dom4j, see: http://xhy0422.javaeye.com/blog/50235
See the http://blog.csdn.net/yedian/archive/2007/12/19/1954194.aspx