dom4j写xml文件

引用
dom4j写xml文件


package com.hugui.drp.util;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class TestXMLWriter {

	public static void main(String[] args){
		Document doc = DocumentHelper.createDocument();
		Element rootElt = doc.addElement("items");
		Element itemElt = rootElt.addElement("item");
		Element idElt  = itemElt.addElement("id");
		idElt.setText("10023");
		Element nameElt = itemElt.addElement("name");
		nameElt.setText("吉林省");
		String xmlString = doc.asXML();
		System.out.println(xmlString);
		
//		try {
//			OutputFormat format = OutputFormat.createPrettyPrint();
//			format.setEncoding("gbk");
//			XMLWriter xmlWriter = new XMLWriter(new FileWriter("D:\\test.xml"), format);
//			xmlWriter.write(doc);
//			xmlWriter.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
}

你可能感兴趣的:(java,xml)