[分享]jdom生成xml文档_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2099 | 回复: 0   主题: [分享]jdom生成xml文档        下一篇 
nengqiang.wang
注册用户
等级:中校
经验:1658
发帖:34
精华:0
注册:1970-1-1
状态:离线
发送短消息息给nengqiang.wang 加好友    发送短消息息给nengqiang.wang 发消息
发表于: IP:您无权察看 2016-2-1 10:09:43 | [全部帖] [楼主帖] 楼主

上一篇简单些了jdom解析xml文档,现在利用jdom生成一份xml文档 。当然需要上一篇中提到的Book.java,books.xml以及jdom2.0.6.zip.

生成xml文档代码:

CreateXMLFile.java

package com.wnq.sk.jdomXML01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class CreateXMLFile
{    
    public static void main(String[] args)throws Exception
    {
        List<Book> books = JdomReadXMLFile.jdomReadXMLFile("books.xml");
        String fileName = "jdom_create_books.xml";
        createXMLFile(books, fileName);
    }    
    public static void createXMLFile(List<Book> books, String fileName)
    {
        // 创建根节点
        Element bookstoreElement = new Element("bookstore");
        Document document = new Document(bookstoreElement);
        // 创建book
        for(int i=0;i<books.size();i++){
            Element bookElement = new Element("book");
            bookElement.setAttribute(new Attribute("category", books.get(i).getCategory()));
            Element titleElement = new Element("title");
            titleElement.setAttribute(new Attribute("lang", books.get(i).getTitleLang()));
            titleElement.setText(books.get(i).getTitle());
            bookElement.addContent(titleElement);
            String[] author = books.get(i).getAuthor().split("/");
            for (int j = 0; j < author.length; j++)
            {
                Element authorElement = new Element("author");
                authorElement.setText(author[j]);
                bookElement.addContent(authorElement);
                
            }
            Element yearElement = new Element("year");
            yearElement.setText(books.get(i).getYear().toString());
            bookElement.addContent(yearElement);
            Element priceElement = new Element("price");
            priceElement.setText(books.get(i).getPrice().toString());
            bookElement.addContent(priceElement);
            bookstoreElement.addContent(bookElement);
        }
        XMLOutputter xmlOutputter = new XMLOutputter(FormatXML());
        try
        {
            xmlOutputter.output(document, new FileOutputStream(fileName));
            System.out.println("JDOM生成XML文件成功");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    // 格式化
    public static Format FormatXML()
    {
        Format format = Format.getCompactFormat();
        format.setEncoding("utf-8");
        format.setIndent(" ");
        return format;
    }
}

生成的jdom_create_books.xml文档

<?xml version="1.0" encoding="utf-8"?>
<bookstore>
 <book category="cooking">
  <title>Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.0</price>
 </book>
 <book category="children">
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
 </book>
 <book category="web">
  <title>XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
 </book>
 <book category="web">
  <title>Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
 </book>
</bookstore>




人们都向往和寻找快乐,其实快乐就是一种心情,秘密隐藏于人的心中。快乐从不曾远离我们,而是我们远离了快乐。原本单纯的心,一旦复杂起来,快乐和幸福就 会远离而去,烦恼和忧愁就会随之而来。






赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论