java 生成XML文檔

package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Java2XML {  
    public void BuildXMLDoc() throws IOException, JDOMException {   
       // 創建根節點 list;   
        Element root = new Element("version");   
       // 根節點添加到文檔中;   
        Document Doc = new Document(root);   
        
        // 創建節點 param;   
        Element elements = new Element("param");   
        // 給 param 節點添加屬性 probability;   
        elements.setAttribute("probability", "100");
        Element element1 = new Element("sss");   
        elements.addContent(element1);
        root.addContent(elements);  

//        // 創建節點 trunck;   
//        Element element = new Element("sss");   
//        // 給 trunck 節點添加屬性 ;   
//        element.setAttribute("name", "ppstream_111216.exe");  
//        element.setAttribute("link", "pps://3iaapywqea.pps/ppstream_111216.exe");  
//        element.setAttribute("md5", "CD9C1E58D9EE40AE1DBE9323F669B896");  
//        element.setAttribute("ver", "1.0");  
        
        // 創建節點 FILE;   
        Element fileElement = new Element("FILE");   
        // 給 FILE 節點添加屬性 ;   
        fileElement.setAttribute("name", "PPStream.exe");  
        fileElement.setAttribute("ver", "2.7.0.1364");  
//        element.addContent(fileElement);  
//        root.addContent(element);  
        
//        root.getContent(1).getDocument().addContent(fileElement);
       Format format = Format.getPrettyFormat();
       format.setEncoding("ISO-8859-1");
       XMLOutputter XMLOut = new XMLOutputter(format);
       
       System.out.println(Doc.getRootElement().addContent(fileElement));
       // 輸出 version.xml 文件;  
        XMLOut.output(Doc, new FileOutputStream("version.xml"));  
        

    }

    public static void main(String[] args) {  

       try {  

           Java2XML j2x = new Java2XML();  

           System.out.println("生成 mxl 文件...");  

           j2x.BuildXMLDoc();  

       } catch (Exception e) {  

           e.printStackTrace();  

       }  

    }  

}  

發佈了35 篇原創文章 · 獲贊 5 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章