JAVA 读取数据库字段并生成xml

原创
2016/11/15 09:57
阅读数 734

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.epoint.ZtbCommon.ZtbCommonDao;
import com.epoint.core.grammar.Record;

/**
 * 读取数据库表信息生成xml文件
 *
 * @作者 lulf
 * @version [版本号, 2016年11月15日]
 */
public class Java2XML
{

    public static void main(String[] args) {
        //建立数据库链接(公司方法)
        ZtbCommonDao service = ZtbCommonDao.getInstance();
        try {
            Java2XML java2xml = new Java2XML();
            System.out.println("正在生成XML文件");
            String FilePath = "D://NEWXML.xml";
            String biaoduanguid = "a0e22cdb-4602-487e-a45d-d3aa1756c55b";
            java2xml.BuildXml(FilePath, biaoduanguid, service);
        }
        catch (Exception e) {
            e.getStackTrace();
            System.out.println("XML文件生成失败");
        }
        service.close();
        System.out.println("XML文件已生成");
    }

    public void BuildXml(String FilePath, String BiaoDuanGuid, ZtbCommonDao service)
            throws FileNotFoundException, IOException {
        String sql = "select * from pingbiao_kaibiaotoubiao where biaoduanguid='" + BiaoDuanGuid + "'";
        List<Record> tmp = service.findList(sql, Record.class);
        Java2XmlModel model = new Java2XmlModel();
        for (int i = 0; i < tmp.size(); i++) {
            model.add(tmp.get(i).getStr("danweiname"), tmp.get(i).getStr("danweiguid"));
        }
        // 创建根节点 并设置它的属性 ;
        Element root = new Element("danweis").setAttribute("count", tmp.size() + "");
        // 将根节点添加到文档中;
        Document Doc = new Document(root);
        for (int i = 0; i < tmp.size(); i++) {
            // 创建节点 danwei;
            Element elements = new Element("danwei");
            // 给 danwei 节点添加子节点并赋值;
            elements.addContent(new Element("danweiname").setText(model.getStu(i).getDanweiname()));
            elements.addContent(new Element("danweiguid").setText(model.getStu(i).getDanweiguid()));
            root.addContent(elements);
        }
        // 使xml文件 缩进效果
        Format format = Format.getPrettyFormat();
        XMLOutputter XMLOut = new XMLOutputter(format);
        XMLOut.output(Doc, new FileOutputStream(FilePath));
    }
}

class Java2XmlModel
{
    Java2XmlModel[] java2XmlModels;
    int count;
    private String danweiname;
    private String danweiguid;

    public String getDanweiname() {
        return danweiname;
    }

    public String getDanweiguid() {
        return danweiguid;
    }

    public void add(String danweiname, String danweiguid) {
        java2XmlModels[count] = new Java2XmlModel(danweiname, danweiguid);
        count++;
    }

    public Java2XmlModel() {
        java2XmlModels = new Java2XmlModel[100];
    }

    public Java2XmlModel(String danweiname, String danweiguid) {
        super();
        this.danweiname = danweiname;
        this.danweiguid = danweiguid;
    }

    public Java2XmlModel getStu(int index) {
        return java2XmlModels[index];
    }

    public int getCount() {
        return count;
    }
}

 

 

最终生成如下格式

<?xml version="1.0" encoding="UTF-8"?>
<danweis count="3">
  <danwei>
    <danweiname>阿里巴巴</danweiname>
    <danweiguid>7997ea17-c8e4-44fb-8a00-21dd866f354c</danweiguid>
  </danwei>
  <danwei>
    <danweiname>百度</danweiname>
    <danweiguid>7eddbe4d-d554-481a-acba-633ba1afbabb</danweiguid>
  </danwei>
  <danwei>
    <danweiname>腾讯</danweiname>
    <danweiguid>d543dc10-5650-4050-8158-0da2958d78cd</danweiguid>
  </danwei>
</danweis>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部