<?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; Java</title>
	<atom:link href="http://www.havenliu.com/category/java/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>java生成复杂word文档的完美解决方案</title>
		<link>http://www.havenliu.com/java/514.html</link>
		<comments>http://www.havenliu.com/java/514.html#comments</comments>
		<pubDate>Fri, 22 Oct 2010 12:37:16 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=514</guid>
		<description><![CDATA[更新：我比较懒。呃。。。应该说是很懒，其实很不想添加这段文字，但随着越来越多的朋友发邮件问我这个问题，而我又要每封邮件都答复一样的问题，觉得是一件比较悲剧的事情，所有还是来更新一下。
很多朋友照着我说的方法生成的文档都遇到了乱码的问题，我觉得有一定编程经验的都曾经遇到乱码，这应该属于老问题，当然解决方法也很多，Google一下一般都能解决【这也是我不想来更新的原因】。要解决这个Demo的乱码问题。只需要在java输出文件流的时候转下编码就行了。将DoucmentHandler.java第45行改成如下方式：out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), &#8220;UTF-8&#8243;));当然你得根据你的实际开发环境来设置编码格式，不一定非得是UTF-8。
客户要求用程序生成标准的word文档，要能打印，而且不能变形，以前用过很多解决方案，都在客户严格要求下牺牲的无比惨烈。
POI读word文档还行，写文档实在不敢恭维，复杂的样式很难控制不提，想象一下一个20多页，嵌套很多表格和图像的word文档靠POI来写代码输出，对程序员来说比去山西挖煤还惨，况且文档格式还经常变化。
iText操作Excel还行。对于复杂的大量的word也是噩梦。
直接通过JSP输出样式基本不达标，而且要打印出来就更是惨不忍睹。
Word从2003开始支持XML格式，用XML还做就很简单了。
大致的思路是先用office2003或者2007编辑好word的样式，然后另存为xml，将xml翻译为FreeMarker模板，最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准，样式、内容控制非常便利，打印也不会变形，生成的文档和office中编辑文档完全一样。
看看实际效果：
首先用office【版本要2003以上，以下的不支持xml格式】编辑文档的样式，图中红线的部分就是我要输出的部分span id=&#8221;more-514&#8243;>
将编辑好的文档另存为XML
再用Firstobject free XML editor【Firstobject free XML editor的使用见这里】将xml中我们需要填数据的地方打上FreeMarker标记【FreeMarker的语法见这里】
最后生成的文档样式
主要程序代码：

?View Code JAVApackage com.havenliu.document;
&#160;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
&#160;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
&#160;
public class DocumentHandler &#123;
	private Configuration configuration = null;
&#160;
	public DocumentHandler&#40;&#41; &#123;
		configuration = new Configuration&#40;&#41;;
		configuration.setDefaultEncoding&#40;&#34;utf-8&#34;&#41;;
	&#125;
&#160;
	public void createDoc&#40;&#41; &#123;
		//要填入模本的数据文件
		Map dataMap=new HashMap&#40;&#41;;
		getData&#40;dataMap&#41;;
		//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		//这里我们的模板是放在com.havenliu.document.template包下面
		configuration.setClassForTemplateLoading&#40;this.getClass&#40;&#41;, &#34;/com/havenliu/document/template&#34;&#41;;
		Template t=null;
		try &#123;
			//test.ftl为要装载的模板
			t = configuration.getTemplate&#40;&#34;test.ftl&#34;&#41;;
		&#125; catch &#40;IOException [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff9900;">更新：我比较懒。呃。。。应该说是很懒，其实很不想添加这段文字，但随着越来越多的朋友发邮件问我这个问题，而我又要每封邮件都答复一样的问题，觉得是一件比较悲剧的事情，所有还是来更新一下。</span></p>
<p><span style="color: #ff9900;">很多朋友照着我说的方法生成的文档都遇到了乱码的问题，我觉得有一定编程经验的都曾经遇到乱码，这应该属于老问题，当然解决方法也很多，Google一下一般都能解决【这也是我不想来更新的原因】。要解决这个Demo的乱码问题。只需要在java输出文件流的时候转下编码就行了。将DoucmentHandler.java第45行改成如下方式：out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), &#8220;UTF-8&#8243;));当然你得根据你的实际开发环境来设置编码格式，不一定非得是UTF-8。</span></p>
<p>客户要求用程序生成标准的word文档，要能打印，而且不能变形，以前用过很多解决方案，都在客户严格要求下牺牲的无比惨烈。</p>
<p>POI读word文档还行，写文档实在不敢恭维，复杂的样式很难控制不提，想象一下一个20多页，嵌套很多表格和图像的word文档靠POI来写代码输出，对程序员来说比去山西挖煤还惨，况且文档格式还经常变化。</p>
<p>iText操作Excel还行。对于复杂的大量的word也是噩梦。</p>
<p>直接通过JSP输出样式基本不达标，而且要打印出来就更是惨不忍睹。</p>
<p>Word从2003开始支持XML格式，用XML还做就很简单了。</p>
<p>大致的思路是先用office2003或者2007编辑好word的样式，然后另存为xml，将xml翻译为FreeMarker模板，最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准，样式、内容控制非常便利，打印也不会变形，生成的文档和office中编辑文档完全一样。</p>
<p>看看实际效果：</p>
<p>首先用office<span style="color: #ff6600;">【版本要2003以上，以下的不支持xml格式】</span>编辑文档的样式，图中红线的部分就是我要输出的部分：
<a href="http://www.havenliu.com/wp-content/gallery/blogpic/doc1.jpg" title="" rel="lightbox[singlepic389]" >
	<img class="ngg-singlepic" src="http://www.havenliu.com/wp-content/gallery/cache/389__640x480_doc1.jpg" alt="doc1" title="doc1" />
</a>
<span id="more-514"></span></p>
<p>将编辑好的文档另存为XML<br />

<a href="http://www.havenliu.com/wp-content/gallery/blogpic/doc2.jpg" title="" rel="lightbox[singlepic390]" >
	<img class="ngg-singlepic" src="http://www.havenliu.com/wp-content/gallery/cache/390__640x480_doc2.jpg" alt="doc2" title="doc2" />
</a>
</p>
<p>再用Firstobject free XML editor<span style="color: #ff6600;">【Firstobject free XML editor的使用见<a href="http://www.havenliu.com/java/511.html">这里</a>】</span>将xml中我们需要填数据的地方打上FreeMarker标记<span style="color: #ff6600;">【FreeMarker的语法见<a href="http://www.havenliu.com/goodarticle/420.html">这里</a>】</span><br />

<a href="http://www.havenliu.com/wp-content/gallery/blogpic/doc3.jpg" title="" rel="lightbox[singlepic391]" >
	<img class="ngg-singlepic" src="http://www.havenliu.com/wp-content/gallery/cache/391__640x480_doc3.jpg" alt="doc3" title="doc3" />
</a>
</p>
<p>最后生成的文档样式<br />

<a href="http://www.havenliu.com/wp-content/gallery/blogpic/doc4.jpg" title="" rel="lightbox[singlepic392]" >
	<img class="ngg-singlepic" src="http://www.havenliu.com/wp-content/gallery/cache/392__640x480_doc4.jpg" alt="doc4" title="doc4" />
</a>
<br />
主要程序代码：</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('p514code2'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5142"><td class="code" id="p514code2"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.havenliu.document</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedWriter</span><span style="color: #339933;">;</span>
<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.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStreamWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Writer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">freemarker.template.Configuration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">freemarker.template.Template</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">freemarker.template.TemplateException</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> DocumentHandler <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Configuration configuration <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> DocumentHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		configuration <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Configuration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		configuration.<span style="color: #006633;">setDefaultEncoding</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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createDoc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//要填入模本的数据文件</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Map</span></a> dataMap<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%3Ahashmap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">HashMap</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getData<span style="color: #009900;">&#40;</span>dataMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，</span>
		<span style="color: #666666; font-style: italic;">//这里我们的模板是放在com.havenliu.document.template包下面</span>
		configuration.<span style="color: #006633;">setClassForTemplateLoading</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;/com/havenliu/document/template&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Template t<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>
			<span style="color: #666666; font-style: italic;">//test.ftl为要装载的模板</span>
			t <span style="color: #339933;">=</span> configuration.<span style="color: #006633;">getTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.ftl&quot;</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: #666666; font-style: italic;">//输出文档路径及名称</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> outFile <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><span style="color: #0000ff;">&quot;D:/temp/outFile.doc&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%3Awriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Writer</span></a> out <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>
			out <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%3Abufferedwriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedWriter</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aoutputstreamwriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">OutputStreamWriter</span></a><span style="color: #009900;">&#40;</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>outFile<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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%3Afilenotfoundexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileNotFoundException</span></a> e1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e1.<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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			t.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>dataMap, out<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>TemplateException 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;">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>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 注意dataMap里存放的数据Key值要与模板中的参数相对应
	 * @param dataMap
	 */</span>
	 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> getData<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Map</span></a> dataMap<span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#123;</span>
		  dataMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;author&quot;</span>, <span style="color: #0000ff;">&quot;张三&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  dataMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;remark&quot;</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%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a>
 _table1<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%3Aarraylist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ArrayList</span></a>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		  Table1 t1<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Table1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  t1.<span style="color: #006633;">setDate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2010-10-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  t1.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;制定10月开发计划内容。&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  _table1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		  Table1 t2<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Table1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  t2.<span style="color: #006633;">setDate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2010-10-2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  t2.<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>
		  _table1.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		  dataMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;table1&quot;</span>, _table1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		  <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a>
 _table2<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%3Aarraylist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ArrayList</span></a>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		  <span style="color: #009900;">&#123;</span>
			  Table2 _t2<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Table2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  _t2.<span style="color: #006633;">setDetail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;测试开发计划&quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  _t2.<span style="color: #006633;">setPerson</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;张三——&quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  _t2.<span style="color: #006633;">setBegindate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2010-10-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  _t2.<span style="color: #006633;">setFinishdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2010-10-31&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  _t2.<span style="color: #006633;">setRemark</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>
			  _table2.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>_t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
		  dataMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;table2&quot;</span>, _table2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>完整源代码下载<span style="color: #ff6600;">【是eclipse工程，直接导入即可运行】</span>：<a href="http://www.havenliu.com/download/undelete/Document.rar">下载</a></p>
<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/514.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>2009年07月2日 -- <a href="http://www.havenliu.com/java/92.html" title="Java生成XML文档的基本操作">Java生成XML文档的基本操作</a></li><li>2009年06月2日 -- <a href="http://www.havenliu.com/java/12.html" title="再次遇到a different object with the same identifier value was already associated with the session错误">再次遇到a different object with the same identifier value was already associated with the session错误</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/514.html/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>XML文件编辑和格式化的利器：Firstobject free XML editor</title>
		<link>http://www.havenliu.com/java/511.html</link>
		<comments>http://www.havenliu.com/java/511.html#comments</comments>
		<pubDate>Thu, 21 Oct 2010 10:54:20 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=511</guid>
		<description><![CDATA[一直为XML文件的排版问题头痛。现在发现一个好工具Firstobject free XML editor，是专门用来编辑XML文件的。软件很小巧，速度很快，最关键是格式化XML文件的效果非常好，而且还免费，强烈推荐。
用Firstobject free XML editor格式化xml文件的方法是在tool-&#62;preferences里。设置Indent为Tabs，然后打开你要格式化的XML文件，选择Tools下的Indent【或者按快捷键F8】就搞定。左边是XML的文档结构，右边是文档内容，很清晰。
官方下载地址：Firstobject free XML editor
相关文章2009年07月29日 -- Dom4j生成XML文件，并解决乱码问题2009年07月2日 -- Java生成XML文档的基本操作]]></description>
			<content:encoded><![CDATA[<p>一直为XML文件的排版问题头痛。现在发现一个好工具Firstobject free XML editor，是专门用来编辑XML文件的。软件很小巧，速度很快，最关键是格式化XML文件的效果非常好，而且还免费，强烈推荐。</p>
<p>用Firstobject free XML editor格式化xml文件的方法是在tool-&gt;preferences里。设置Indent为Tabs，然后打开你要格式化的XML文件，选择Tools下的Indent【或者按快捷键F8】就搞定。左边是XML的文档结构，右边是文档内容，很清晰。</p>
<p>官方下载地址：<a href="http://www.firstobject.com/dn_editor.htm" target="_blank">Firstobject free XML editor</a></p>

<a href="http://www.havenliu.com/wp-content/gallery/blogpic/firstobject1.jpg" title="" rel="lightbox[singlepic388]" >
	<img class="ngg-singlepic" src="http://www.havenliu.com/wp-content/gallery/cache/388__640x480_firstobject1.jpg" alt="firstobject1" title="firstobject1" />
</a>

<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/511.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>2009年07月29日 -- <a href="http://www.havenliu.com/java/165.html" title="Dom4j生成XML文件，并解决乱码问题">Dom4j生成XML文件，并解决乱码问题</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/511.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>struts2.1中include取不到参数的问题</title>
		<link>http://www.havenliu.com/java/310.html</link>
		<comments>http://www.havenliu.com/java/310.html#comments</comments>
		<pubDate>Tue, 02 Mar 2010 05:21:24 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=310</guid>
		<description><![CDATA[Struts2.1中，用
&#60;s:include value=&#8221;top.jsp&#8221;&#62;
&#60;s:param name=&#8221;menuid&#8221;&#62;123&#60;/s:param&#62;
&#60;/s:include&#62;
方式include页面，在top.jsp中通过&#60;s:property value=&#8221;#request.menuid&#8221;/&#62;或者&#60;s:property value=&#8221;%{#attr.menuid}&#8221;/&#62;均不能获取到menuid的值，但用&#60;%=request.getparameter(&#8221;menuid&#8221;)%&#62;的方式可以获取到.如果在地址栏直接输入top.jsp?menuid=123这种方式访问是可以获取到的。由于Struts2.1不再支持 EL表达式语言，不知道用${param.menuid}这种方式行不行。简单查阅了一些资料，没有得出结论，估计是OGNL本身的缺陷，没在深入研究。希望Struts的后续版本中能修正这个问题。
随机文章2009年11月2日 -- 十二大著名法则2009年06月21日 -- 哲学寓言2011年12月9日 -- 发现个不错的网盘：酷盘2009年09月13日 -- 再度杀回淘宝2009年06月30日 -- 惠普前总裁孙振耀指点职业规划（二）]]></description>
			<content:encoded><![CDATA[<p>Struts2.1中，用<br />
&lt;s:include value=&#8221;top.jsp&#8221;&gt;<br />
&lt;s:param name=&#8221;menuid&#8221;&gt;123&lt;/s:param&gt;<br />
&lt;/s:include&gt;<br />
方式include页面，在top.jsp中通过&lt;s:property value=&#8221;#request.menuid&#8221;/&gt;或者&lt;s:property value=&#8221;%{#attr.menuid}&#8221;/&gt;均不能获取到menuid的值，但用&lt;%=request.getparameter(&#8221;menuid&#8221;)%&gt;的方式可以获取到.如果在地址栏直接输入top.jsp?menuid=123这种方式访问是可以获取到的。由于Struts2.1不再支持 EL表达式语言，不知道用${param.menuid}这种方式行不行。简单查阅了一些资料，没有得出结论，估计是OGNL本身的缺陷，没在深入研究。希望Struts的后续版本中能修正这个问题。</p>
<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/310.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>2011年02月17日 -- <a href="http://www.havenliu.com/goodarticle/609.html" title="【网摘】6岁女童每天捉虫捡米粒养鸡以攒钱交学费">【网摘】6岁女童每天捉虫捡米粒养鸡以攒钱交学费</a></li><li>2011年10月9日 -- <a href="http://www.havenliu.com/android/656.html" title="cocos2d-x中，CCSAXParser的一个小问题">cocos2d-x中，CCSAXParser的一个小问题</a></li><li>2009年07月14日 -- <a href="http://www.havenliu.com/bike/145.html" title="2009年第96届环法自行车赛第九、十站视频">2009年第96届环法自行车赛第九、十站视频</a></li><li>2009年06月30日 -- <a href="http://www.havenliu.com/goodarticle/61.html" title="惠普前总裁孙振耀指点职业规划（一）">惠普前总裁孙振耀指点职业规划（一）</a></li><li>2009年09月7日 -- <a href="http://www.havenliu.com/goodarticle/224.html" title="加菲猫经典语录五十条(转载)">加菲猫经典语录五十条(转载)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/310.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MyEclipse 7.5,MyEclipse 8.0中安装Flex插件</title>
		<link>http://www.havenliu.com/java/280.html</link>
		<comments>http://www.havenliu.com/java/280.html#comments</comments>
		<pubDate>Fri, 11 Dec 2009 03:15:43 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[MyEclipse]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=280</guid>
		<description><![CDATA[不可否认MyEclipse现在越做越强大（毕竟是收费的嘛），强大到开始排斥第三方插件，这有些不符合Eclipse的初衷，Eclipse就是靠良好的扩展性发展起来的。
MyEclipse从7.5开始（7.0没用过，不知道），完全改变了程序结构，现在扩展第三方插件 再也没有以前那么方便了，特别是link方式，这对程序员来说是致命的。Flex Plugin 安装时需要选择Eclipse目录，7.5中根本没有Eclipse目录，所以想直接在myeclipse中安装flex plugin是不太可能。我试过将flex里的features和plugins目录拷贝到myeclipse中进行强制安装，还是失败，所以最后只能将Eclipse,Flex plugin和myeclipse分开进行安装。
MyEclipse 7.5对应的Eclipse 版本为3.4，先到www.Eclipse.org官网去下载Eclipse3.4的版本。然后再到Myeclipse官网（已屏蔽大陆IP，需代理，但下载可以用迅雷直接下）去下载增量升级包，注意，一定要是增量升级包，即archived update site.zip 这个包，不能下载集成安装包，flex builder plugin 的版本也必须为3.02，这一点很重要，Adobe官网已经很明确的说明了3.0本版存在bug，不能再Eclipse3.4  中安装，已经下载3.0本版的可以到Adobe官网或google 中去下载补丁。安装顺序是，先解压Eclipse3.4 ，然后安装 Flex plugin ，和以前的安装方法一样，选好目录，直接下一步就行，然后再通过Eclipse的Help-&#62;Software Updates-&#62;Available Software -&#62;Add site-&#62;Archive安装刚才下载的MyEclipse7.5 的增量升级包。如果你RP不是特别差的话，应该就大功告成了,Enjoy this !
同样，MyEclipse8.0中的Flex plugin 也可以这样安装。需要说明的是MyEclipse8.0 对应的Eclipse版本是3.5 。Eclipse3.5 对应的Flex plugin为4.0.目前Flex plugin 4.0的正式版还没出，还是Beta 版 ，Flex builder4 已经正式更名为 Flash builder4了 ,还是喜欢Flex builder的命名，感觉更加专业点，哈哈！
发现现在自己真懒，很不想写东西。难得敲几个字出来，装载请注明出处，致谢！
相关文章2010年03月31日 -- 自己写的一个Flex3多文件上传组件2009年11月29日 -- MyEclipse 8没有启动画面的问题2009年08月14日 -- 发贴器有了比较大的进展]]></description>
			<content:encoded><![CDATA[<p>不可否认MyEclipse现在越做越强大（毕竟是收费的嘛），强大到开始排斥第三方插件，这有些不符合Eclipse的初衷，Eclipse就是靠良好的扩展性发展起来的。</p>
<p>MyEclipse从7.5开始（7.0没用过，不知道），完全改变了程序结构，现在扩展第三方插件 再也没有以前那么方便了，特别是link方式，这对程序员来说是致命的。Flex Plugin 安装时需要选择Eclipse目录，7.5中根本没有Eclipse目录，所以想直接在myeclipse中安装flex plugin是不太可能。我试过将flex里的features和plugins目录拷贝到myeclipse中进行强制安装，还是失败，所以最后只能将Eclipse,Flex plugin和myeclipse分开进行安装。</p>
<p>MyEclipse 7.5对应的Eclipse 版本为3.4，先到www.Eclipse.org官网去下载Eclipse3.4的版本。然后再到Myeclipse官网（已屏蔽大陆IP，需代理，但下载可以用迅雷直接下）去下载增量升级包，注意，一定要是增量升级包，即archived update site.zip 这个包，不能下载集成安装包，flex builder plugin 的版本也必须为3.02，这一点很重要，Adobe官网已经很明确的说明了3.0本版存在bug，不能再Eclipse3.4  中安装，已经下载3.0本版的可以到Adobe官网或google 中去下载补丁。安装顺序是，先解压Eclipse3.4 ，然后安装 Flex plugin ，和以前的安装方法一样，选好目录，直接下一步就行，然后再通过Eclipse的Help-&gt;Software Updates-&gt;Available Software -&gt;Add site-&gt;Archive安装刚才下载的MyEclipse7.5 的增量升级包。如果你RP不是特别差的话，应该就大功告成了,Enjoy this !</p>
<p>同样，MyEclipse8.0中的Flex plugin 也可以这样安装。需要说明的是MyEclipse8.0 对应的Eclipse版本是3.5 。Eclipse3.5 对应的Flex plugin为4.0.目前Flex plugin 4.0的正式版还没出，还是Beta 版 ，Flex builder4 已经正式更名为 Flash builder4了 ,还是喜欢Flex builder的命名，感觉更加专业点，哈哈！</p>
<p>发现现在自己真懒，很不想写东西。难得敲几个字出来，装载请注明<a title="原文出自“夜半难眠”" href="http://www.havenliu.com/java/280.html">出处</a>，致谢！</p>
<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/280.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年03月31日 -- <a href="http://www.havenliu.com/web/327.html" title="自己写的一个Flex3多文件上传组件">自己写的一个Flex3多文件上传组件</a></li><li>2009年11月29日 -- <a href="http://www.havenliu.com/java/275.html" title="MyEclipse 8没有启动画面的问题">MyEclipse 8没有启动画面的问题</a></li><li>2009年08月14日 -- <a href="http://www.havenliu.com/java/191.html" title="发贴器有了比较大的进展">发贴器有了比较大的进展</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/280.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MyEclipse 8没有启动画面的问题</title>
		<link>http://www.havenliu.com/java/275.html</link>
		<comments>http://www.havenliu.com/java/275.html#comments</comments>
		<pubDate>Mon, 30 Nov 2009 02:59:49 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MyEclipse]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=275</guid>
		<description><![CDATA[一直不能忍受Myeclipse7.5的龟速启动，最后还是忍痛换成了8.0，但安装后发现MyEclipse8.0没有以前的启动画面，对我这种有追求的完美的性格缺陷的人来说，这是不可忍受的。还好广大IT民工朋友的智慧的无限的，在网上一查，早有解决方法：只要在安装目录common里将splash.bmp图片搜出来，放到Myeclipse的启动文件所在目录，然后再修改myeclipse.ini文件，在文件最后一行加上下面这句话：-Dosgi.splashLocation=splash.bmp（myeclipse.ini和splash.bmp在同一目录，你还可以将splash.bmp换成其他图片，比如饭岛爱的。。。不知道有没有未成年看我的Blog。罪过啊罪过。。。）。搞定！
PS：MyEclipse8.0的启动貌似确实比7.5略快，不过现在MyEclipse集成的越来越臃肿，很多东西都是自己不用的，非常耗资源，电脑没2G的内存，还是慎选！
相关文章2009年12月10日 -- MyEclipse 7.5,MyEclipse 8.0中安装Flex插件]]></description>
			<content:encoded><![CDATA[<p>一直不能忍受Myeclipse7.5的龟速启动，最后还是忍痛换成了8.0，但安装后发现MyEclipse8.0没有以前的启动画面，对我这种有追求的完美的性格缺陷的人来说，这是不可忍受的。还好广大IT民工朋友的智慧的无限的，在网上一查，早有解决方法：只要在安装目录common里将splash.bmp图片搜出来，放到Myeclipse的启动文件所在目录，然后再修改myeclipse.ini文件，在文件最后一行加上下面这句话：-Dosgi.splashLocation=splash.bmp（myeclipse.ini和splash.bmp在同一目录，你还可以将splash.bmp换成其他图片，比如饭岛爱的。。。不知道有没有未成年看我的Blog。罪过啊罪过。。。）。搞定！</p>
<p>PS：MyEclipse8.0的启动貌似确实比7.5略快，不过现在MyEclipse集成的越来越臃肿，很多东西都是自己不用的，非常耗资源，电脑没2G的内存，还是慎选！</p>
<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/275.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>2009年12月10日 -- <a href="http://www.havenliu.com/java/280.html" title="MyEclipse 7.5,MyEclipse 8.0中安装Flex插件">MyEclipse 7.5,MyEclipse 8.0中安装Flex插件</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/275.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySql access denied for user错误</title>
		<link>http://www.havenliu.com/java/266.html</link>
		<comments>http://www.havenliu.com/java/266.html#comments</comments>
		<pubDate>Tue, 03 Nov 2009 06:51:48 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=266</guid>
		<description><![CDATA[怪自己学艺不精，今天遇到MySql远程连接时的“access denied for user **@**”错误，搞的我很头大，后来查出来解决方法。记录一下，怕以后再忘记：
首先本地登陆MySQL，然后执行这两句代码：GRANT ALL PRIVILEGES ON *.* TO root@&#8217;%&#8217; IDENTIFIED BY &#8216;000000&#8242;;
FLUSH PRIVILEGES;
格式：grant 权限 on 数据库名.表名 用户@登录主机 identified by &#8220;用户密码&#8221;;
参数说明：  ALL PRIVILEGES表示赋给远程登录用户的权限，ALL PRIVILEGES表示所有的权限，你也可以单独或组合赋select,update,insert,delete权限；*.*：第一个*表示要赋权的数据库名，*当然表示全部数据库了，第二个*表示数据库下的表名，同理，*表示全部表，像我这样的懒人当然就直接用*.*了，反正都是自己开发用（不要BS我）。。。。root表示要赋权的用户；%表示远程登录的IP，如果要限制登录IP的话，这里就添你允许登录的IP，比如192.18.1.99等，%表示不限制IP（再次偷懒），000000是用户远程登录的密码。就这么简单。这句运行以后再运行FLUSH PRIVILEGES，搞定！
“Host &#8216;XXX&#8217; is not allowed to connect to this MySQL server”这错误貌似也可以这样搞定。
随机文章2009年07月20日 -- Picasa被和谐了2009年06月30日 -- 惠普前总裁孙振耀指点职业规划（二）2009年09月1日 -- 【骑单车看世界】骑行徽杭古道（二）2011年05月12日 -- 总算回到成都2011年04月26日 -- 此文无标题]]></description>
			<content:encoded><![CDATA[<p>怪自己学艺不精，今天遇到MySql远程连接时的“access denied for user **@**”错误，搞的我很头大，后来查出来解决方法。记录一下，怕以后再忘记：<br />
首先本地登陆MySQL，然后执行这两句代码：GRANT ALL PRIVILEGES ON *.* TO root@&#8217;%&#8217; IDENTIFIED BY &#8216;000000&#8242;;<br />
FLUSH PRIVILEGES;<br />
格式：grant 权限 on 数据库名.表名 用户@登录主机 identified by &#8220;用户密码&#8221;;<br />
参数说明：  ALL PRIVILEGES表示赋给远程登录用户的权限，ALL PRIVILEGES表示所有的权限，你也可以单独或组合赋select,update,insert,delete权限；*.*：第一个*表示要赋权的数据库名，*当然表示全部数据库了，第二个*表示数据库下的表名，同理，*表示全部表，像我这样的懒人当然就直接用*.*了，反正都是自己开发用（不要BS我）。。。。root表示要赋权的用户；%表示远程登录的IP，如果要限制登录IP的话，这里就添你允许登录的IP，比如192.18.1.99等，%表示不限制IP（再次偷懒），000000是用户远程登录的密码。就这么简单。这句运行以后再运行FLUSH PRIVILEGES，搞定！</p>
<p>“Host &#8216;XXX&#8217; is not allowed to connect to this MySQL server”这错误貌似也可以这样搞定。</p>
<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/266.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>2009年07月2日 -- <a href="http://www.havenliu.com/other/59.html" title="网络上收集的搞笑签名">网络上收集的搞笑签名</a></li><li>2010年07月11日 -- <a href="http://www.havenliu.com/mylife/465.html" title="西湖的荷花">西湖的荷花</a></li><li>2009年07月9日 -- <a href="http://www.havenliu.com/bike/139.html" title=" 2009年第96届环法自行车赛第六站视频"> 2009年第96届环法自行车赛第六站视频</a></li><li>2009年11月29日 -- <a href="http://www.havenliu.com/java/275.html" title="MyEclipse 8没有启动画面的问题">MyEclipse 8没有启动画面的问题</a></li><li>2010年05月27日 -- <a href="http://www.havenliu.com/web/446.html" title="Jquery html(val)使用注意">Jquery html(val)使用注意</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/266.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>发贴器有了比较大的进展</title>
		<link>http://www.havenliu.com/java/191.html</link>
		<comments>http://www.havenliu.com/java/191.html#comments</comments>
		<pubDate>Sat, 15 Aug 2009 03:18:41 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Demo版]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[发贴器]]></category>
		<category><![CDATA[工作流]]></category>
		<category><![CDATA[流程]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=191</guid>
		<description><![CDATA[上次在整体测试的时候发现了一个致命的BUG，是框架性的问题，就把整个程序的框架结构调整了一下，现在的框架模式比以前更加优化了，理论上程序的运行速度应该更快（没测过，根据理论推测的），减少了数据库的访问量在50%以上，如果并发数多的话，这可以大大提高系统性能，减少数据库压力。最开始的BUG就是数据库方面的问题，并发数太多会耗尽数据库资源，最后系统宕机。

最近工作状态不佳。工作进度很慢。感觉生活的压力太大。这次的框架调整应该可以在一个星期内完成的，我花了半个多月。
每次坐在电脑面前整个人都是茫然的，不知道该干什么，很多时候会订这空荡荡的桌面发呆，强打起精神写也写不上几行代码。这对程序员来是真的是要命。
朋友昨天还要我和他一起做一套OA系统，他给我看了他的朋友现在做的一套OA，老实说技术方面这套系统做的很不错了，工作流都有了。流程的定义还是用Flex做的，只是这套系统还缺少了商业性的包装，我不知道开发这系统的兄弟花了多少时间来完成这套系统，我很佩服他。如果以我现在的状态来开发这套系统，最起码要一年半以上，因为我早了半年前就在谋划着要自己开发一套OA，但谋划了半年也还没见动手，到是别人先做出来了。再次证明了我的执行能力太差，打击不小。
貌似和标题离的太远了。。。。
不管怎样，生活还是得继续，强打起精神来吧，争取在一个月内可以让发贴器的Demo版出来。
相关文章2010年03月31日 -- 自己写的一个Flex3多文件上传组件2009年12月10日 -- MyEclipse 7.5,MyEclipse 8.0中安装Flex插件]]></description>
			<content:encoded><![CDATA[<p>上次在整体测试的时候发现了一个致命的BUG，是框架性的问题，就把整个程序的框架结构调整了一下，现在的框架模式比以前更加优化了，理论上程序的运行速度应该更快（没测过，根据理论推测的），减少了数据库的访问量在50%以上，如果并发数多的话，这可以大大提高系统性能，减少数据库压力。最开始的BUG就是数据库方面的问题，并发数太多会耗尽数据库资源，最后系统宕机。<br />
<span id="more-191"></span></p>
<p>最近工作状态不佳。工作进度很慢。感觉生活的压力太大。这次的框架调整应该可以在一个星期内完成的，我花了半个多月。</p>
<p>每次坐在电脑面前整个人都是茫然的，不知道该干什么，很多时候会订这空荡荡的桌面发呆，强打起精神写也写不上几行代码。这对程序员来是真的是要命。</p>
<p>朋友昨天还要我和他一起做一套OA系统，他给我看了他的朋友现在做的一套OA，老实说技术方面这套系统做的很不错了，工作流都有了。流程的定义还是用Flex做的，只是这套系统还缺少了商业性的包装，我不知道开发这系统的兄弟花了多少时间来完成这套系统，我很佩服他。如果以我现在的状态来开发这套系统，最起码要一年半以上，因为我早了半年前就在谋划着要自己开发一套OA，但谋划了半年也还没见动手，到是别人先做出来了。再次证明了我的执行能力太差，打击不小。</p>
<p>貌似和标题离的太远了。。。。</p>
<p>不管怎样，生活还是得继续，强打起精神来吧，争取在一个月内可以让发贴器的Demo版出来。</p>
<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/191.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年03月31日 -- <a href="http://www.havenliu.com/web/327.html" title="自己写的一个Flex3多文件上传组件">自己写的一个Flex3多文件上传组件</a></li><li>2009年12月10日 -- <a href="http://www.havenliu.com/java/280.html" title="MyEclipse 7.5,MyEclipse 8.0中安装Flex插件">MyEclipse 7.5,MyEclipse 8.0中安装Flex插件</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/191.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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('p165code5'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1655"><td class="code" id="p165code5"><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('p165code6'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1656"><td class="code" id="p165code6"><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>
		<item>
		<title>Java生成XML文档的基本操作</title>
		<link>http://www.havenliu.com/java/92.html</link>
		<comments>http://www.havenliu.com/java/92.html#comments</comments>
		<pubDate>Thu, 02 Jul 2009 11:45:11 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jdom]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=92</guid>
		<description><![CDATA[利用JDOM生成XML文档，JDOM.jar的官方下载地址：点击这里，官方网址是http://www.jdom.org，下面代码可以下载后直接执行：

?View Code JAVApackage com.haven.reply.serviceimpl;
&#160;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
&#160;
public class JavaXMLDaoImpl &#123;
&#160;
	public void createXMLDoc&#40;&#41; &#123;
		// 创建根节点 root;
		Element root = new Element&#40;&#34;root&#34;&#41;;
&#160;
		// 根节点添加到文档中； 根节点是唯一的
		Document Doc = new Document&#40;root&#41;;
&#160;
		// 下面开始向XML文档中插入数据
		// 创建节点elemets1;
		Element elements1 = new Element&#40;&#34;elemets1&#34;&#41;;
&#160;
		// 给 elemets 节点添加属性 name;
		elements1.setAttribute&#40;&#34;name&#34;, &#34;test1&#34;&#41;;
&#160;
		// 给 elemets 节点添加子节点并赋值；
		elements1.addContent&#40;new Element&#40;&#34;note1-1&#34;&#41;.setText&#40;&#34;value1-1&#34;&#41;&#41;;
		elements1.addContent&#40;new Element&#40;&#34;note1-2&#34;&#41;.setText&#40;&#34;value1-2&#34;&#41;&#41;;
		// 添加一个注释
		elements1.addContent&#40;new Comment&#40;&#34;This is Comment!!!!&#34;&#41;&#41;;
		elements1.addContent&#40;new Element&#40;&#34;note1-3&#34;&#41;.setText&#40;&#34;value1-3&#34;&#41;&#41;;
&#160;
		// 给父节点list添加user子节点;
		root.addContent&#40;elements1&#41;;
&#160;
		// 继续创建节点elemets2;方法和步骤和elemets1完全一样
		Element elements2 = [...]]]></description>
			<content:encoded><![CDATA[<p>利用JDOM生成XML文档，JDOM.jar的官方下载地址：<a href="http://www.jdom.org/downloads/source.html">点击这里</a>，官方网址是http://www.jdom.org，下面代码可以下载后直接执行：</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('p92code9'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p929"><td class="code" id="p92code9"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.haven.reply.serviceimpl</span><span style="color: #339933;">;</span>
&nbsp;
<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.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdom.Comment</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdom.Document</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdom.Element</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdom.output.Format</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdom.output.XMLOutputter</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> JavaXMLDaoImpl <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createXMLDoc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// 创建根节点 root;</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> <span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 根节点添加到文档中； 根节点是唯一的</span>
		<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> Doc <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%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a><span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 下面开始向XML文档中插入数据</span>
		<span style="color: #666666; font-style: italic;">// 创建节点elemets1;</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> elements1 <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%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;elemets1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 给 elemets 节点添加属性 name;</span>
		elements1.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;test1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 给 elemets 节点添加子节点并赋值；</span>
		elements1.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note1-1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value1-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		elements1.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note1-2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value1-2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 添加一个注释</span>
		elements1.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Comment<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is Comment!!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		elements1.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note1-3&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value1-3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 给父节点list添加user子节点;</span>
		root.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span>elements1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 继续创建节点elemets2;方法和步骤和elemets1完全一样</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> elements2 <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%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;elemets2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 给 elemets 节点添加属性 name;</span>
		elements2.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;test2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 给 elemets 节点添加子节点并赋值；</span>
		elements2.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note2-1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value2-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		elements2.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note2-2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value2-2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		elements2.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Comment<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is Comment!!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		elements2.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</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><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;note2-3&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value2-3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		root.<span style="color: #006633;">addContent</span><span style="color: #009900;">&#40;</span>elements2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 下面是美化工作，如果不进行排版，生成的XML全部在一行显示。可读性很差</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Format</span></a> format <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Format</span></a>.<span style="color: #006633;">getCompactFormat</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>
		format.<span style="color: #006633;">setIndent</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> <span style="color: #666666; font-style: italic;">// 缩进4个空格后换行</span>
&nbsp;
		XMLOutputter XMLOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLOutputter<span style="color: #009900;">&#40;</span>format<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// test.xml为生成的xml文档名</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			XMLOut.<span style="color: #006633;">output</span><span style="color: #009900;">&#40;</span>Doc, <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><span style="color: #0000ff;">&quot;test.xml&quot;</span><span style="color: #009900;">&#41;</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%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;">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>
&nbsp;
	<span style="color: #009900;">&#125;</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> 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>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			JavaXMLDaoImpl j2x <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JavaXMLDaoImpl<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%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;生成 mxl 文件...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			j2x.<span style="color: #006633;">createXMLDoc</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%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</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>
&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('p92code10'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9210"><td class="code" id="p92code10"><pre class="xml" style="font-family:monospace;">&nbsp;
        value1-1
        value1-2
        <span style="color: #808080; font-style: italic;">&lt;!--This is Comment!!!!--&gt;</span>
        value1-3
&nbsp;
        value2-1
        value2-2
        <span style="color: #808080; font-style: italic;">&lt;!--This is Comment!!!!--&gt;</span>
        value2-3</pre></td></tr></table></div>

<p>下次有时间再把JDOM解析XML的代码写一下，今天先到这里<br />
欢迎转载，转载时请指明出处！</p>
<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/92.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月22日 -- <a href="http://www.havenliu.com/java/514.html" title="java生成复杂word文档的完美解决方案">java生成复杂word文档的完美解决方案</a></li><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月29日 -- <a href="http://www.havenliu.com/java/165.html" title="Dom4j生成XML文件，并解决乱码问题">Dom4j生成XML文件，并解决乱码问题</a></li><li>2009年06月2日 -- <a href="http://www.havenliu.com/java/12.html" title="再次遇到a different object with the same identifier value was already associated with the session错误">再次遇到a different object with the same identifier value was already associated with the session错误</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/java/92.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>再次遇到a different object with the same identifier value was already associated with the session错误</title>
		<link>http://www.havenliu.com/java/12.html</link>
		<comments>http://www.havenliu.com/java/12.html#comments</comments>
		<pubDate>Wed, 03 Jun 2009 04:24:15 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hiberate]]></category>
		<category><![CDATA[错误]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=12</guid>
		<description><![CDATA[这错误以前遇到过一次，我是这样解决的：
正常情况一般调用hiberate端方法进行数据库操作时，是直接调用 getHibernateTemplate().update(user);，但在hibernate中同一个session里面有了两个相同标识但是是 不同实体时就会出现如标题的错误，在网上找来如下解决方法，错误是解决了，但不完美：
public void update(User user) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.clear();
session.update(user);
}
这 样在session.update(user)后如果还有其他对数据库的save或update并且是在同一个事务中，后面的操作可能就会无效，具体原因 没查出来，可能是getcurrentSession()获取的是“当前”session,调用getcurrentSession()时“当前 ”session被从hibernate中剥离，所以导致了后面的操作无效。
如果是struts+hibernate+spring框架下面有个很简单的方法：直接调用getHibernateTemplate().merge(user)。一切搞定！
相关文章2010年10月22日 -- java生成复杂word文档的完美解决方案2009年07月2日 -- Java生成XML文档的基本操作]]></description>
			<content:encoded><![CDATA[<p>这错误以前遇到过一次，我是这样解决的：<br />
正常情况一般调用hiberate端方法进行数据库操作时，是直接调用 getHibernateTemplate().update(user);，但在hibernate中同一个session里面有了两个相同标识但是是 不同实体时就会出现如标题的错误，在网上找来如下解决方法，错误是解决了，但不完美：<br />
public void update(User user) {</p>
<p>Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();<br />
session.clear();<br />
session.update(user);</p>
<p>}<br />
这 样在session.update(user)后如果还有其他对数据库的save或update并且是在同一个事务中，后面的操作可能就会无效，具体原因 没查出来，可能是getcurrentSession()获取的是“当前”session,调用getcurrentSession()时“当前 ”session被从hibernate中剥离，所以导致了后面的操作无效。<br />
如果是struts+hibernate+spring框架下面有个很简单的方法：直接调用getHibernateTemplate().merge(user)。一切搞定！</p>
<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/12.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月22日 -- <a href="http://www.havenliu.com/java/514.html" title="java生成复杂word文档的完美解决方案">java生成复杂word文档的完美解决方案</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/12.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

