<?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://code.oseschool.com/index.php/archives/category/java/feed" rel="self" type="application/rss+xml" />
	<link>http://code.oseschool.com</link>
	<description>世界：男人、女人、程序员</description>
	<lastBuildDate>Sun, 07 Mar 2010 02:43:20 +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的正则表达式</title>
		<link>http://code.oseschool.com/index.php/archives/76</link>
		<comments>http://code.oseschool.com/index.php/archives/76#comments</comments>
		<pubDate>Fri, 26 Feb 2010 03:47:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[正则]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/index.php/archives/76</guid>
		<description><![CDATA[1、  Pattern p = Pattern.compile(参数1);
Matcher m = p.matcher(参数2);
参数值分别去null的情况 和 参数值分别取&#8221;"
答:
当把参数1或参数2设置成null的时候,都将会抛出空指针异常,原因如下:
首先了解一下静态方法compile里的源代码为
public static Pattern compile(String regex) {
return new Pattern(regex, 0);
}
该方法返回了一个Pattern实例,那么我们再看看该Pattern的构建函数
private Pattern(String p, int f) {tt
pattern = p;
flags = f;
// Reset group index count
capturingGroupCount = 1;
localCount = 0;
if (pattern.length() &#62; 0) {
compile();
} else {
root = new Start(lastAccept);
matchRoot = lastAccept;
}
}
我们会发现,p是我们刚开始传进来的null值,pattern引用了p
当pattern.length的时候,pattern是个null,空指针异常抛出了.
而matcher方法里和上面的方法情况差不多,依然是执行了给参数取length,造成的空指针异常
当把参数1或参数2传&#8220;&#8221;时,它是一个空字符串,跟其它普通字符串一样.
2、 为什么p.matcher(参数2);要生成Matcher对象。或者说是Matcher有什么用。
答:根据以下的API,我们可以得到答案
public Matcher matcher(CharSequence input)
创建匹配给定输入与此模式的匹配器。
参数：
input &#8211; 要匹配的字符序列。
返回：
此模式的新匹配器。
由此得知,Matcher是一个匹配器,可以执行三种不同的匹配操作,也就是
matches   [...]]]></description>
			<content:encoded><![CDATA[<p>1、  Pattern p = Pattern.compile(参数1);<br />
Matcher m = p.matcher(参数2);<br />
参数值分别去null的情况 和 参数值分别取&#8221;"</p>
<p>答:<br />
当把参数1或参数2设置成null的时候,都将会抛出空指针异常,原因如下:<br />
首先了解一下静态方法compile里的源代码为<br />
public static Pattern compile(String regex) {<br />
return new Pattern(regex, 0);<br />
}<br />
该方法返回了一个Pattern实例,那么我们再看看该Pattern的构建函数<br />
private Pattern(String p, int f) {tt<br />
pattern = p;<br />
flags = f;</p>
<p>// Reset group index count<br />
capturingGroupCount = 1;<br />
localCount = 0;</p>
<p>if (pattern.length() &gt; 0) {<br />
compile();<br />
} else {<br />
root = new Start(lastAccept);<br />
matchRoot = lastAccept;<br />
}<br />
}</p>
<p>我们会发现,p是我们刚开始传进来的null值,pattern引用了p<br />
当pattern.length的时候,pattern是个null,空指针异常抛出了.<br />
而matcher方法里和上面的方法情况差不多,依然是执行了给参数取length,造成的空指针异常</p>
<p>当把参数1或参数2传&#8220;&#8221;时,它是一个空字符串,跟其它普通字符串一样.</p>
<p>2、 为什么p.matcher(参数2);要生成Matcher对象。或者说是Matcher有什么用。</p>
<p>答:根据以下的API,我们可以得到答案<br />
public Matcher matcher(CharSequence input)<br />
创建匹配给定输入与此模式的匹配器。<br />
参数：<br />
input &#8211; 要匹配的字符序列。<br />
返回：<br />
此模式的新匹配器。</p>
<p>由此得知,Matcher是一个匹配器,可以执行三种不同的匹配操作,也就是<br />
matches    尝试将整个输入序列与该模式匹配<br />
find    扫描输入序列以查找与该模式匹配的下一个子序列<br />
lookingAt  尝试将输入序列从头开始与该模式匹配</p>
<p>并且Matcher对象里提供了很多好用的方法,可供我们实现更高级的查找替换</p>
<p>3、正则表达式 &#8220;(bb)?&#8221; 匹配 &#8220;&#8221; ,&#8221;b&#8221;,&#8221;bbb&#8221; 的结果，以及解释。</p>
<p>答:<br />
首先,?号代表的是?号前面的字符串出现零次或一次,<br />
那么&#8221;(bb)?&#8221;表示字符串bb要出现零次或一次<br />
当出现零次的时候,能匹配到的是个空字符串<br />
当出现一次的时候,能匹配到的是&#8221;bb&#8221;字符串<br />
那么答案很快就出来了<br />
匹配&#8221;",答案当然是true<br />
匹配&#8221;b&#8221;,答案false<br />
匹配&#8221;bbb&#8221;,答案false</p>
<p>4、正则表达式写成&#8221;**&#8221; 或者&#8221;x**&#8221;等类似的情况。</p>
<p>答:<br />
*,?,+都是正则里的特殊字符<br />
标准的写法是X*,X不能是一个空字符串,<br />
所以&#8221;**&#8221;或&#8221;x**&#8221;都将会抛出异常</p>
<p>5、有[a-z]的正则表达式，那[z-a] [a-A]或者[A-a]等相类似的表达式。</p>
<p>答:[a-z]表示一个范围,a和z会转换成ASCII码,范围要遵循从小到大的原则</p>
<p>6、字符串 &#8220;&#8221; 匹配&#8221;[a-z&amp;&amp;[ABC]]&#8221; ， 以及其他字符串对此正则表达式的匹配。</p>
<p>答:[a-z&amp;&amp;[ABC]]是取a-z与ABC的交集,他们没有任何交集,所以任何字符都不能与他匹配</p>
<p>7、字符串&#8243;bbc&#8221; 匹配正则表达式 &#8220;b^b.&#8221;、&#8221;b?^b..&#8221;等类似的正则表达式。</p>
<p>答:<br />
首先,我们要明白两点:^号在[]外面,表示一行的开头,而在[]里,表示取反<br />
我们可以用下面的一个等式:<br />
&#8220;b.&#8221;==&#8221;^b.&#8221;<br />
那么&#8221;b^b.&#8221;==&#8221;^b^b.&#8221;,说明是行开头的第一个字符为&#8221;bb&#8221;,但是&#8221;bb&#8221;只能表示为两个字符而非一个字符(字符集里应该没有一个字符为&#8221;bb&#8221;的吧..)<br />
所以任何字符串都不能与它匹配上<br />
而&#8221;b?^b..&#8221;,b?代表b出现零次或多次,匹配器里为了尽量能让字符串&#8243;bbc&#8221;与该模式匹配,把b?设定为出现零次,那么后面的^b..就可以与&#8221;bbc&#8221;相匹配上了</p>
<p>8、email的正则表达式。</p>
<p>答:&#8221;[\w[.-]]+@[\w[.-]]+\.[\w]+&#8221;</p>
<p>9、用正则区分全角半角。</p>
<p>答:全角范围:[uFF00-uFFFF]<br />
半角范围:[^uFF00-uFFFF](也就是全角的范围取反了)</p>
<p>扩充:<br />
汉字的UNICODE编码范围是4e00-9fa5</p>
<p>汉字范围:[u4E00-u9FA5](那如果取反,就表示不是汉字咯)</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/76/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DOM4J官方快速入门（译文）中文详解</title>
		<link>http://code.oseschool.com/index.php/archives/70</link>
		<comments>http://code.oseschool.com/index.php/archives/70#comments</comments>
		<pubDate>Wed, 09 Sep 2009 01:32:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=70</guid>
		<description><![CDATA[官方地址：http://www.dom4j.org/dom4j-1.6.1/guide.html
Dom4j 快速入门
Parsing XML (xml 解析)
以下例子演示了如何使用SAX解析器解析XML，返回一个DOM 模型的对象，即DOCUMENT.
有了DOCUMENT我们就可以进行文档操作了。

import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
&#160;
public class Foo &#123;
&#160;
public Document parse&#40;URL url&#41; throws DocumentException &#123;
	// 以DOM4J默认的SAX解析器解析
        SAXReader reader = new SAXReader&#40;&#41;;
	// read函数的形参可以是url 也可以是 File类型，也可以是STRING类型的相对或绝对路径
        Document document = reader.read&#40;url&#41;;
        return document;
 [...]]]></description>
			<content:encoded><![CDATA[<p>官方地址：http://www.dom4j.org/dom4j-1.6.1/guide.html</p>
<p>Dom4j 快速入门</p>
<p>Parsing XML (xml 解析)</p>
<p>以下例子演示了如何使用SAX解析器解析XML，返回一个DOM 模型的对象，即DOCUMENT.</p>
<p>有了DOCUMENT我们就可以进行文档操作了。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</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.DocumentException</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Document</span> parse<span style="color: #009900;">&#40;</span><span style="color: #003399;">URL</span> url<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> DocumentException <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 以DOM4J默认的SAX解析器解析</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>
	<span style="color: #666666; font-style: italic;">// read函数的形参可以是url 也可以是 File类型，也可以是STRING类型的相对或绝对路径</span>
        <span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> reader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> document<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 Using Iterators（使用迭代器）
该部分演示了如何使用迭代器（迭代器是一种设计模式，可用于遍历如集合、数组等）进行文档的遍历。以下演示了获得三种的迭代器。
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bar<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> DocumentException <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 获得根节点</span>
	<span style="color: #003399;">Element</span> root <span style="color: #339933;">=</span> document.<span style="color: #006633;">getRootElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// i为根节点孩子节点的迭代器</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> root.<span style="color: #006633;">elementIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// do something</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>element.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 名为“student1”节点以下孩子节点的迭代器</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> root.<span style="color: #006633;">elementIterator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;student1&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Element</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// do something</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根节点属性的迭代器</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> root.<span style="color: #006633;">attributeIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Attribute</span> attribute <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Attribute</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// do something</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>attribute.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</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: #009900;">&#125;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>以下是：foo.xml
</p>

<div class="wp_syntax"><div class="code"><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;gb2312&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1111&quot;</span> <span style="color: #000066;">fooname</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;01&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;02&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>20<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student1</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;03&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>小翁<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/student1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Main函数中执行如下命令：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Document</span> doc <span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//1.2 Using Iterators</span>
		<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo.xml&quot;</span><span style="color: #009900;">&#41;</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;">//parse  为以上1.1 部分的方法</span>
			doc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			foo.<span style="color: #006633;">bar</span><span style="color: #009900;">&#40;</span>doc<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>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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></pre></div></div>

<p>结果：<br />
student<br />
student<br />
student1<br />
小翁<br />
1111<br />
test</p>
<p>Powerful Navigation with Xpath(使用Xpth进行导航)</p>
<p>XPath 使用路径表达式来选取 XML 文档中的节点或节点集。</p>
<p>具体可以参见http://www.w3school.com.cn/xpath/xpath_syntax.asp</p>
<p>注意：使用XPATH进行导航时，有时需要导入一个名jaxen-XXX.jar的包,该包可以在	官方DOM4J文件夹lib目录下有。</p>
<p>Dom4j XPath 表达式也以表示文档或文档树中的节点（如属性，元素，以及处理指令）。可以用一行简单的代码进行复杂的文档遍历。如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> barWithXPath<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// “//”表示任意路径节点 如 //name 任意名为name 的节点</span>
		List<span style="color: #339933;">&lt;</span>Node<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> document.<span style="color: #006633;">selectNodes</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>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Node nodeTmp <span style="color: #339933;">:</span> list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>nodeTmp.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</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: #666666; font-style: italic;">//&quot;//foo/student[2]&quot; 表示带有foo路径下第二个student节点</span>
		Node node <span style="color: #339933;">=</span> document.<span style="color: #006633;">selectSingleNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;//foo/student[2]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// “@”表示属性</span>
		<span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> node.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@sn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>以下是：foo.xml</p>

<div class="wp_syntax"><div class="code"><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;gb2312&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1111&quot;</span> <span style="color: #000066;">fooname</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;01&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;02&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>20<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student1</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;03&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>小翁<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/student1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Main函数中执行如下命令：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> doc <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//1.3 Powerful Navigation with Xpath</span>
	<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		doc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		foo.<span style="color: #006633;">barWithXPath</span><span style="color: #009900;">&#40;</span>doc<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>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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></pre></div></div>

<p>结果：<br />
张三<br />
李四<br />
02</p>
<p>例如，如果你想获取一个XHTML中的所有超级链接，可以采用如下方式：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findLinks<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> DocumentException <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">List</span> list <span style="color: #339933;">=</span> document.<span style="color: #006633;">selectNodes</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;//a/@href&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> iter <span style="color: #339933;">=</span> list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> iter.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Attribute</span> attribute <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Attribute</span><span style="color: #009900;">&#41;</span> iter.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> attribute.<span style="color: #006633;">getValue</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></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>MyXhtml.xhtml文件</p>

<div class="wp_syntax"><div class="code"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color: #000066;">xml:lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyXhtml.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>www.oseschool.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2222<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3333<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>注意：若要利用上述findLinks 函数得到MyXhtml.xhtml文件中的a标签中href的属性值，必须对代码进行一定的修改。主要是因为MyXhtml.xhtml文件中带有命名空间。</p>
<p>具体代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Document</span> parseWithNameSpace<span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span> file<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> DocumentException <span style="color: #009900;">&#123;</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>
	<span style="color: #003399;">Map</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//把文件中的所以命名空间先放入一个MAP中</span>
	map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xmlns1&quot;</span>, <span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//设置命名空间</span>
	reader.<span style="color: #006633;">getDocumentFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setXPathNamespaceURIs</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> reader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> document<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> findLinks<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> DocumentException <span style="color: #009900;">&#123;</span>	<span style="color: #666666; font-style: italic;">//注意对要遍历的元素要加命名空间限制</span>
	<span style="color: #003399;">List</span> list <span style="color: #339933;">=</span> document.<span style="color: #006633;">selectNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;//xmlns1:a/@href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> iter <span style="color: #339933;">=</span> list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> iter.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Attribute</span> attribute <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Attribute</span><span style="color: #009900;">&#41;</span> iter.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> attribute.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Main函数执行：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> doc <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//1.3 Powerful Navigation with Xpath</span>
	<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyXhtml.xhtml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		doc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">parseWithNameSpace</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		foo.<span style="color: #006633;">findLinks</span><span style="color: #009900;">&#40;</span>doc<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>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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></pre></div></div>

<p>结果：<br />
www.oseschool.com<br />
2<br />
3</p>
<p>Fast Looping（快速循环）</p>
<p>如果你要遍历一个较大的XML文档，那么我们推荐你使用快速循环（递归）方法，这样将避免创建迭代器对象带来的开销。如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> treeWalk<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        treeWalk<span style="color: #009900;">&#40;</span> document.<span style="color: #006633;">getRootElement</span><span style="color: #009900;">&#40;</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: #666666; font-style: italic;">//采用递归的方式</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> treeWalk<span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span> element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>, size <span style="color: #339933;">=</span> element.<span style="color: #006633;">nodeCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Node node <span style="color: #339933;">=</span> element.<span style="color: #006633;">node</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//判断是否是元素，如&lt;test&gt;11&lt;/test&gt;即为元素</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> node <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Element</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                treeWalk<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> node <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// do something....打印元素中的文本内容</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>node.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</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: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>以下是：foo.xml</p>

<div class="wp_syntax"><div class="code"><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;gb2312&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1111&quot;</span> <span style="color: #000066;">fooname</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;01&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Main函数执行：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> doc <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//1.4 Fast Looping</span>
	<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo.xml&quot;</span><span style="color: #009900;">&#41;</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;">//parse方法为1.1 中 parse方法</span>
		doc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		foo.<span style="color: #006633;">treeWalk</span><span style="color: #009900;">&#40;</span>doc<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>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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></pre></div></div>

<p>结果：<br />
	（空行）<br />
	（空行）<br />
	张三<br />
	18</p>
<p>分析：“（空行）”表示本行内容为空，或许很多人感到疑问，为什么会有两个空行了，由于</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1111&quot;</span> <span style="color: #000066;">fooname</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;01&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>在<foo id="1111" fooname="test">以及</student>后都出现了换行，故解析器认为有两个TEXT类型的节点。也就是把文件改为：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1111&quot;</span> <span style="color: #000066;">fooname</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;student</span> <span style="color: #000066;">sn</span>=<span style="color: #ff0000;">&quot;01&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><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 style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/student<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>所以节点在同一行，结果就不会有空行。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> Creating a <span style="color: #000000; font-weight: bold;">new</span> XML document（创建一个新的XML文档）
<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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Document</span> createDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Document</span> 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>
        <span style="color: #003399;">Element</span> root <span style="color: #339933;">=</span> document.<span style="color: #006633;">addElement</span><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: #003399;">Element</span> author1 <span style="color: #339933;">=</span> root.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;author&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;James&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;location&quot;</span>, <span style="color: #0000ff;">&quot;UK&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addText</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;James Strachan&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Element</span> author2 <span style="color: #339933;">=</span> root.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;author&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;Bob&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;location&quot;</span>, <span style="color: #0000ff;">&quot;US&quot;</span> <span style="color: #009900;">&#41;</span>
            .<span style="color: #006633;">addText</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Bob McWhirter&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> document<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Writing a document to a file（写入一个文档到一个文件）</p>
<p>一种快速而简单地把文档写入到一个文件，即调用字符输出流的write方法。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #003399;">FileWriter</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileWriter</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo.xml&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  document.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span> out <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果你想要改变输出的格式，如宽松或紧凑的格式，而且你想要使用一些基本的字节或字符输出流，那么你可以使用XMLWriter类，如下。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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.io.OutputFormat</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> Foo <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> write<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// lets write to a file</span>
        XMLWriter writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLWriter<span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileWriter</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;output.xml&quot;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
&nbsp;
        <span style="color: #666666; font-style: italic;">// 打印出Pretty的样式。即元素之间有换行。</span>
        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>
        writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLWriter<span style="color: #009900;">&#40;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>, format <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
&nbsp;
        <span style="color: #666666; font-style: italic;">// 紧凑的格式，所有元素在一行</span>
        format <span style="color: #339933;">=</span> OutputFormat.<span style="color: #006633;">createCompactFormat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLWriter<span style="color: #009900;">&#40;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>, format <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>注意：如果按上述形式打印输出，是以计算机默认编码，进行写入操作，也就是如果在中文系统下，你读出的DOM，如果是UTF-8编码的，不会正确输出。所以建议用对上面的代码改进，如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	OutputFormat outFmt<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> OutputFormat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	outFmt.<span style="color: #006633;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">try</span>
	<span style="color: #009900;">&#123;</span>
		XMLWriter xmlWriter<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> XMLWriter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dom4jtest.xml&quot;</span><span style="color: #009900;">&#41;</span>,outFmt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		xmlWriter.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		xmlWriter.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		xmlWriter.<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></pre></div></div>

<p>上面最大改变是把FileWriter->FileOutputStream.然后使用OutputFormat设定编码。</p>
<p>Converting to and from Strings（转换字符串）</p>
<p>如果你有一个文档对象的引用或任何其他像属性或元素的节点，那么你可以把他们转换为一种默认的XML文本，通过调用 asXML 方法。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> document.<span style="color: #006633;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果你有些XML的文本字符串，你可以把它再转换回文档对象，通过使用帮助方法DocumentHelper.parseText()</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;person&gt; &lt;name&gt;James&lt;/name&gt; &lt;/person&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> DocumentHelper.<span style="color: #006633;">parseText</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 Styling a <span style="color: #003399;">Document</span> with XSLT（使用XSLT格式一个文档）</pre></div></div>

<p>要想在一个文档对象上运用一个XSLT可以直接调用SUN 的JAXP API（JAVA API FOR XML PROGRAM）。允许使用像Xalan或SAXON这样的任何XSLT引擎。这里有一个使用JAXP来创建transformer对象然后将XSLT运用在一个文档上。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.transform.Transformer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.transform.TransformerFactory</span><span style="color: #339933;">;</span>
&nbsp;
<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.io.DocumentResult</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dom4j.io.DocumentSource</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> Foo <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Document</span> styleDocument<span style="color: #009900;">&#40;</span>
        <span style="color: #003399;">Document</span> document, 
        <span style="color: #003399;">String</span> stylesheet
    <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// load the transformer using JAXP</span>
        TransformerFactory factory <span style="color: #339933;">=</span> TransformerFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Transformer transformer <span style="color: #339933;">=</span> factory.<span style="color: #006633;">newTransformer</span><span style="color: #009900;">&#40;</span> 
            <span style="color: #000000; font-weight: bold;">new</span> StreamSource<span style="color: #009900;">&#40;</span> stylesheet <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;">// now lets style the given document</span>
        DocumentSource source <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DocumentSource<span style="color: #009900;">&#40;</span> document <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        DocumentResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DocumentResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        transformer.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span> source, result <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// return the transformed document</span>
        <span style="color: #003399;">Document</span> transformedDoc <span style="color: #339933;">=</span> result.<span style="color: #006633;">getDocument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> transformedDoc<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Catalog.XML</p>

<div class="wp_syntax"><div class="code"><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;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catalog<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Empire Burlesque<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bob Dylan<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;country<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>USA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/country<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;company<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Columbia<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/company<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10.90<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;year<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1985<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/year<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/catalog<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>xsl<span style="color: #339933;">:</span>stylesheet version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> xmlns<span style="color: #339933;">:</span>xsl<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>xsl<span style="color: #339933;">:</span>template match<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>My CD Collection<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>table border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>tr bgcolor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#9acd32&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Artist<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>xsl<span style="color: #339933;">:</span>for<span style="color: #339933;">-</span>each select<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;catalog/cd&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>xsl<span style="color: #339933;">:</span>value<span style="color: #339933;">-</span>of select<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>xsl<span style="color: #339933;">:</span>value<span style="color: #339933;">-</span>of select<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>xsl<span style="color: #339933;">:</span>for<span style="color: #339933;">-</span>each<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>xsl<span style="color: #339933;">:</span>template<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>xsl<span style="color: #339933;">:</span>stylesheet<span style="color: #339933;">&gt;</span></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Main函数：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Document</span> doc <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//1.4 Fast Looping</span>
	<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;catalog.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		doc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Document</span> newDoc <span style="color: #339933;">=</span> foo.<span style="color: #006633;">styleDocument</span><span style="color: #009900;">&#40;</span>doc, <span style="color: #0000ff;">&quot;catalog.xsl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 1.6 中的write方法</span>
		foo.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>newDoc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</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></pre></div></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>结果：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>My CD Collection<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>table border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr bgcolor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#9acd32&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Artist<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>Empire Burlesque<span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>Bob Dylan<span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;&lt;</span>body<span style="color: #339933;">&gt;&lt;</span>h2<span style="color: #339933;">&gt;</span>My CD Collection<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;&lt;</span>table border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;&lt;</span>tr bgcolor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#9acd32&quot;</span><span style="color: #339933;">&gt;&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;&lt;</span>th align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">&gt;</span>Artist<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;&lt;/</span>tr<span style="color: #339933;">&gt;&lt;</span>tr<span style="color: #339933;">&gt;&lt;</span>td<span style="color: #339933;">&gt;</span>Empire Burlesque<span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;&lt;</span>td<span style="color: #339933;">&gt;</span>Bob Dylan<span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;&lt;/</span>tr<span style="color: #339933;">&gt;&lt;/</span>table<span style="color: #339933;">&gt;&lt;/</span>body<span style="color: #339933;">&gt;&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>分析：结果中有一个为Pretty的样式，一个为compact的样式。还生成了一个output.xml</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/70/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java中的多维数组</title>
		<link>http://code.oseschool.com/index.php/archives/68</link>
		<comments>http://code.oseschool.com/index.php/archives/68#comments</comments>
		<pubDate>Thu, 27 Aug 2009 02:02:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[数组]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=68</guid>
		<description><![CDATA[多维数组的声明：

数据类型[] []  数组名称;
数据类型[] 数组名称 [];
数据类型   数组名称[][];

以上三种都的数组命名方式都是正确的，功能也是等价的
但是我们在JAVA的推荐规范中。推荐使用第一种方式。
既：String[][] args = new String[1][2];
在JAVA中多维数组的初始化也可以分为静态初始化和动态初始化两种。
静态初始化:
String[][] args = {{“a”,” b”,”c”},{“d”,”e”}}; 
实际等效于 
String[][] args2 = new String[2][];
args2[0] = new String[3];
args2[0][0] = &#8220;a&#8221;;
args2[0][1] = &#8220;b&#8221;;
args2[0][2] = &#8220;c&#8221;;
args2[1] = new String[2];
args2[1][0] = &#8220;d&#8221;;
args2[1][1] = &#8220;e&#8221;;
动态初始化：

数据类型[][] 数组名称 = new 数据类型[第一维的长度][第二维的长度];
数据类型[][] 数组名称;
数组名称 = new 数据类型[第一维的长度][第二维的长度];

例：String[][] args = new String[2][3];
绝对不能这样写：String[][] args = new String[][3];
必须先初始化第一维的长度后，再初始化后面的长度，才可以。
所以JAVA 的多维数组也称之为 数组中的数组。也就是最后一维的数组才是真正存具体值信息的地方。
]]></description>
			<content:encoded><![CDATA[<p>多维数组的声明：</p>
<ul>
<li>数据类型[] []  数组名称;</li>
<li>数据类型[] 数组名称 [];</li>
<li>数据类型   数组名称[][];</li>
</ul>
<p>以上三种都的数组命名方式都是正确的，功能也是等价的</p>
<p>但是我们在JAVA的推荐规范中。推荐使用第一种方式。</p>
<p>既：String[][] args = new String[1][2];</p>
<p>在JAVA中多维数组的初始化也可以分为静态初始化和动态初始化两种。</p>
<p>静态初始化:</p>
<p>String[][] args = {{“a”,” b”,”c”},{“d”,”e”}}; </p>
<p>实际等效于 </p>
<p>String[][] args2 = new String[2][];</p>
<p>args2[0] = new String[3];</p>
<p>args2[0][0] = &#8220;a&#8221;;</p>
<p>args2[0][1] = &#8220;b&#8221;;</p>
<p>args2[0][2] = &#8220;c&#8221;;</p>
<p>args2[1] = new String[2];</p>
<p>args2[1][0] = &#8220;d&#8221;;</p>
<p>args2[1][1] = &#8220;e&#8221;;</p>
<p>动态初始化：</p>
<ul>
<li>数据类型[][] 数组名称 = new 数据类型[第一维的长度][第二维的长度];</li>
<li>数据类型[][] 数组名称;</li>
<li>数组名称 = new 数据类型[第一维的长度][第二维的长度];</li>
</ul>
<p>例：String[][] args = new String[2][3];</p>
<p>绝对不能这样写：String[][] args = new String[][3];</p>
<p>必须先初始化第一维的长度后，再初始化后面的长度，才可以。</p>
<p>所以JAVA 的多维数组也称之为 数组中的数组。也就是最后一维的数组才是真正存具体值信息的地方。</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/68/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MyEclipse6.0.1配置Tomcat6.0</title>
		<link>http://code.oseschool.com/index.php/archives/55</link>
		<comments>http://code.oseschool.com/index.php/archives/55#comments</comments>
		<pubDate>Wed, 26 Aug 2009 01:26:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=55</guid>
		<description><![CDATA[配置Tomcat6.0：
1.点Configure Server

2.如图所示：选择MyEclipse->Servers->Tomcat 6.x(如果是Tomcat5.5版本则选择Tomcat5.x),点Browse后找到Tomcat6.0安装文件夹,点确定,并把Tomcat server设置为Enable,点Apply->OK!

部署j2ee项目：
1.点左边图标
2.选择要部署的项目：Hello,点Add

3.选择Server:Tomcat  6.x，点Finish（刚才配置的Tomcat)

4.出现如下画面：Deployment Status为Successfully deployed，说明部署成功，点ＯＫ！

启动Tomcat：
点击右边的三角按钮,选择Tomcat 6.x->Start,如下图所示:

在控制台出现如下信息: Server startup in 6824 ms,说明Tomcat成功启动!
]]></description>
			<content:encoded><![CDATA[<p>配置Tomcat6.0：</p>
<p>1.点Configure Server</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/1.png" /></p>
<p>2.如图所示：选择MyEclipse->Servers->Tomcat 6.x(如果是Tomcat5.5版本则选择Tomcat5.x),点Browse后找到Tomcat6.0安装文件夹,点确定,并把Tomcat server设置为Enable,点Apply->OK!</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/2.jpg" /></p>
<p>部署j2ee项目：</p>
<p>1.<img src="http://code.oseschool.com/wp-content/uploads/2009/08/3.jpg" />点左边图标</p>
<p>2.选择要部署的项目：Hello,点Add</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/4.png" /></p>
<p>3.选择Server:Tomcat  6.x，点Finish（刚才配置的Tomcat)</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/5.jpg" /></p>
<p>4.出现如下画面：Deployment Status为Successfully deployed，说明部署成功，点ＯＫ！</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/6.png" /></p>
<p>启动Tomcat：</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/7.png" />点击右边的三角按钮,选择Tomcat 6.x->Start,如下图所示:</p>
<p><img src="http://code.oseschool.com/wp-content/uploads/2009/08/8.jpg" /></p>
<p>在控制台出现如下信息: Server startup in 6824 ms,说明Tomcat成功启动!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/55/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JDBC连接后使用篇</title>
		<link>http://code.oseschool.com/index.php/archives/51</link>
		<comments>http://code.oseschool.com/index.php/archives/51#comments</comments>
		<pubDate>Mon, 24 Aug 2009 14:11:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=51</guid>
		<description><![CDATA[准备之一：你已经成功的获取了连接
准备之二：手头一定要有JDK-API
连接使用篇之statemment 与preparedstatement 
大多数人都或多或少的听过这两个对象的传言，也应该有印象都是在告诫大家用preparedstatement更好，例如预编译效率快，安全性高，代码更具可读性云云。我们先来看这两个对象的用法（方便起见，我们先假设执行的都是无返回的sql语句，增 删 改）
Statement的使用

	/**
	 * Function : a demo for using Statement
	 *
	 * Product describe:
	 *
	 * @throws SQLException
	 * on Aug 23, 2009 BY Simrina
	 */
	public void sqlExcuteDemo&#40;&#41; throws SQLException&#123;
&#160;
		//连接数据库
		this.connectDB&#40;&#41;;
&#160;
		//当然你也可以换成insert into或者update ..
		String sql = &#34;delete from user where uid = 1&#34;;
&#160;
		//获取Statement对象用于执行Sql语句
		Statement stm = this.conn.createStatement&#40;&#41;;
&#160;
		//执行就这么一句话，简单搞定
		stm.execute&#40;sql&#41;;
&#160;
		//好习惯，有开始，有关闭——当然关闭的时机，你可以选一个你认为更适合的
		stm.close&#40;&#41;;
		this.conn.close&#40;&#41;;
&#160;
	&#125;

2、PreparedStatement的使用

/**
	 * Function : a demo for using Statement
	 [...]]]></description>
			<content:encoded><![CDATA[<p>准备之一：你已经成功的获取了连接</p>
<p>准备之二：手头一定要有JDK-API</p>
<p>连接使用篇之statemment 与preparedstatement </p>
<p>大多数人都或多或少的听过这两个对象的传言，也应该有印象都是在告诫大家用preparedstatement更好，例如预编译效率快，安全性高，代码更具可读性云云。我们先来看这两个对象的用法（方便起见，我们先假设执行的都是无返回的sql语句，增 删 改）</p>
<p>Statement的使用</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Function : a demo for using Statement
	 *
	 * Product describe:
	 *
	 * @throws SQLException
	 * on Aug 23, 2009 BY Simrina
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sqlExcuteDemo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//连接数据库</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">connectDB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//当然你也可以换成insert into或者update ..</span>
		<span style="color: #003399;">String</span> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;delete from user where uid = 1&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//获取Statement对象用于执行Sql语句</span>
		<span style="color: #003399;">Statement</span> stm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//执行就这么一句话，简单搞定</span>
		stm.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//好习惯，有开始，有关闭——当然关闭的时机，你可以选一个你认为更适合的</span>
		stm.<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>2、PreparedStatement的使用</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Function : a demo for using Statement
	 *
	 * Product describe:
	 *
	 * @throws SQLException
	 * on Aug 23, 2009 BY Simrina
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sqlExcuteDemo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> uid<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//连接数据库</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">connectDB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//当然你也可以换成insert into或者update ..</span>
		<span style="color: #003399;">String</span> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;delete from user where uid = ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//获取PreparedStatement对象 </span>
		<span style="color: #003399;">PreparedStatement</span> pstm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//设置参数 注意设置参数的方法</span>
		pstm.<span style="color: #006633;">setInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, uid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//关闭</span>
		pstm.<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>比较上面两个例子，首先需要注意：Statement和PreparedStatement的获取的方法是不一样的</p>
<p>最后特别关注PreparedStatement设置参数的方法。一般需要对应参数的类型，例如String 类型，则使用setString(parameterIndex,value),同理可证肯定还有setDouble setDate之类的。方法的第一个参数是参数的位置，换句话说，是你sql的问号的位子，从1开始，第一个参数则是参数的value。</p>
<p>所以注意，你可以给语句设置多个参数，当然要类型相对应。</p>
<p>现在我们来谈谈两者的区别，我们一开始说的为什么要推荐PreparedStatement</p>
<p>需要了解prepared的预编译，是需要数据库支持的，不过幸好大部分主流数据库都支持，除了mySQL。所谓的预编译，其实就是相同的Sql语句预先编译并保存起来，之后用到相同的，数据库直接调用已经编译好的语句，节约效率。因此，在相同的一次操作，两者并没有大的性能差异，反而prepared的开销更大一点。</p>
<p>针对有缓存预编译语句的DB而言，预编译的好处在于，如果同一时间有多个用户进行相同语句的操作，或者是打算进行大批量数据处理，或者有一些非常常用的SQL语句，预编译才能明显体现出效率来</p>
<p>最后说一个安全性，举个例子，如果想要查询用户名为Simrina’D的数据，如果你用“select * from user where username = ‘Simrina’D’”，肯定会出错，单引号会提前匹配掉，如果用setString,则可以完整匹配我们整个字符串——姑且当作抛砖引玉</p>
<p>最后，我们来做一个有返回值的查询例子，作为JDBC使用的补完Demo.这个时候不要忘了随时查看，我们准备的API。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Function : 查询数据 
	 *
	 * Product describe: demo 的数据表 uid username
	 *									1 simrina'D
	 *									2 simrina
	 * @return
	 * @throws SQLException
	 * on Aug 24, 2009 BY Simrina
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> excuteDemo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//连接数据库</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">connectDB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//当然你也可以换成insert into或者update ..</span>
		<span style="color: #003399;">String</span> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from user&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">List</span> lst <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//获取Statement对象用于执行Sql语句</span>
		<span style="color: #003399;">Statement</span> stm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//查询，返回ResultSet——关于ResultSet，自行查询API</span>
		<span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> stm.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//取数据的简单demo</span>
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #003399;">String</span> username <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			lst.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//好习惯，有开始，有关闭——当然关闭的时机，你可以选一个你认为更适合的</span>
		stm.<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conn</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> lst<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/51/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JDBC连接数据库篇</title>
		<link>http://code.oseschool.com/index.php/archives/44</link>
		<comments>http://code.oseschool.com/index.php/archives/44#comments</comments>
		<pubDate>Mon, 24 Aug 2009 13:59:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jdbc]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=44</guid>
		<description><![CDATA[Java Data Base Connectivity,JDBC——简单来说就是java连接数据库并执行SQL的API，当然不管什么数据库都一样。我们来看看链接代码：

/** @author Sinyee Simrina */
public class DBConnectDemo &#123;
&#160;
	private Connection conn;
&#160;
	/**
	 * Function : just a jdbc connection DEMO
	 *
	 * Product describe: just a jdbc connection DEMO
	 *
	 * on Aug 21, 2009 BY Simrina
	 */
	public void connectDB&#40;&#41; &#123;
&#160;
		//connncet messages
		String user = &#34;root&#34;;
		String password = &#34;root&#34;;
&#160;
		//url for different DB  问题一：连接名是怎么构成的
		String url [...]]]></description>
			<content:encoded><![CDATA[<p>Java Data Base Connectivity,JDBC——简单来说就是java连接数据库并执行SQL的API，当然不管什么数据库都一样。我们来看看链接代码：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** @author Sinyee Simrina */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DBConnectDemo <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> conn<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Function : just a jdbc connection DEMO
	 *
	 * Product describe: just a jdbc connection DEMO
	 *
	 * on Aug 21, 2009 BY Simrina
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> connectDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//connncet messages</span>
		<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//url for different DB  问题一：连接名是怎么构成的</span>
		<span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/testDB&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//driver for mySQL问题二：加载驱动的字符串到底什么意思？</span>
			<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
&nbsp;
			<span style="color: #666666; font-style: italic;">//问题三：为什么我没有用常用Class.forName(&quot;XXX&quot;).newInstance();</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				conn <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>url, user, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// TODO to do sth after connnect DB 后续问题：连接了数据之后怎么用</span>
&nbsp;
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
				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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			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: #009900;">&#125;</span></pre></div></div>

<p>问题一：连接名是怎么构成的</p>
<p>问题二：加载驱动的字符串到底什么意思？</p>
<p>问题三：为什么我没有用常用Class.forName(&#8221;XXX&#8221;).newInstance();</p>
<p>后续问题：连接了数据之后怎么用</p>
<p>搞清楚前三个问题，JDBC以后闭着眼睛也可以写出来了</p>
<p>JDBC 连接方式+数据库类别+对应数据库的连接url</p>
<p>所以除了mySQL:</p>
<p>我们看看其他数据库的连接讯息。。</p>
<p>//url = &#8220;jdbc:oracle:thin:@localhost:1521:testDB&#8221;;</p>
<p>//url = &#8220;jdbc:sqlserver://localhost:1433;DatabaseName=testDB&#8221;</p>
<p>Class.forName(&#8221;com.mysql.jdbc.Driver&#8221;); 其实很简单，看看图</p>
<p><img class="alignnone size-full wp-image-48" title="jdbc配置" src="http://code.oseschool.com/wp-content/uploads/2009/08/jdbc.png" alt="jdbc配置" width="221" height="378" /></p>
<p>看懂了吧，所谓加载驱动就是找到对应的驱动class而已，不需要死记硬背。所以不要再问为什么sqlServer2000和2005的驱动字符串不一样了，微软刚好改变了Driver的存放的包的包名而已——下次发现ClassNotFoundException时候，直接看看你的classpath下有没有这个驱动——或者是驱动的包名类名是不是正确。</p>
<p>我们看看oracle/sqlserver的，现在知道为什么了吧</p>
<p>//Driver for ORACLE</p>
<p>//Class.forName(&#8221;oracle.jdbc.OracleDriver&#8221;);</p>
<p>//Driver for sqlSeriver2000</p>
<p>//Class.forName(&#8221;com.microsoft.jdbc.sqlserver.SQLServerDriver&#8221;);</p>
<p>//Driver for sqlSeriver2005</p>
<p>//Class.forName(&#8221;com.microsoft.sqlserver.jdbc.SQLServerDriver&#8221;);</p>
<p>为什么没有用常用Class.forName(&#8221;XXX&#8221;).newInstance();</p>
<p>newInstance方法的作用，是获取该驱动的一个实例，连接JDBC驱动只要加载就足够了，DriverManager就可以获取到连接</p>
<p>前三步做完了，不管哪一个数据库都可以获取到连接，不管是orcale还是sqlServer,DB2或者Sybase，当然写的时候不要忘记抛错。</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/44/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH开发心得</title>
		<link>http://code.oseschool.com/index.php/archives/42</link>
		<comments>http://code.oseschool.com/index.php/archives/42#comments</comments>
		<pubDate>Mon, 24 Aug 2009 13:53:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=42</guid>
		<description><![CDATA[前段时间开发一个J2EE的Web项目，使用SSH（Struts+Spring+hibernate）框架。开发过程中遇到一个相当郁闷的问题,困扰了我好几天。这里我跟大家分享一下。
问题是这样的：我用Spring的声明式事务管理来管理事务，当出现异常时能回滚事务。于是我做了一个测试，在一个方法里，我先添加了一条数据，然后再删除一条数据，在删除数据的时候我让它强行抛出异常，这时刚添加的那条数据应该回滚掉，可是我查看了一下数据库，发现数据已经插入，根本没有回滚，这是怎么回事呢？我的第一个反应就是Spring配置文件可能出问题了。
以下是我的Spring的事务管理的配置方式：

	&#60;bean id=&#34;transactionManager&#34; class=&#34;org.springframework.orm.hibernate3.HibernateTransactionManager&#34;&#62;
	&#60;propertyname=&#34;sessionFactory&#34;ref=&#34;sessionFactory&#34;/&#62;
	&#60;/bean&#62;
&#160;
	&#60;bean id=&#34;transactionInterceptor&#34; class=&#34;org.springframework.transaction.interceptor.TransactionInterceptor&#34;&#62;
	&#60;!--事务拦截器bean需要依赖注入一个事务管理器--&#62;
	&#60;property name=&#34;transactionManager&#34;ref=&#34;transactionManager&#34;/&#62;
	&#60;property name=&#34;transactionAttributes&#34;&#62;
	&#60;!--下面定义事务传播属性--&#62;
	&#60;props&#62;
	&#60;prop key=&#34;get*&#34;&#62;PROPAGATION_REQUIRED,readOnly&#60;/prop&#62;
	&#60;prop key=&#34;*&#34;&#62;PROPAGATION_REQUIRED,-Exception&#60;/prop&#62;
	&#60;/props&#62;
	&#60;/property&#62;
	&#60;/bean&#62;
		&#60;!--定义BeanNameAutoProxyCreator--&#62;
		&#60;bean
		class=&#34;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&#34;&#62;
			&#60;!--指定对满足哪些beanname的bean自动生成业务代理--&#62;
			&#60;property name=&#34;beanNames&#34;&#62;
				&#60;!--下面是所有需要自动创建事务代理的bean--&#62;
				&#60;list&#62;
					&#60;value&#62;*Bo&#60;/value&#62;
				&#60;/list&#62;
				&#60;!--此处可增加其他需要自动创建事务代理的bean--&#62;
			&#60;/property&#62;
			&#60;!--下面定义BeanNameAutoProxyCreator所需的事务拦截器--&#62;
			&#60;property name=&#34;interceptorNames&#34;&#62;
				&#60;list&#62;
					&#60;!--此处可增加其他新的Interceptor--&#62;
					&#60;value&#62;transactionInterceptor&#60;/value&#62;
				&#60;/list&#62;
			&#60;/property&#62;
		&#60;/bean&#62;

带着这个问题，我到网络上查了很多关于Spring配置事务的资料，也做了很多次修改，但问题仍然存在。我开始怀疑难道是Spring配置方式根本就是正确的。那这样的话问题到底出在哪里呢？为什么抛出异常后事务没有回滚呢？
快接近崩溃边缘的我还是硬着头皮继续在网络上求教，突然在一篇文章里看到这样一句话“Mysql表的MyISAM类型不支持事务处理，InnoDB类型提供事务支持”。我马上查了一下数据库的表类型，发现我的表就是MyISAM类型的，太兴奋了,难道就这样搞定了?于是我马上将表类型改成InnoDB，重启测试，问题解决。兴奋之余总结一下:原因就是我一直以为是Spring配置文件出问题了，却一直忽略了其他的情况。最后，将Mysql这两种表类型的详细对比跟大家分享一下。
InnoDB和MyISAM是MySQL最常用的表类型，各有各的优缺点，选用哪个视具体应用场景而定。
MyIASM:基于传统的IASM类型,ISAM是Indexed Sequential Access Method(有索引的顺序访问方法)的缩写,IASM是存储记录和文件的标准方法。与其他存储引擎比较,MyISAM具有检查和修复表的大多数工具。MyISAM表可以被压缩,而且它们支持全文搜索。它们不是事务安全的,而且也不支持外键。如果事物回滚将造成不完全回滚，不具有原子性。如果执行大量的SELECT，MyISAM是更好的选择。MyISAM对IASM有如下扩展：
二进制层次的可移植性。
NULL列索引。
对变长行比ISAM表有更少的碎片。
支持大文件。
更好的索引压缩。
更好的键码统计分布。
更好和更快的auto_increment处理。
InnoDB:它是事务安全的,与BDB类型具有相同的特性。它们支持外键。InnoDB表速度很快,具有比BDB还丰富的特性,因此如果需要一个事务安全的存储引擎,建议使用它。如果你的数据执行大量的INSERT或UPDATE,出于性能与事务方面的考虑，应该使用InnoDB类型的表。
以下是InnoDB与MyISAM细节和具体实现的差别：
InnoDB有它自己的缓冲池，能缓冲数据和索引，InnoDB把数据和索引存放在表空间里面，可能包含好几个文件，在MyISAM中，表被存放在单独的文件中，InnoDB表的大小只受限于操作系统文件的大小，一般为2GB。InnoDB不支持FULLTEXT类型的索引。
InnoDB支持行锁，提供和Oracle一样的一致性的不加锁读取，能增加并发读的用户数量并提高性能，不会增加锁的数量。另外，InnoDB表的行锁也不是绝对的，如果在执行一个SQL语句时MySQL不能确定要扫描的范围，InnoDB表同样会锁全表，例如update table set num=1 where name like“%aaa%”。
InnoDB中不保存表的具体行数，也就是说，执行selectcount(*)fromtable时，InnoDB要扫描一遍整个表来计算有多少行，但是MyISAM只要简单的读出保存好的行数即可。要注意的是，当count(*)语句包含where条件时，两种表的操作是一样的。
对于AUTO_INCREMENT类型的字段，InnoDB中必须包含只有该字段的索引，但是在MyISAM表中，可以和其他字段一起建立联合索引。
DELETE FROM table时，InnoDB不会重新建立表，而是一行一行的删除。
LOAD TABLE FROM MASTER操作对InnoDB是不起作用的，解决方法是首先把InnoDB表改成MyISAM表，导入数据后再改成InnoDB表，但是对于使用的额外的InnoDB特性（例如外键）的表不适用。
任何一种表类型都不是万能的，只能针对具体的应用场景来选择合适的表类型，这样才能最大限度地发挥MySQL的性能。
]]></description>
			<content:encoded><![CDATA[<p>前段时间开发一个J2EE的Web项目，使用SSH（Struts+Spring+hibernate）框架。开发过程中遇到一个相当郁闷的问题,困扰了我好几天。这里我跟大家分享一下。</p>
<p>问题是这样的：我用Spring的声明式事务管理来管理事务，当出现异常时能回滚事务。于是我做了一个测试，在一个方法里，我先添加了一条数据，然后再删除一条数据，在删除数据的时候我让它强行抛出异常，这时刚添加的那条数据应该回滚掉，可是我查看了一下数据库，发现数据已经插入，根本没有回滚，这是怎么回事呢？我的第一个反应就是Spring配置文件可能出问题了。</p>
<p>以下是我的Spring的事务管理的配置方式：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;">&lt;<span style="color: #000066;">propertyname</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionInterceptor&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.transaction.interceptor.TransactionInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #808080; font-style: italic;">&lt;!--事务拦截器bean需要依赖注入一个事务管理器--&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span><span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;transactionAttributes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #808080; font-style: italic;">&lt;!--下面定义事务传播属性--&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;get*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>PROPAGATION_REQUIRED,readOnly<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>PROPAGATION_REQUIRED,-Exception<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #808080; font-style: italic;">&lt;!--定义BeanNameAutoProxyCreator--&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #808080; font-style: italic;">&lt;!--指定对满足哪些beanname的bean自动生成业务代理--&gt;</span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;beanNames&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #808080; font-style: italic;">&lt;!--下面是所有需要自动创建事务代理的bean--&gt;</span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*Bo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #808080; font-style: italic;">&lt;!--此处可增加其他需要自动创建事务代理的bean--&gt;</span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #808080; font-style: italic;">&lt;!--下面定义BeanNameAutoProxyCreator所需的事务拦截器--&gt;</span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;interceptorNames&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #808080; font-style: italic;">&lt;!--此处可增加其他新的Interceptor--&gt;</span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>transactionInterceptor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>带着这个问题，我到网络上查了很多关于Spring配置事务的资料，也做了很多次修改，但问题仍然存在。我开始怀疑难道是Spring配置方式根本就是正确的。那这样的话问题到底出在哪里呢？为什么抛出异常后事务没有回滚呢？</p>
<p>快接近崩溃边缘的我还是硬着头皮继续在网络上求教，突然在一篇文章里看到这样一句话“Mysql表的MyISAM类型不支持事务处理，InnoDB类型提供事务支持”。我马上查了一下数据库的表类型，发现我的表就是MyISAM类型的，太兴奋了,难道就这样搞定了?于是我马上将表类型改成InnoDB，重启测试，问题解决。兴奋之余总结一下:原因就是我一直以为是Spring配置文件出问题了，却一直忽略了其他的情况。最后，将Mysql这两种表类型的详细对比跟大家分享一下。</p>
<p>InnoDB和MyISAM是MySQL最常用的表类型，各有各的优缺点，选用哪个视具体应用场景而定。</p>
<p>MyIASM:基于传统的IASM类型,ISAM是Indexed Sequential Access Method(有索引的顺序访问方法)的缩写,IASM是存储记录和文件的标准方法。与其他存储引擎比较,MyISAM具有检查和修复表的大多数工具。MyISAM表可以被压缩,而且它们支持全文搜索。它们不是事务安全的,而且也不支持外键。如果事物回滚将造成不完全回滚，不具有原子性。如果执行大量的SELECT，MyISAM是更好的选择。MyISAM对IASM有如下扩展：</p>
<p>二进制层次的可移植性。</p>
<p>NULL列索引。</p>
<p>对变长行比ISAM表有更少的碎片。</p>
<p>支持大文件。</p>
<p>更好的索引压缩。</p>
<p>更好的键码统计分布。</p>
<p>更好和更快的auto_increment处理。</p>
<p>InnoDB:它是事务安全的,与BDB类型具有相同的特性。它们支持外键。InnoDB表速度很快,具有比BDB还丰富的特性,因此如果需要一个事务安全的存储引擎,建议使用它。如果你的数据执行大量的INSERT或UPDATE,出于性能与事务方面的考虑，应该使用InnoDB类型的表。</p>
<p>以下是InnoDB与MyISAM细节和具体实现的差别：</p>
<p>InnoDB有它自己的缓冲池，能缓冲数据和索引，InnoDB把数据和索引存放在表空间里面，可能包含好几个文件，在MyISAM中，表被存放在单独的文件中，InnoDB表的大小只受限于操作系统文件的大小，一般为2GB。InnoDB不支持FULLTEXT类型的索引。</p>
<p>InnoDB支持行锁，提供和Oracle一样的一致性的不加锁读取，能增加并发读的用户数量并提高性能，不会增加锁的数量。另外，InnoDB表的行锁也不是绝对的，如果在执行一个SQL语句时MySQL不能确定要扫描的范围，InnoDB表同样会锁全表，例如update table set num=1 where name like“%aaa%”。</p>
<p>InnoDB中不保存表的具体行数，也就是说，执行selectcount(*)fromtable时，InnoDB要扫描一遍整个表来计算有多少行，但是MyISAM只要简单的读出保存好的行数即可。要注意的是，当count(*)语句包含where条件时，两种表的操作是一样的。</p>
<p>对于AUTO_INCREMENT类型的字段，InnoDB中必须包含只有该字段的索引，但是在MyISAM表中，可以和其他字段一起建立联合索引。</p>
<p>DELETE FROM table时，InnoDB不会重新建立表，而是一行一行的删除。</p>
<p>LOAD TABLE FROM MASTER操作对InnoDB是不起作用的，解决方法是首先把InnoDB表改成MyISAM表，导入数据后再改成InnoDB表，但是对于使用的额外的InnoDB特性（例如外键）的表不适用。</p>
<p>任何一种表类型都不是万能的，只能针对具体的应用场景来选择合适的表类型，这样才能最大限度地发挥MySQL的性能。</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/42/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>J2EE开发三件宝: Domain Model(域建模)、patterns(模式)和framework(框架)。</title>
		<link>http://code.oseschool.com/index.php/archives/40</link>
		<comments>http://code.oseschool.com/index.php/archives/40#comments</comments>
		<pubDate>Mon, 24 Aug 2009 13:49:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=40</guid>
		<description><![CDATA[J2EE可以说指Java在数据库信息系统上实现，数据库信息系统从早期的dBase、到Delphi/VB等C/S结构，发展到B/S(Browser浏览器/Server服务器)结构，而J2EE主要是指B/S结构的实现。
J2EE是一个虚的大的概念，J2EE标准主要有三种子技术标准：WEB技术、EJB技术和JMS，谈到J2EE应该说最终要落实到这三个子概念上。
这三种技术的每个技术在应用时都涉及两个部分：容器部分和应用部分，Web容器也是指Jsp/Servlet容器，你如果要开发一个Web应用，无论是编译或运行，都必须要有Jsp/Servlet库或API支持(除了JDK/J2SE以外)。
Web技术中除了Jsp/Servlet技术外，还需要JavaBeans或Java Class实现一些功能或者包装携带数据，所以Web技术最初裸体简称为Jsp/Servlet+JavaBeans系统。
衡量J2EE应用系统设计开发水平高低的标准就是：解耦性;你的应用系统各个功能是否能够彻底脱离?是否不相互依赖，也只有这样，才能体现可维护性、可拓展性的软件设计目标。
了达到这个目的，诞生各种框架概念，J2EE框架标准将一个系统划分为WEB和EJB主要部分，当然我们有时不是以这个具体技术区分，而是从设计上抽象为表现层、服务层和持久层，这三个层次从一个高度将J2EE分离开来，实现解耦目的。
因此，我们实际编程中，也要将自己的功能向这三个层次上靠，做到大方向清楚，泾渭分明，但是没有技术上约束限制要做到这点是很不容易的，因此我们还是必须借助J2EE具体技术来实现
使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来，而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离，这部分JavaBeans主要负责显示相关，一般是通过标签库(taglib)实现，不同框架有不同自己的标签库，Struts是应用比较广泛的一种表现层框架。
这样，表现层和服务层的分离是通过两种框架达到目的，剩余的就是持久层框架了，通过持久层的框架将数据库存储从服务层中分离出来是其目的，持久层框架有两种方向：直接自己编写JDBC等SQL语句(如iBatis);使用O/R Mapping技术实现的Hibernate和JDO技术;当然还有EJB中的实体Bean技术。
持久层框架目前呈现百花齐放，各有优缺点的现状，所以正如表现层框架一样，目前没有一个框架被指定为标准框架。
以本公司来说，完全是基于以上几点的考虑采用了如下架构：
表现层：Struts
持久层：Hibernate
服务层：Business Object 
Struts完全体现了MVC思想，将一个应用分成三个基本部分：Model（模型）、View（视图）和Controller（控制器），这三个部分以最少的耦合协同工作，从而提高应用的可扩展性及可维护性。在数据库端使用了Hibernate，它使得与关系数据库打交道变得十分轻松，就像您的数据库中包含每天使用的普通Java对象一样，同时不必考虑如何把它们从神秘的数据库表中取出（或放回到数据库表中）。它解放了您，使您可以专注于应用程序的对象和功能，而不必担心如何保存它们或稍后如何找到它们。
最后，你的J2EE应用系统如果采取上面提到的表现层、服务层和持久层的框架实现，基本你也可以在无需深刻掌握设计模式的情况下开发出一个高质量的应用系统了。
还要注意的是: 开发出一个高质量的J2EE系统还需要正确的业务需求理解，那么域建模提供了一种比较切实可行的正确理解业务需求的方法，相关详细知识可从UML角度结合理解。
当然，如果你想设计自己的行业框架，那么第一步从设计模式开始吧，因为设计模式提供你一个实现JavaBeans或类之间解耦参考实现方法，当你学会了系统基本单元JavaBean或类之间解耦时，那么系统模块之间的解耦你就可能掌握，进而你就可以实现行业框架的提炼了，这又是另外一个发展方向了。
以上理念可以总结为一句话：
J2EE开发三件宝: Domain Model(域建模)、patterns(模式)和framework(框架)。
]]></description>
			<content:encoded><![CDATA[<p>J2EE可以说指Java在数据库信息系统上实现，数据库信息系统从早期的dBase、到Delphi/VB等C/S结构，发展到B/S(Browser浏览器/Server服务器)结构，而J2EE主要是指B/S结构的实现。</p>
<p>J2EE是一个虚的大的概念，J2EE标准主要有三种子技术标准：WEB技术、EJB技术和JMS，谈到J2EE应该说最终要落实到这三个子概念上。</p>
<p>这三种技术的每个技术在应用时都涉及两个部分：容器部分和应用部分，Web容器也是指Jsp/Servlet容器，你如果要开发一个Web应用，无论是编译或运行，都必须要有Jsp/Servlet库或API支持(除了JDK/J2SE以外)。</p>
<p>Web技术中除了Jsp/Servlet技术外，还需要JavaBeans或Java Class实现一些功能或者包装携带数据，所以Web技术最初裸体简称为Jsp/Servlet+JavaBeans系统。</p>
<p>衡量J2EE应用系统设计开发水平高低的标准就是：解耦性;你的应用系统各个功能是否能够彻底脱离?是否不相互依赖，也只有这样，才能体现可维护性、可拓展性的软件设计目标。</p>
<p>了达到这个目的，诞生各种框架概念，J2EE框架标准将一个系统划分为WEB和EJB主要部分，当然我们有时不是以这个具体技术区分，而是从设计上抽象为表现层、服务层和持久层，这三个层次从一个高度将J2EE分离开来，实现解耦目的。</p>
<p>因此，我们实际编程中，也要将自己的功能向这三个层次上靠，做到大方向清楚，泾渭分明，但是没有技术上约束限制要做到这点是很不容易的，因此我们还是必须借助J2EE具体技术来实现</p>
<p>使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来，而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离，这部分JavaBeans主要负责显示相关，一般是通过标签库(taglib)实现，不同框架有不同自己的标签库，Struts是应用比较广泛的一种表现层框架。</p>
<p>这样，表现层和服务层的分离是通过两种框架达到目的，剩余的就是持久层框架了，通过持久层的框架将数据库存储从服务层中分离出来是其目的，持久层框架有两种方向：直接自己编写JDBC等SQL语句(如iBatis);使用O/R Mapping技术实现的Hibernate和JDO技术;当然还有EJB中的实体Bean技术。</p>
<p>持久层框架目前呈现百花齐放，各有优缺点的现状，所以正如表现层框架一样，目前没有一个框架被指定为标准框架。</p>
<p>以本公司来说，完全是基于以上几点的考虑采用了如下架构：</p>
<p>表现层：Struts</p>
<p>持久层：Hibernate</p>
<p>服务层：Business Object </p>
<p>Struts完全体现了MVC思想，将一个应用分成三个基本部分：Model（模型）、View（视图）和Controller（控制器），这三个部分以最少的耦合协同工作，从而提高应用的可扩展性及可维护性。在数据库端使用了Hibernate，它使得与关系数据库打交道变得十分轻松，就像您的数据库中包含每天使用的普通Java对象一样，同时不必考虑如何把它们从神秘的数据库表中取出（或放回到数据库表中）。它解放了您，使您可以专注于应用程序的对象和功能，而不必担心如何保存它们或稍后如何找到它们。</p>
<p>最后，你的J2EE应用系统如果采取上面提到的表现层、服务层和持久层的框架实现，基本你也可以在无需深刻掌握设计模式的情况下开发出一个高质量的应用系统了。</p>
<p>还要注意的是: 开发出一个高质量的J2EE系统还需要正确的业务需求理解，那么域建模提供了一种比较切实可行的正确理解业务需求的方法，相关详细知识可从UML角度结合理解。</p>
<p>当然，如果你想设计自己的行业框架，那么第一步从设计模式开始吧，因为设计模式提供你一个实现JavaBeans或类之间解耦参考实现方法，当你学会了系统基本单元JavaBean或类之间解耦时，那么系统模块之间的解耦你就可能掌握，进而你就可以实现行业框架的提炼了，这又是另外一个发展方向了。</p>
<p>以上理念可以总结为一句话：</p>
<p>J2EE开发三件宝: Domain Model(域建模)、patterns(模式)和framework(框架)。</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/40/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java获取文件夹下所有子文件夹</title>
		<link>http://code.oseschool.com/index.php/archives/5</link>
		<comments>http://code.oseschool.com/index.php/archives/5#comments</comments>
		<pubDate>Tue, 18 Aug 2009 13:39:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=5</guid>
		<description><![CDATA[
import java.io.File;
public class travelAllFile &#123;
	public static void main&#40;String&#91;&#93; args&#41; &#123;
		String path =&#34;E:/a&#34;;
		File f = new File&#40;path&#41;;
		if&#40;f.isDirectory&#40;&#41;&#41;&#123;
			new travelAllFile&#40;&#41;.getFileName&#40;f&#41;;
		&#125;
&#160;
	&#125;
	//递归查找函数，参数为file对象
	public void getFileName&#40;File f&#41;&#123;
		//File 数组
		File&#91;&#93; files = f.listFiles&#40;&#41;;
&#160;
		for &#40;int i = 0; i &#38;lt; files.length; i++&#41; &#123;
			//递归出子目录
			if&#40;files&#91;i&#93;.isDirectory&#40;&#41;&#41;&#123;
				System.out.println&#40;&#34;子目录是：&#34;+files&#91;i&#93;.getName&#40;&#41;&#41;;
				getFileName&#40;files&#91;i&#93;&#41;;
			//递归出子文件
			&#125;else&#123;
				System.out.println&#40;files&#91;i&#93;.getName&#40;&#41;&#41;;
			&#125;
&#160;
		&#125;
	&#125;
&#125;

isDirectory()方法可以判断当前是否为文件夹
]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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;">public</span> <span style="color: #000000; font-weight: bold;">class</span> travelAllFile <span style="color: #009900;">&#123;</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><span style="color: #003399;">String</span><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: #003399;">String</span> path <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;E:/a&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">File</span> f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">isDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">new</span> travelAllFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFileName</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//递归查找函数，参数为file对象</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getFileName<span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span> f<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//File 数组</span>
		<span style="color: #003399;">File</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> files <span style="color: #339933;">=</span> f.<span style="color: #006633;">listFiles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<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;">&amp;</span>lt<span style="color: #339933;">;</span> files.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//递归出子目录</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">isDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;子目录是：&quot;</span><span style="color: #339933;">+</span>files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				getFileName<span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//递归出子文件</span>
			<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>isDirectory()方法可以判断当前是否为文件夹</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

