Use Dom4j to create an XML file and dom4j to create an xml file

Source: Internet
Author: User
Tags processing instruction

Use Dom4j to create an XML file and dom4j to create an xml file

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>

 


Create an xml document in dom4j

Currently, XMLWriter of dom4j does not support standalone. You can override its writeDeclaration method:

Public class StandaloneWriter extends XMLWriter {
Protected void writeDeclaration () throws IOException {
OutputFormat format = getOutputFormat ();
String encoding = format. getEncoding ();
If (! Format. isSuppressDeclaration ()){
Writer. write ("<? Xml version = \ "1.0 \"");
If (! Format. isOmitEncoding ()){
If (encoding. equals ("UTF8 "))
Writer. write ("encoding = \" UTF-8 \"");
Else
Writer. write ("encoding = \" "+ encoding + "\"");
}
Writer. write ("standalone = \" true \"");
Writer. write ("?> ");
If (format. isNewLineAfterDeclaration () {println ();}
}
}
}

How to Use Dom4j to Append content to XML

/**
* Use dom4j to write xml documents
*/
Public void createXml (File file ){

// XML declaration <? Xml version = "1.0" encoding = "UTF-8"?> Automatically added to XML document

// Use the DocumentHelper class to create a document instance (the dom4j API factory class for generating XML document nodes)
Document document = incluenthelper. createDocument ();

// Use the addElement () method to create the root element employees (used to add elements to the XML document)
Element root = document. addElement ("employees ");

// Add the comment "An XML Note" using addComment () in the root element"
Root. addComment ("An XML Note ");

// Add a Processing Instruction Using addProcessingInstruction () in the root element
Root. addProcessingInstruction ("target", "text ");

// Use the addElement () method in the root element to add the employee element.
Element empElem = root. addElement ("employee ");

// Use the addattriee () method to add the id and name attributes to the employee Element
EmpElem. addAttribute ("id", "0001 ");
EmpElem. addAttribute ("name", "wanglp ");

// Add the sex element to the employee Element
Element sexElem = empElem. addElement ("sex ");
// Use setText () to set the text of the sex element
SexElem. setText ("m ");

// Add the age element to the employee element and set the text of the element.
Element ageElem = empElem. addElement ("age ");
AgeElem. setText ("25 ");

// Use the addElement () method in the root element to add the employee element.
Element emp2Elem = root. addElement ("employee ");

... The remaining full text>

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.