<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>夜半难眠 &#187; DOM4J</title>
	<atom:link href="http://www.havenliu.com/tag/dom4j/feed" rel="self" type="application/rss+xml" />
	<link>http://www.havenliu.com</link>
	<description>记录点生活的无奈</description>
	<lastBuildDate>Thu, 29 Dec 2011 03:25:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dom4j生成XML文件，并解决乱码问题</title>
		<link>http://www.havenliu.com/java/165.html</link>
		<comments>http://www.havenliu.com/java/165.html#comments</comments>
		<pubDate>Wed, 29 Jul 2009 15:25:49 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[DOM4J]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[乱码]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=165</guid>
		<description><![CDATA[上次写过一段JDOM生成XML文件的例子（文章在这里），其实java操作xml文件还能用dom4j。
首先要下载DOM4J的jar包，可以去DOM4J的官网去下，地址是www.dom4j.org，如果会翻墙的也可以去sourceforge下载


?View Code JAVApackage com.havenliu.blog;
&#160;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
&#160;
public class Dom4jXmlOper &#123;
&#160;
	public static void createXml&#40;File file&#41;
	&#123;
		XMLWriter writer = null;
		SAXReader reader = new SAXReader&#40;&#41;;
&#160;
		OutputFormat format = OutputFormat.createPrettyPrint&#40;&#41;;
		format.setEncoding&#40;&#34;utf-8&#34;&#41;;//设置XML文件的编码格式,如果有中文可设置为GBK或UTF-8
&#160;
		Document _document = DocumentHelper.createDocument&#40;&#41;;
		Element _root = _document.addElement&#40;&#34;userinfo&#34;&#41;;
		Element user = _root.addElement&#40;&#34;user&#34;&#41;;
		user.addAttribute&#40;&#34;id&#34;, &#34;001&#34;&#41;;
		Element name = user.addElement&#40;&#34;name&#34;&#41;;
		name.setText&#40;&#34;张三&#34;&#41;;
		Element age = user.addElement&#40;&#34;age&#34;&#41;;
		age.setText&#40;&#34;28&#34;&#41;;
		Element sex = user.addElement&#40;&#34;sex&#34;&#41;;
		sex.setText&#40;&#34;男&#34;&#41;;
		Element email = user.addElement&#40;&#34;email&#34;&#41;;
		email.setText&#40;&#34;abc@abc.com&#34;&#41;;
&#160;
		//如果上面设置的xml编码类型为GBK，则应当用FileWriter来构建xml文件，否则会出现中文连码问题
		/*
		 [...]]]></description>
			<content:encoded><![CDATA[<p>上次写过一段JDOM生成XML文件的例子（文章在<a class="wpGallery" title="DOM生成XML文档" href="http://www.havenliu.com/java/92.html" target="_blank">这里</a>），其实java操作xml文件还能用dom4j。</p>
<p>首先要下载DOM4J的jar包，可以去DOM4J的官网去下，地址是<a title="www.dom4j.org" href="www.dom4j.org" target="_blank">www.dom4j.org</a>，如果会翻墙的也可以去<a title="http://sourceforge.net/project/showfiles.php?group_id=16035" href="http://sourceforge.net/project/showfiles.php?group_id=16035" target="_blank">sourceforge</a>下载</p>
<p><span id="more-165"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p165code3'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1653"><td class="code" id="p165code3"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.havenliu.blog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileNotFoundException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.UnsupportedEncodingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.Document</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.DocumentHelper</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.Element</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.io.OutputFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.io.SAXReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.io.XMLWriter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dom4jXmlOper <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> createXml<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> file<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		XMLWriter writer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		SAXReader reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SAXReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		OutputFormat format <span style="color: #339933;">=</span> OutputFormat.<span style="color: #006633;">createPrettyPrint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		format.<span style="color: #006633;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//设置XML文件的编码格式,如果有中文可设置为GBK或UTF-8</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> _document <span style="color: #339933;">=</span> DocumentHelper.<span style="color: #006633;">createDocument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> _root <span style="color: #339933;">=</span> _document.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;userinfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> user <span style="color: #339933;">=</span> _root.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		user.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span>, <span style="color: #0000ff;">&quot;001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> name <span style="color: #339933;">=</span> user.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		name.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;张三&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> age <span style="color: #339933;">=</span> user.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		age.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;28&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> sex <span style="color: #339933;">=</span> user.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sex&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sex.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;男&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> email <span style="color: #339933;">=</span> user.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		email.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abc@abc.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//如果上面设置的xml编码类型为GBK，则应当用FileWriter来构建xml文件，否则会出现中文连码问题</span>
		<span style="color: #666666; font-style: italic;">/*
		 try {
			writer = new XMLWriter(new FileWriter(file), format);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		*/</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//如果上面设置的xml编码类型为utf-8，则应当用FileOutputStream来构建xml文件，否则还是会出现乱码问题</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afileoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileOutputStream</span></a> fos <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			fos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afileoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileOutputStream</span></a><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afilenotfoundexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileNotFoundException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLWriter<span style="color: #009900;">&#40;</span>fos, format<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aunsupportedencodingexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">UnsupportedEncodingException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>_document<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			writer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> filePath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;d:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>user.xml&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//生产的XML文件位置</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>filePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Dom4jXmlOper.<span style="color: #006633;">createXml</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>生成好以后的xml文件格式：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p165code4'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1654"><td class="code" id="p165code4"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;userinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;001&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>张三<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>28<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>男<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;email<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>abc@abc.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/email<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/userinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="http://www.havenliu.com/java/165.html"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div><h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2010年10月21日 -- <a href="http://www.havenliu.com/java/511.html" title="XML文件编辑和格式化的利器：Firstobject free XML editor ">XML文件编辑和格式化的利器：Firstobject free XML editor </a></li><li>2009年07月2日 -- <a href="http://www.havenliu.com/java/92.html" title="Java生成XML文档的基本操作">Java生成XML文档的基本操作</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/165.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

