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. }
  • 相关阅读:
    [iOS微博项目
    [iOS微博项目
    [iOS微博项目
    [iOS微博项目
    [iOS微博项目
    [iOS微博项目
    [iOS微博项目
    COS Javascript 上传图片putObject成功后没有返回ETag?
    模拟一个在线音乐播放程序(socket + 数据库)
    python 基础(while 循环、格式化输出、运算符、编码初识)
  • 原文地址:https://www.cnblogs.com/duanxz/p/2615242.html
Copyright © 2011-2022 走看看