主程序大概思路就是这样,以credit标签外边套一层cbrc标签为例演示下我的想法:- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import org.jdom.*;
- import org.jdom.output.XMLOutputter;
- public class XmlFileGen {
- public static void main(String[] args){
- try{
- Element root = new Element("cbrc");
- root.setAttribute(new Attribute("reportnumber","01"));
- root.setAttribute(new Attribute("bankcode","XXXXXXXXXXXXXXXXX"));
- root.setAttribute(new Attribute("bankname","XX银行"));
- root.setAttribute(new Attribute("reportdate","201303"));
- root.setAttribute(new Attribute("version",""));
- Document document = new Document(root);
- Element basicElement = new Element("credit");
- Element customerType = new Element("customerType").addContent("1");
- Element customerName = new Element("customerName").addContent("XX能源集团");
- Element customerCode = new Element("customerCode").addContent("XX-XXXX");
- Element nationalityCode = new Element("nationalityCode").addContent("CHN");
- basicElement.addContent(customerType);
- basicElement.addContent(customerName);
- basicElement.addContent(customerCode);
- basicElement.addContent(nationalityCode);
- root.addContent(basicElement);
- org.jdom.output.Format format=org.jdom.output.Format.getPrettyFormat();
- XMLOutputter output = new XMLOutputter(format.setIndent(" "));
- OutputStream os = new FileOutputStream(new File("dd.xml"));
- output.output(document, os);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
复制代码 生成XML结果如下:- <?xml version="1.0" encoding="UTF-8"?>
- <cbrc reportnumber="01" bankcode="XXXXXXXXXXXXXXXXX" bankname="XX银行" reportdate="201303" version="">
- <credit>
- <customerType>1</customerType>
- <customerName>XX能源集团</customerName>
- <customerCode>XX-XXXX</customerCode>
- <nationalityCode>CHN</nationalityCode>
- </credit>
- </cbrc>
复制代码 这样生成一个简单的XML文件当然没有问题,但实际上一个XML文件里边经常会有几万个标签。。。用这样的方法生成XML文件非常容易就会溢出了,那我们该如何解决呢?
|