用jdom创建中文的xml文件的方法
程序员文章站
2022-06-22 17:09:59
实例如下:package test;import java.io.file;public class domtest {private string outfile = "c:\abc\peopl...
实例如下:
package test; import java.io.file; public class domtest { private string outfile = "c:\\abc\\people.xml"; public static void main(string[] args) { new domtest(); } public domtest() { try { file file = new file(outfile); if(!file.exists()){ file.createnewfile(); } javax.xml.parsers.documentbuilder builder = javax.xml.parsers.documentbuilderfactory.newinstance() .newdocumentbuilder(); org.w3c.dom.document doc = builder.newdocument(); org.w3c.dom.element root = doc.createelement("老师"); org.w3c.dom.element wang = doc.createelement("苍"); org.w3c.dom.element liu = doc.createelement("刘"); wang.appendchild(doc.createtextnode("我是苍老师")); root.appendchild(wang); root.appendchild(liu); doc.appendchild(root); javax.xml.transform.transformer transformer = javax.xml.transform.transformerfactory.newinstance() .newtransformer(); transformer.setoutputproperty(javax.xml.transform.outputkeys.encoding, "gb2312"); transformer.setoutputproperty(javax.xml.transform.outputkeys.indent, "yes"); transformer.transform(new javax.xml.transform.dom.domsource(doc), new javax.xml.transform.stream.streamresult(outfile)); } catch (exception e) { system.out.println(e.getmessage()); } } }
以上这篇用jdom创建中文的xml文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。