dom4j jar创建XML

来源:互联网 发布:pos机s90如何设置网络 编辑:程序博客网 时间:2024/05/29 18:32
 
package cn.nahan.cd.tool;import java.io.File;import java.io.FileWriter;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;public class XMLtool {public XMLtool() {// TODO Auto-generated constructor stubsuper();}public static void createApplicationConfigXML(String filePath,String fileName) throws Exception {// 建立document对象Document document = DocumentHelper.createDocument();// 文档对象Element root = document.addElement("root");// 添加文档根root.addComment("微信xml通信");// 加入一行注释Element request = root.addElement("index"); // 添加root的子节点// 添加元素节点Element puttype = request.addElement("puttype");puttype.addText("1");// 添加元素节点Element text = request.addElement("text");text.addText("我们的歌曲");// 添加元素节点Element pictext = request.addElement("pictext");pictext.addText(" ");// 添加元素节点Element picture = request.addElement("picture");picture.addText("http://localhost:8085/upload/");// 添加元素节点Element video = request.addElement("video");video.addText("http://localhost:8085/upload/");// 根据需要设置编码OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");// 输出全部原始数据,在编译器中显示/* * XMLWriter writer = new XMLWriter(System.out, format); * document.normalize(); writer.write(document); writer.close(); */// 输出全部原始数据,并用它生成新的我们需要的XML文件XMLWriter writer2 = new XMLWriter(new FileWriter(new File(filePath+ fileName + ".xml")), format);// 输出到文件writer2.write(document);// 关闭流writer2.close();}public static void main(String[] args) throws Exception {String pathString = "E:\\Users\\WZ\\workspace\\wxt_manage\\WebContent\\xml\\";String fileName = "index";XMLtool.createApplicationConfigXML(pathString, fileName);}}

0 0