zoukankan      html  css  js  c++  java
  • jdom生成xml文件

    Java代码  收藏代码
    1. import java.io.File;  
    2. import java.io.FileOutputStream;  
    3. import java.io.IOException;  
    4.   
    5. import org.jdom.Attribute;  
    6. import org.jdom.Document;  
    7. import org.jdom.Element;  
    8. import org.jdom.output.Format;  
    9. import org.jdom.output.XMLOutputter;  
    10.   
    11. public class generateXML {  
    12.   
    13.  public static void main(String[] args) throws IOException {  
    14.   
    15.   Document doc = new Document(); // 创建空白文档  
    16.   
    17.   Element root = new Element("Root"); // 创建一个元素  
    18.   doc.setRootElement(root); // 将该元素做为根元素  
    19.   
    20.   Element element = new Element("elementA");  
    21.   root.addContent(element); // 将product做为productsDetails的子元素  
    22.   
    23.   Attribute att = new Attribute("attA""中文"); // 创建属性  
    24.   element.setAttribute(att); // 为product设置属性  
    25.   
    26.   // 为product创建子元素,并将其content分别设为100.00,red  
    27.   element.addContent(new Element("childA").setText("100"));  
    28.   element.addContent(new Element("childB").setText("200"));  
    29.   
    30.   /* 
    31.    * 格式化输出 
    32.    */  
    33.   File file = new File("result.xml");  
    34.   XMLOutputter outp = new XMLOutputter();// 用于输出jdom 文档  
    35.   Format format = Format.getPrettyFormat(); // 格式化文档  
    36.   format.setEncoding("UTF-8"); // 设置编码格式为utf-8  
    37.   outp.setFormat(format);  
    38.   outp.output(doc, new FileOutputStream(file)); // 输出文档  
    39.   System.out.println("out put file done!");  
    40.  }  
    41. }
  • 相关阅读:
    问题:关于抛出例外的一个问题。
    向北京球迷致敬!!!
    [下载]高质量C++C编程指南
    WinCE.NET中播放声音
    WINCE.NET中程序只运行一次
    解决vs2003开发PDA(wince.net4.2)调试与部署问题
    WinCE.NET中设置系统日期时间
    网页上发送mail(PHP)
    点阵字库预览工具 V1.0.0
    WINCE.NET4.2下如何获取程序当前运行目录
  • 原文地址:https://www.cnblogs.com/duanxz/p/2615242.html
Copyright © 2011-2022 走看看