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"));
 } 
}

相关文章:

  • 2021-08-05
  • 2021-10-02
  • 2021-12-06
  • 2021-12-13
  • 2021-06-12
  • 2022-01-18
  • 2022-01-01
猜你喜欢
  • 2022-02-18
  • 2021-09-24
  • 2021-11-09
  • 2021-06-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案