<?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>机械境-信永软件工程师培训学校</title>
	<atom:link href="http://code.oseschool.com/index.php/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>spring tips!</title>
		<link>http://code.oseschool.com/index.php/archives/79</link>
		<comments>http://code.oseschool.com/index.php/archives/79#comments</comments>
		<pubDate>Fri, 05 Mar 2010 02:40:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[undefind]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=79</guid>
		<description><![CDATA[1、关于Spring的xml配置文件，文件命名是否固定，文件位置可以放在什么地方？
文件名可以随便书写，只要是文件名就可以，不能包含特殊符号。  文件位置可以放置在src，web路径，系统路径，ftp，http等。。只要可能是一个url就可以进行装载。
具体查看spring文档第四章的resource
2、在用xml配置bean的时候，用指定方法注入，方法参数为可变长的的可以不？
不可，属性注入只能为单一属性注入。
具体查看spring文档的3.3.1.2
3、注入数组？
使用list节点具体查看spring文档的3.3.2.4 collections
4、&#60;rel&#62;节点的local属性和name属性的区别？
local必须指定同一文件下的bean的id ，如果不为同一个xml文件会报错。
name为指定同一容器下或者父容器的bean的name（包含id）且可以不为同一个xml文件。
具体查看spring文档的3.3.2.2
5、关于监听器和事件（具体的题目没听清楚，记得是阎总问的）
applicationContext有5个默认事件，程序员也可以自定义事件和驱动事件，实现ApplicationEvent接口，和
使用ctx.publishEvent(event)方法。   监听事件的类必须实现ApplicationListener接口，而且为spring管理的类。
具体查看spring文档的3.8.3
6、注解可以直接作用在属性上？注解都可以作用在什么地方？
根据不同的注解有不同的域，详细可以看java 注解的使用。注解可以使用在类，属性，方法等。
7、使用注解注入，scope是单例还是？
默认为单例，也可以是用@scope指定
具体查看spring文档的3.12.5
8、在xml配置的时候， exclude-filter 和include-filter 所是使用的表达式是同一个的时候，结果如何。（
具体的标签名我不知道，你自己补充完整下）
当加入一个bean和去除一个bean冲突的时候，去除一个bean为优先。
具体查看spring文档的3.12.3
9.如果一个bean是person，他在构造函数中注入自己（person对象）是不是不能注入。有时候在外部建了一个
person，赋值过去，spring可以吗？
在构造函数中注入自己是不行的，因为会导致死锁。
外部建了一个person是什么意思？
可以考虑这么做
在xml中配置两个person的bean ，比如 a 和b ， a需要注入一个person ， b 不要求注入person， 这样子，就注入b就可以了，
10.属性注入时，能不能利用spring传参，在setter里做很多操作，这样子像印象了bean的规范，是不是可以骗
过spring，它只是传参。
可以，spring只是负责调用你的方法，把属性注入进去而已，你里面想写任何操作都可以，不影响javabean规
范，只要有setter方法就行。。
11.在工学项目里每次修改配置文件都需要重启tomcat，如果项目已上线，所有人都在用，又不得不改里头的类
1-》类2，那怎么办，配置文件不是在启动时就加载吗，spring可以设置修改xml的监听器吗。
第一个：现阶段的框架只能进行修改后重新启动，不过spring3.0已经实现了osgi，分模块管理，使用osgi就可
以实现模块的热插拔，不用重新启动服务器，如淘宝就是一个例子。如果我们想简单的实现的话，可以操作
webapplicationContext进行close和重新加载。。这个比较土。
第二个：现在spring还没有能直接扫描xml改变的组件，也没有这个事件，如果时间允许，也可以自己写个。
有兴趣可以去研究一下spring3.0。
12.spring事件方面可以补充讲下。
详细内容在spring文档的3.8.3，第三章，第8节的第三小节，application节下。
12.是否可以注入空值？若可以，注入空值，@request是什么效果。
可以注入空值。使用 &#60;value /&#62;的话效果等于 setName(&#8221;) 。使用&#60;null /&#62;的话效果等于  setName(null);
@Required 注解不会报错，因为它只要有调用到方法就已经满足了。。不管是注入null还是&#8221;
详细内容查看spring文档的3.3.2.5
13.aop的原理，与hibernate的集成，在项目中是否常用
技术原理底层使用了AspectJ 组件。与hibernate的集成可以具体查看spring文档12.2
aop最常用使用在事务等。
14.spring 与 struts2.X的集成。
具体查看struts2文档的Plugin Developers Guide下的spring Plugin
]]></description>
			<content:encoded><![CDATA[<p><strong>1、关于Spring的xml配置文件，文件命名是否固定，文件位置可以放在什么地方？</strong><br />
文件名可以随便书写，只要是文件名就可以，不能包含特殊符号。  文件位置可以放置在src，web路径，系统路径，ftp，http等。。只要可能是一个url就可以进行装载。<br />
具体查看spring文档第四章的resource</p>
<p><strong>2、在用xml配置bean的时候，用指定方法注入，方法参数为可变长的的可以不？</strong><br />
不可，属性注入只能为单一属性注入。<br />
具体查看spring文档的3.3.1.2</p>
<p><strong>3、注入数组？</strong><br />
使用list节点具体查看spring文档的3.3.2.4 collections</p>
<p><strong>4、&lt;rel&gt;节点的local属性和name属性的区别？</strong><br />
local必须指定同一文件下的bean的id ，如果不为同一个xml文件会报错。<br />
name为指定同一容器下或者父容器的bean的name（包含id）且可以不为同一个xml文件。<br />
具体查看spring文档的3.3.2.2</p>
<p><strong>5、关于监听器和事件（具体的题目没听清楚，记得是阎总问的）</strong><br />
applicationContext有5个默认事件，程序员也可以自定义事件和驱动事件，实现ApplicationEvent接口，和<br />
使用ctx.publishEvent(event)方法。   监听事件的类必须实现ApplicationListener接口，而且为spring管理的类。<br />
具体查看spring文档的3.8.3</p>
<p><strong>6、注解可以直接作用在属性上？注解都可以作用在什么地方？</strong><br />
根据不同的注解有不同的域，详细可以看java 注解的使用。注解可以使用在类，属性，方法等。</p>
<p><strong>7、使用注解注入，scope是单例还是？</strong><br />
默认为单例，也可以是用@scope指定<br />
具体查看spring文档的3.12.5</p>
<p><strong>8、在xml配置的时候， exclude-filter 和include-filter 所是使用的表达式是同一个的时候，结果如何。（<br />
具体的标签名我不知道，你自己补充完整下）</strong></p>
<p>当加入一个bean和去除一个bean冲突的时候，去除一个bean为优先。<br />
具体查看spring文档的3.12.3</p>
<p><strong>9.如果一个bean是person，他在构造函数中注入自己（person对象）是不是不能注入。有时候在外部建了一个<br />
person，赋值过去，spring可以吗？</p>
<p><span style="font-weight: normal;">在构造函数中注入自己是不行的，因为会导致死锁。<br />
外部建了一个person是什么意思？</p>
<p>可以考虑这么做<br />
在xml中配置两个person的bean ，比如 a 和b ， a需要注入一个person ， b 不要求注入person， 这样子，就注入b就可以了，</span></strong></p>
<p><strong>10.属性注入时，能不能利用spring传参，在setter里做很多操作，这样子像印象了bean的规范，是不是可以骗<br />
过spring，它只是传参。</strong><br />
可以，spring只是负责调用你的方法，把属性注入进去而已，你里面想写任何操作都可以，不影响javabean规<br />
范，只要有setter方法就行。。</p>
<p><strong>11.在工学项目里每次修改配置文件都需要重启tomcat，如果项目已上线，所有人都在用，又不得不改里头的类</strong></p>
<p><strong>1-》类2，那怎么办，配置文件不是在启动时就加载吗，spring可以设置修改xml的监听器吗。</strong><br />
第一个：现阶段的框架只能进行修改后重新启动，不过spring3.0已经实现了osgi，分模块管理，使用osgi就可<br />
以实现模块的热插拔，不用重新启动服务器，如淘宝就是一个例子。如果我们想简单的实现的话，可以操作<br />
webapplicationContext进行close和重新加载。。这个比较土。<br />
第二个：现在spring还没有能直接扫描xml改变的组件，也没有这个事件，如果时间允许，也可以自己写个。<br />
有兴趣可以去研究一下spring3.0。</p>
<p><strong>12.spring事件方面可以补充讲下。</strong><br />
详细内容在spring文档的3.8.3，第三章，第8节的第三小节，application节下。</p>
<p><strong>12.是否可以注入空值？若可以，注入空值，@request是什么效果。</strong><br />
可以注入空值。使用 &lt;value /&gt;的话效果等于 setName(&#8221;) 。使用&lt;null /&gt;的话效果等于  setName(null);<br />
@Required 注解不会报错，因为它只要有调用到方法就已经满足了。。不管是注入null还是&#8221;<br />
详细内容查看spring文档的3.3.2.5</p>
<p><strong>13.aop的原理，与hibernate的集成，在项目中是否常用</strong><br />
技术原理底层使用了AspectJ 组件。与hibernate的集成可以具体查看spring文档12.2<br />
aop最常用使用在事务等。</p>
<p><strong>14.spring 与 struts2.X的集成。</strong></p>
<p>具体查看struts2文档的Plugin Developers Guide下的spring Plugin</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/79/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JAVA正则表达式 与 JS的正则表达式有何区别</title>
		<link>http://code.oseschool.com/index.php/archives/77</link>
		<comments>http://code.oseschool.com/index.php/archives/77#comments</comments>
		<pubDate>Fri, 05 Mar 2010 01:56:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[undefind]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=77</guid>
		<description><![CDATA[1.[a-z]在java中匹配一个英文字母。在javascript中，被测试字符串中包含一个英文字幕都被被接受。
2。[a-z]*java匹配纯英文字母。^[a-z]$在javascript中匹配纯英文字母。
主要区别为，一个正则表达式，在java中要求被测试字符串完全匹配，在javascript中要求，只要字符串中有一部分匹配就可以。
上边的这个结论对java和javascript中的理解不透彻：
javascript:
match和exec的函数的目的是找到字符串中，符合模式的字符串
match和exec找到尽可能多的字符串
对字符串!取得的是false，对null进行!取得的是true
而java直接得到的就是true或false。
关于javascript的正则表达式。
field.value.match(&#8217;[a-z]&#8216;)与（Struts2 Validation这么使用的）
field.value.match([a-z])含义相同（有的资料上这么说的）
有的资料在讲的时候说正则表达式必须包含在\之间，有的没说。
关于转义字符
javascript
field = &#8216;_jimjimf112gds&#8217;
alert(field.match(/w+/))这样匹配所有字符串。但当使用&#8221;时，必须对\进行转义。也就是说 alert(field.match(&#8217;\w+&#8217;))匹配所有字符串。alert(field.match(&#8217;w+&#8217;))匹配的是w字母。这是因为字符串 使用斜线作为转义字符。&#8217;w'中的被忽略。也就是：alert(field.match(&#8217;w+&#8217;))匹配的是w字母。这就体现出在使用\包含表达式时的 优势了——不必转义
在java中也有字符串转义的问题：
System.out.println(Pattern.matches(&#8217;\w+&#8217;,'dfsasdf&#8217;));
为true，而System.out.println(Pattern.matches(&#8217;w+&#8217;,'dfsasdf&#8217;));是不能被编译的。
所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符，可以用来规定其前导字符（即位于元字符前面的字符）在目标对象中的出现模式。
限定符可以指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。
定位符用于规定匹配模式在目标对象中的出现位置。
field = &#8216;jimwtoo,totooaztA3213142jimtof112gds&#8217;
alert(field.match(/[^a-c]+/))=jimwtoo,totoo
而alert(field.match(/[^a-c]/))=j
结论是：匹配尽可能多的字符串，是在含有+或*的情况下。
关于javascript优先级：
1． 转义符
2．(), (?:), (?=), [] 圆括号和方括号
3．*, +, ?, {n}, {n,}, {n,m} 限定符
4．^, $, anymetacharacter 位置和顺序
5．&#124;“或”操作
]]></description>
			<content:encoded><![CDATA[<p>1.[a-z]在java中匹配一个英文字母。在javascript中，被测试字符串中包含一个英文字幕都被被接受。<br />
2。[a-z]*java匹配纯英文字母。^[a-z]$在javascript中匹配纯英文字母。<br />
主要区别为，一个正则表达式，在java中要求被测试字符串完全匹配，在javascript中要求，只要字符串中有一部分匹配就可以。<br />
上边的这个结论对java和javascript中的理解不透彻：<br />
javascript:<br />
match和exec的函数的目的是找到字符串中，符合模式的字符串<br />
match和exec找到尽可能多的字符串<br />
对字符串!取得的是false，对null进行!取得的是true<br />
而java直接得到的就是true或false。</p>
<p>关于javascript的正则表达式。<br />
field.value.match(&#8217;[a-z]&#8216;)与（Struts2 Validation这么使用的）<br />
field.value.match([a-z])含义相同（有的资料上这么说的）<br />
有的资料在讲的时候说正则表达式必须包含在\之间，有的没说。</p>
<p>关于转义字符<br />
javascript<br />
field = &#8216;_jimjimf112gds&#8217;<br />
alert(field.match(/w+/))这样匹配所有字符串。但当使用&#8221;时，必须对\进行转义。也就是说 alert(field.match(&#8217;\w+&#8217;))匹配所有字符串。alert(field.match(&#8217;w+&#8217;))匹配的是w字母。这是因为字符串 使用斜线作为转义字符。&#8217;w'中的被忽略。也就是：alert(field.match(&#8217;w+&#8217;))匹配的是w字母。这就体现出在使用\包含表达式时的 优势了——不必转义<br />
在java中也有字符串转义的问题：<br />
System.out.println(Pattern.matches(&#8217;\w+&#8217;,'dfsasdf&#8217;));<br />
为true，而System.out.println(Pattern.matches(&#8217;w+&#8217;,'dfsasdf&#8217;));是不能被编译的。</p>
<p>所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符，可以用来规定其前导字符（即位于元字符前面的字符）在目标对象中的出现模式。<br />
限定符可以指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。<br />
定位符用于规定匹配模式在目标对象中的出现位置。</p>
<p>field = &#8216;jimwtoo,totooaztA3213142jimtof112gds&#8217;<br />
alert(field.match(/[^a-c]+/))=jimwtoo,totoo<br />
而alert(field.match(/[^a-c]/))=j<br />
结论是：匹配尽可能多的字符串，是在含有+或*的情况下。</p>
<p>关于javascript优先级：<br />
1． 转义符<br />
2．(), (?:), (?=), [] 圆括号和方括号<br />
3．*, +, ?, {n}, {n,}, {n,m} 限定符<br />
4．^, $, anymetacharacter 位置和顺序<br />
5．|“或”操作</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/77/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>SQL查询与优化</title>
		<link>http://code.oseschool.com/index.php/archives/72</link>
		<comments>http://code.oseschool.com/index.php/archives/72#comments</comments>
		<pubDate>Tue, 23 Feb 2010 03:14:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=72</guid>
		<description><![CDATA[精彩回答问题一:
exists的用法?
答:EXISTS的执行流程
select * from t1 where exists ( select null from t2 where y = x )
可以理解为:
  for x in ( select * from t1 )
  loop
      if ( exists ( select null from t2 where y = x.x )
      then
    [...]]]></description>
			<content:encoded><![CDATA[<p>精彩回答问题一:<br />
exists的用法?<br />
答:EXISTS的执行流程<br />
select * from t1 where exists ( select null from t2 where y = x )<br />
可以理解为:<br />
  for x in ( select * from t1 )<br />
  loop<br />
      if ( exists ( select null from t2 where y = x.x )<br />
      then<br />
        OUTPUT THE RECORD<br />
      end if<br />
  end loop<br />
对于in 和 exists的性能区别:<br />
  如果子查询得出的结果集记录较少，主查询中的表较大且又有索引时应该用in,反之如果外层的主查询记录较少，子查询中的表大，又有索引时使用exists。<br />
  其实我们区分in和exists主要是造成了驱动顺序的改变（这是性能变化的关键），如果是exists，那么以外层表为驱动表，先被访问，如果是IN，那么先执行子查询，所以我们会以驱动表的快速返回为目标，那么就会考虑到索引及结果集的关系了<br />
另外IN是不对NULL进行处理<br />
如：<br />
select 1 from dual where null  in (0,1,2,null) 为空</p>
<p>精彩回答问题二:<br />
如何处理空值排序?<br />
答:<br />
例:select * from emp order by comm desc nulls last(first);<br />
使用nulls last,把将要排序的列,凡是空值的排在最后面<br />
使用nulls first,把将要排序的列,凡是空值的排在最前面</p>
<p>精彩回答问题三:<br />
如何得到当前行上一行或者下一行的数据?<br />
答:<br />
例:select ename,sal,lead(sal) over(order by sal) aaa ,lag(sal) over(order by sal) bbb from emp;<br />
这些函数为oracle的窗口函数,在mysql不适用,但是mysql会有这种功能的函数吗??(疑问)</p>
<p>疑问一:如果没有索引,id!=0与id<0 or id>0,哪个速度更快?</p>
<p>疑问二:检索字符串与数值时,哪个速度比较快?例:dy=&#8221;1KV以下&#8221; and dy_2=1与 dy_2=1 and dy=&#8221;1KV以下&#8221;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;update&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>B树索引速度的问题</p>
<p>我们都知道，用索引快，可是为什么快，到底会快多少？<br />
比如：有100000条记录，需要定位name=‘yannaiyong’的这条记录，在没有索引的情况下，可能最大要查询10W次，才能定位到该记录，如果有了索引，最多要查询多少次？这样比较一下，就知道有索引和无索引查询速度不是同一个数量级的。<br />
请问：如果有了索引，最多要查询多少次？同时讲解下B树索引定位或排序的原理？ </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;update&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>若在索引列在使用函数或者进行计算，则在查询的时候，不会用到该索引。如：student表（id,name,age),在age字段上有索引。则<br />
select * from student where age>30 (会用到索引）<br />
select * from student where age-20>10(不会用到索引）</p>
<p>但如果在age字段上建立这样的索引<br />
create index age_index on student(age-20);则：<br />
select * from student where age-20>10(就会用到索引）</p>
<p>备注：在字段上建立这样的索引，需要比较高的权限。比如sys或者system</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;update&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>精彩回答问题三:<br />
如何得到当前行上一行或者下一行的数据?<br />
答:<br />
例:select ename,sal,lead(sal) over(order by sal) aaa ,lag(sal) over(order by sal) bbb from emp;<br />
这些函数为oracle的窗口函数,在mysql不适用,但是mysql会有这种功能的函数吗??(疑问)</p>
<p>MYSQL没有查到有这样的函数。<br />
在Oracle中，窗口函数其实还有很多，比如:sum(sal) over(),sum(sal) over(order by deptno),sum(sal) over(partition by deptno)等都存在细微差别，大家可以执行下，看看结果就知道差别了。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;update&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>疑问一:如果没有索引,id!=0与id<0 or id>0,哪个速度更快?</p>
<p>回答：</p>
<p>没有索引，检索表都是全表扫描，速度并没有明显差别。</p>
<p>实验：</p>
<p>1000条的数据，用id!=0        用时17.1m<br />
                id<0 or id>0 用时16.8m</p>
<p>建议没有索引的情况，用id!=0,因为毕竟看起来比较舒服。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;update&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>疑问二:检索字符串与数值时,哪个速度比较快?例:dy=&#8221;1KV以下&#8221; and dy_2=1与 dy_2=1 and dy=&#8221;1KV以下&#8221;</p>
<p>回答：</p>
<p>与上面的回答一样，没有索引的情况下。都是全表扫描，而Oracle在数据排序优化上已经比较成熟，检索字符串与数值，可能会有略微差别，但速度基本一致。</p>
<p>SQL的性能最重要的因素还是取决于是否有充分利用索引。 </p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/72/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>鲜果来吧</title>
		<link>http://code.oseschool.com/index.php/archives/53</link>
		<comments>http://code.oseschool.com/index.php/archives/53#comments</comments>
		<pubDate>Mon, 24 Aug 2009 14:15:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[undefind]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=53</guid>
		<description><![CDATA[BANG67D9B58B528B0DDFCE678FE3XIANGUO
]]></description>
			<content:encoded><![CDATA[<p>BANG67D9B58B528B0DDFCE678FE3XIANGUO</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/53/feed</wfw:commentRss>
		<slash:comments>0</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>
	</channel>
</rss>
