XML files generated Java application examples

/**
 * desciption:java create xml file
 * Author: maomao
 * datetime:2007/04/04 23:42
 */
 
package com.xh.xml;
 
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
 
public   class  Java2XML {
 
     public  void BuildXMLDoc()  throws IOException, JDOMException {
 
       // Create a root node list;
        Element root =  new Element("list");
      
       // root node added to the document;
        Document Doc =  new Document(root);
 
       // This loop can be replaced for traversing the result set of database table operation;
        for ( int i = 0; i < 5; i++) {
          
           // Create a node user;
           Element elements =  new Element("user");
          
           // add attributes to the user node id;
           elements.setAttribute("id", "" + i);
          
           // add a child node to the user node and assignment;
           // new Element ( "name") in the "name" field in the corresponding table replace, in setText ( "xuehui") "xuehui replaced record values ​​in the table;
           elements.addContent( new Element("name").setText("xuehui"));
           elements.addContent( new Element("age").setText("28"));
           elements.addContent( new Element("sex").setText("Male"));
 
           // add a user to a parent node list of child nodes;
           root.addContent(elements);
 
       }
        XMLOutputter XMLOut =  new XMLOutputter();
      
       // output user.xml file;
        XMLOut.output(Doc,  new FileOutputStream("user.xml"));
    }
 
     public  static  void main(String[] args) {
        try {
           Java2XML j2x =  new Java2XML();
           . System OUT .println ( "generate mxl file ...");
           j2x.BuildXMLDoc();
       }  catch (Exception e) {
           e.printStackTrace ();
       }
    }
 
}
 
 
Files generated user.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<list>
    <user id="0">
        <name>xuehui</name>
        <age>28</age>
        <sex>Male</sex>
    </user>
    <user id="1">
        <name>xuehui</name>
        <age>28</age>
        <sex>Male</sex>
    </user>
    <user id="2">
        <name>xuehui</name>
        <age>28</age>
        <sex>Male</sex>
    </user>
    <user id="3">
        <name>xuehui</name>
        <age>28</age>
        <sex>Male</sex>
    </user>
    <user id="4">
        <name>xuehui</name>
        <age>28</age>
        <sex>Male</sex>
    </user>
</list>
 https://www.cnblogs.com/liudianjia/p/12513320.html
https://www.cnblogs.com/liudianjia/p/12513272.html
https://www.cnblogs.com/liudianjia/p/12495990.html
https://www.cnblogs.com/liudianjia/p/12488902.html
https://www.cnblogs.com/liudianjia/p/12484952.html
https://www.cnblogs.com/liudianjia/p/12479110.html

Guess you like

Origin www.cnblogs.com/liudianjia/p/12525888.html