码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j读取xml

时间:2014-11-21 20:32:20      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   sp   java   for   

代码如下

<span style="font-size:18px;">import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Utils {

	
	// 获得代表 xml 文档 的 document 对象
	public static Document getDocument() {
		
		SAXReader reader = new SAXReader();
		
		try {
			return reader.read(new File("e:\\person.xml"));
		} catch (DocumentException e) {
			
			e.printStackTrace();
			throw new RuntimeException("对不起, 获得 xml 文件 失败 ");
		}
	}

	public static void write2Xml(Document rootElement) throws Exception {
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
	
		XMLWriter writer = new XMLWriter(new FileOutputStream("e:\\person.xml"), format);
		writer.write(rootElement);
		writer.close();
	}

}
</span>

详细地址:http://java.662p.com/thread-3804-1-1.html

dom4j读取xml

标签:style   blog   http   io   ar   os   sp   java   for   

原文地址:http://blog.csdn.net/wenzhilanyu2012/article/details/41356907

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!