Dom4j generates and parses XML documents

Source: Internet
Author: User

Dom4j is a very good Java xml api with excellent performance, powerful functionality and extreme ease of use. It is also an open source software. Now you can see that more and more Java software are using dom4j to read and write XML. It is particularly worth mentioning that Sun's jaxm is also using dom4j.

XML file

<?xml version="1.0" encoding="UTF-8"?> <employees> <employee> <name>ddviplinux</name> <sex>m</sex> <age>30</age> </employee> </employees>

Resolution and Creation

Package COM. alisoft. facepay. framework. bean; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. io. writer; import Java. util. iterator; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. export enthelper; import Org. dom4j. element; import Org. dom4j. io. saxreader; import Org. dom4j. io. xmlwriter;/***** @ author hongliang. dinghl * dom4j generate XML document and parse XML document */public class dom4jdemo implements xmldocument {public void createxml (string filename) {document = enenthelper. createdocument (); element employees = document. addelement ("employees"); element Employee = employees. addelement ("employee"); element name = employee. addelement ("name"); Name. settext ("ddvip"); element sex = employee. addelement ("sex"); sex. settext ("M"); element age = employee. addelement ("Age"); age. settext ("29"); try {writer filewriter = new filewriter (filename); xmlwriter = new xmlwriter (filewriter); xmlwriter. write (document); xmlwriter. close ();} catch (ioexception e) {system. out. println (E. getmessage () ;}} public void parserxml (string filename) {file inputxml = new file (filename); saxreader = new saxreader (); try {document = saxreader. read (inputxml); element employees = document. getrootelement (); For (iterator I = employees. elementiterator (); I. hasnext ();) {element Employee = (element) I. next (); For (iterator J = employee. elementiterator (); J. hasnext ();) {element node = (element) J. next (); system. out. println (node. getname () + ":" + node. gettext () ;}} catch (incluentexception e) {system. out. println (E. getmessage ();} system. out. println ("dom4j parserxml ");}}

 

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.