xml生成

xml生成,原来是因为可视化树需要这个xml,所以写了这个程序来为那个可视化applet制作显示数据用。

import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

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


public class CopyOfProcess_sql {
	public static void main(String[] args) throws Exception {
		CopyOfProcess_sql b = new CopyOfProcess_sql();
		System.out.println("now we build an xml doc");
		b.buildXml();
		
	}
	
	public void buildXml() throws Exception {
		// generate root
		Element root = new Element("tree");
		Document doc = new Document(root);
		// generate node1
		Element root1 = new Element("declarations");
		Element root12 = new Element("attributeDecl");
		Attribute root1Attr = new Attribute("name", "name");
		Attribute root1Attr1 = new Attribute("type", "String");
		root12.setAttribute(root1Attr);
		root12.setAttribute(root1Attr1);
		
		Element root13 = new Element("attributeDecl");
		Attribute root2Attr = new Attribute("name", "title");
		Attribute root2Attr1 = new Attribute("type", "String");
		root13.setAttribute(root2Attr);
		root13.setAttribute(root2Attr1);
		
//		Element root14 = new Element("attributeDecl");
//		Attribute root4Attr = new Attribute("name", "id");
//		Attribute root4Attr1 = new Attribute("type", "String");
//		root14.setAttribute(root4Attr);
//		root14.setAttribute(root4Attr1);
		
		root1.addContent(root12);
		root1.addContent(root13);
//		root1.addContent(root14);
		root.addContent(root1);
		
		
		Element root4 = new Element("branch");
		Element root41 = new Element("attribute");
		Attribute root3Attr = new Attribute("name", "name");
		Attribute root3Attr1 = new Attribute("value", "Categories");
		root41.setAttribute(root3Attr);
		root41.setAttribute(root3Attr1);
		root4.addContent(root41);

//这里可以根据需求加入很多:root4.addContent(root21);root21.addContent(root22);root22.addContent(root23);等
		root.addContent(root4);

		Format format = Format.getCompactFormat();
		format.setEncoding("gb2312");
		format.setIndent("   ");
		XMLOutputter outp = new XMLOutputter(format);
		outp.output(doc, new FileOutputStream("result7.xml"));
	}

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值