Use dom4j to create an XML file

Source: Internet
Author: User

The Code is as follows:

1 package XML; 2 3 Import Java. io. filewriter; 4 Import Java. io. ioexception; 5 import Java. io. writer; 6 Import Org. dom4j. document; 7 Import Org. dom4j. export enthelper; 8 Import Org. dom4j. element; 9 Import Org. dom4j. io. outputformat; 10 Import Org. dom4j. io. xmlwriter; 11 12 public class dom4jcreatexml {13 public void testcreatexml () {14 // create an XML document 15 document DOC = documenthelper. createdocument (); 16 // to XML Add Comment 17 Doc. addcomment ("here is the comment"); 18 // create a node named students. because it is the first creation, it is the root node. If you create a node through doc, an error is returned. 19 element root = Doc. addelement ("Students"); 20 // create a node named student under the root node 21 element stuele = root. addelement ("student"); 22 // Add attribute 23 to student node. addattriele ("ID", "101"); 24 // Add a subnode 25 element nameele = stuele to the student node. addelement ("name"); 26 // set the subnode text 27 nameele. settext ("Zhang San"); 28 // used to format the XML content and set the header label 29 outputformat format = outputformat. createprettyprint (); 30 // set the XML document encoding to utf-831 format. setencoding ("UTF-8"); 32 writer out; 33 try {34 // create an output stream object 35 out = new filewriter ("E: // XML // new. XML "); 36 // create a dom4j object to create XML 37 xmlwriter writer = new xmlwriter (Out, format ); 38 // call the write method to write the DOC file to the specified path 39 writer. write (DOC); 40 writer. close (); 41 system. out. print ("XML file generated successfully"); 42} catch (ioexception e) {43 system. out. print ("failed to generate xml file"); 44 E. printstacktrace (); 45} 46} 47 48 public static void main (string [] ARGs) {49 dom4jcreatexml xml = new dom4jcreatexml (); 50 XML. testcreatexml (); 51} 52}

Generated XML file:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 3 <! -- Here is the comment --> 4 <students> 5 <student ID = "101"> 6 <Name> Zhang San </Name> 7 </student> 8 </students>

 

Use dom4j to create an XML file

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.