你可以用jdom创建,如果你愿意用它,我给你示例代码:
/*
* Created on 2005/10/20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXml {public static void main(String[] args) throws IOException {
String xmlpath = "test.xml";
Document doc = new Document();
Element root = new Element("root");
doc.setRootElement(root);
Element tongzhanbu = new Element("tongzhanbu");
root.addContent(tongzhanbu);
tongzhanbu.addContent(new Element("ganbuke").setText("ganbuke"));
tongzhanbu.addContent(new Element("mike").setText("mike"));
Element zhongjiao = new Element("zongjiao");
tongzhanbu.addContent(zhongjiao);
zhongjiao.addContent(new Element("zongjiaoke").setText("zongjiaoke"));
zhongjiao.addContent(new Element("fojiao").setText("fojiao"));
tongzhanbu.addContent(new Element("sheke").setText("sheke"));
for(int m=0;m<10;m++){
Element taiban = new Element("taiwai");
root.addContent(taiban);
taiban.addContent(new Element("bangong").setText("bangong"));
taiban.addContent(new Element("taibaojie").setText("taibaojie"));
}
XMLOutputter outp = new XMLOutputter();
Format format=Format.getPrettyFormat();
format.setEncoding("GB2312");
outp.setFormat(format);
outp.output(doc,new FileOutputStream(xmlpath));
}
}
/*
* Created on 2005/10/20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXml {public static void main(String[] args) throws IOException {
String xmlpath = "test.xml";
Document doc = new Document();
Element root = new Element("root");
doc.setRootElement(root);
Element tongzhanbu = new Element("tongzhanbu");
root.addContent(tongzhanbu);
tongzhanbu.addContent(new Element("ganbuke").setText("ganbuke"));
tongzhanbu.addContent(new Element("mike").setText("mike"));
Element zhongjiao = new Element("zongjiao");
tongzhanbu.addContent(zhongjiao);
zhongjiao.addContent(new Element("zongjiaoke").setText("zongjiaoke"));
zhongjiao.addContent(new Element("fojiao").setText("fojiao"));
tongzhanbu.addContent(new Element("sheke").setText("sheke"));
for(int m=0;m<10;m++){
Element taiban = new Element("taiwai");
root.addContent(taiban);
taiban.addContent(new Element("bangong").setText("bangong"));
taiban.addContent(new Element("taibaojie").setText("taibaojie"));
}
XMLOutputter outp = new XMLOutputter();
Format format=Format.getPrettyFormat();
format.setEncoding("GB2312");
outp.setFormat(format);
outp.output(doc,new FileOutputStream(xmlpath));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货