import java.io.FileOutputStream;
import org.w3c.dom.Element;

public class XMLWriter {
 public static void main(String args[])
 {
  Element root=new Element("selects");
  Element selectElt=new Element("select");
  Element idElt=new Element("id");
  idElt.addContent("1");
 
  Element valueElt=new Element("name");
  valueElt.addContent("吉林省");
  
  selectElt.addContent(idElt);
  selectElt.addContent(valueElt);
  
  root.addContent(selectElt);
  
  Document doc=new Document(rootElt);
  XMLOutputter out=new XMLOutputter();
  //设置输出的编码
  out.setFormat(Format.getCompactFormat().setEncoding("gb2312"));
  //输出字符串
  String xmlStr=out.outputString(doc);
     System.out.println(xmlStr);
    
     //保存在文件中
     out.output(doc,new FileOutputStream("c:/test.xml"));
 } 
}

posted on 2011-02-20 23:04  魔战  阅读(460)  评论(0编辑  收藏  举报