Using Java to parse XML files (2) -- dom4j

Source: Internet
Author: User

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

Related Article

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.