您的位置:首页 > 编程语言 > Java开发

java使用jdom生成xml格式文件

2017-03-07 13:55 288 查看


本文生成xml使用的工具是jdom.jar,下载地址如下:
链接:https://eyun.baidu.com/s/3slyHgnj 密码:0TXF

import java.io.FileOutputStream;  

import java.io.IOException;  

import org.jdom.Document;  

import org.jdom.Element;  

import org.jdom.JDOMException;  

import org.jdom.output.Format;  

import org.jdom.output.XMLOutputter;  

public class Java2XMLNew {  

    public void BuildXMLDoc() throws IOException, JDOMException {     

        // 创建根节点 并设置它的属性 ;     

        Element root = new Element("book");  

        // 将根节点添加到文档中;     

        Document Doc = new Document(root);   

        root.addContent(new Element("java").setText("java"));    

        root.addContent(new Element("hadoop").setText("hadoop"));    

        root.addContent(new Element("mysql").setText("mysql"));    

        root.addContent(new Element("AJAX").setText("AJAX"));   

        root.addContent(new Element("Jquery").setText("Jquery"));    

        root.addContent(new Element("oracle").setText("oracle"));   

        root.addContent(new Element("mongodb").setText("mongodb"));   

        // 输出 books.xml 文件;    

        // 使xml文件 缩进效果  

        Format format = Format.getPrettyFormat();  

        XMLOutputter XMLOut = new XMLOutputter(format);  

        XMLOut.output(Doc, new FileOutputStream("D:/books.xml"));  

    }   

    public static void main(String[] args) {    

       try {    

           Java2XMLNew j2x = new Java2XMLNew();    

           System.out.println("正在生成xml文件");    

           j2x.BuildXMLDoc();    

       } catch (Exception e) {    

           e.printStackTrace();    

       }    

       System.out.println("D:/books.xml 文件生成成功");  

    }    

}  
生成之后的文档格式类型,就如上面的图片一样,简单吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: