﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-JAVA EVERY DAY-文章分类-XML</title><link>http://www.blogjava.net/xyy198203/category/20721.html</link><description>DAY DAY UP , STUDY HARD</description><language>zh-cn</language><lastBuildDate>Sun, 18 Mar 2007 02:31:33 GMT</lastBuildDate><pubDate>Sun, 18 Mar 2007 02:31:33 GMT</pubDate><ttl>60</ttl><item><title>JDOM处理XML快速上手--结合XPATH</title><link>http://www.blogjava.net/xyy198203/articles/104403.html</link><dc:creator>没有鱼的水</dc:creator><author>没有鱼的水</author><pubDate>Sat, 17 Mar 2007 02:46:00 GMT</pubDate><guid>http://www.blogjava.net/xyy198203/articles/104403.html</guid><wfw:comment>http://www.blogjava.net/xyy198203/comments/104403.html</wfw:comment><comments>http://www.blogjava.net/xyy198203/articles/104403.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xyy198203/comments/commentRss/104403.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xyy198203/services/trackbacks/104403.html</trackback:ping><description><![CDATA[
		<div>
				<font size="2">
						<strong>XPATH基础<br /></strong>       XPATH遵循文档对象模型(DOM)的路径格式，基本语法由表达式构成。在计算表达式的值之后产生一个对象，这种对象有以下四种基本类型：节点集合、布尔型、数字型和字符</font>
		</div>
		<div>
				<font size="2">串型。XPATH基本上和在文件系统中寻找文件类似，如果路径是以"/"开头的，就表明该路径表示的是一个绝对路径，这和在UNIX系统中关于文件路径的定义是一致的。以"//"开头</font>
		</div>
		<div>
				<font size="2">则表示在文档中的任意位置查找。<br />       以样例XML文档（friends.xml）为例来了解XPATH： 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_104529" onclick="this.style.display='none'; Code_Closed_Text_104529.style.display='none'; Code_Open_Image_104529.style.display='inline'; Code_Open_Text_104529.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_104529" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_104529.style.display='none'; Code_Closed_Image_104529.style.display='inline'; Code_Closed_Text_104529.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_104529" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">xml文件</span><span id="Code_Open_Text_104529" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">  </span><span style="COLOR: #000000">&lt;?</span><span style="COLOR: #000000">xml version</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1.0</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> encoding</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">?&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />       </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">friends comment</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Friends List</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />           </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">friend number</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />               </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">zoof</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />               </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sex value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">male</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />               </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">87654321</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">friend</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">friend number</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">joe</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sex value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">male</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">87654322</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">friend</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">friend number</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">joe</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">name</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sex value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">female</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">87654323</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">phone</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">friend</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">friends</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span></span></div></font>
		</div>
		<div>
				<font size="2">      </font>
				<font size="2">在XML文档中使用位置路径表达式来查找信息，这些表达式有很多种组成方式。一般我们用得最多的恐怕是 节点元素 查找。XPATH中用正斜杠（/）来分隔子结点，返回所有与</font>
		</div>
		<div>
				<font size="2">模式相匹配的元素。下面以几个 表达式 的例子来说明一下返回结果：</font>
		</div>
		<div>
				<font size="2">      表达式：/friends/friend<br />      返回：根元素friends下所有的 friend 元素（或节点）。（当然本例只有一个啦！）</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      表达式：/friends/*<br />      返回：根元素friends下所有的元素（或节点）。（“*”相当于通配符，表示“所有”的）</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      表达式：//friend<br />      返回：任意元素（或节点）下的所有 friend 元素（或节点）。（注意：不仅仅是1中 根元素friends下面的friend元素,如果存在的话）</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      表达式：/friends/friend[@number='1']<br />      返回：根元素下元素名称为 friend，number属性为'1'的全部元素（或节点）。（对于元素或节点的附加元素，比如属性，函数等都要用方括号"[]"扩起来，属性前面要加</font>
		</div>
		<div>
				<font size="2">上"@"号）</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      表达式：/friends/friend/phone[text()='87654321']<br />      返回：元素friends/friend下电话号码为 87654321 的全部元素。（text()是XPATH的函数，功能是取出当前节点的文本内容，即content。）</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      表达式：//name/parent::*<br />      返回：name元素的所有父元素（或节点）。(parent::* 表示这个元素的所有的父节点的集合)</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      上面的介绍对于我们一般的应用基本上够用了，如果你需要进一步的深入，请查看W3C发布的关于XAPH的官方资料。</font>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<strong>
						<font size="2">JDOM+XPATH应用</font>
				</strong>
		</div>
		<div>
				<font size="2">
				</font> </div>
		<div>
				<font size="2">      现在我们开始使用XPATH结合JDOM来操作XML文件了。JDOM的关于XPATH的api只有一个类：Xpath.java，在org.jdom.xpath这个包中。这个类中的核心方法主要是两个静态方法 </font>
		</div>
		<div>
				<font size="2">selectNodes()和selectSingleNode()。前者根据一个xpath语句返回一组节点:List；后者根据一个xpath语句返回符合条件的第一个节点:Object。<br />         public static List selectNodes(Object arg1,String arg2) throws org.jdom.JDOMException;<br />         public static Object selectSingleNode(Object arg1,String arg2) throws org.jdom.JDOMException;<br />      </font>
		</div>
		<div>
				<font size="2">      在使用XPATH之前，请先确定你的classpath路径里面有如下几个JAR包，如果没有，请从JDOM的发行包中lib目录下copy:<br />         saxpaht.jar<br />         jaxen-core.jar<br />         jaxen-jdom.jar</font>
		</div>
		<div>
				<br />
				<font size="2">样列JAVA代码：</font>
				<br />
				<br />
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">package</span>
						<span style="COLOR: #000000">  cyberobject.com.xpath; <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  java.io.IOException;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  java.util.List; <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.Document;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.Element;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.Text;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.JDOMException;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.input.SAXBuilder;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />     </span>
						<span style="COLOR: #0000ff">import</span>
						<span style="COLOR: #000000">  org.jdom.xpath.XPath; <br /><img id="Codehighlighter1_280_285_Open_Image" onclick="this.style.display='none'; Codehighlighter1_280_285_Open_Text.style.display='none'; Codehighlighter1_280_285_Closed_Image.style.display='inline'; Codehighlighter1_280_285_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_280_285_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_280_285_Closed_Text.style.display='none'; Codehighlighter1_280_285_Open_Image.style.display='inline'; Codehighlighter1_280_285_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />     </span>
						<span id="Codehighlighter1_280_285_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/** */</span>
						<span id="Codehighlighter1_280_285_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000"> </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000"> </span>
						<span id="Codehighlighter1_287_454_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/** */</span>
						<span id="Codehighlighter1_287_454_Open_Text">
								<span style="COLOR: #008000">/**</span>
								<span style="COLOR: #008000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *  </span>
								<span style="COLOR: #808080">@author</span>
								<span style="COLOR: #008000">  wanghua<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     *<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     * TODO To change the template for this generated type comment go to<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     * Window - Preferences - Java - Code Style - Code Templates<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />      </span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000"> <br /><img id="Codehighlighter1_487_2067_Open_Image" onclick="this.style.display='none'; Codehighlighter1_487_2067_Open_Text.style.display='none'; Codehighlighter1_487_2067_Closed_Image.style.display='inline'; Codehighlighter1_487_2067_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_487_2067_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_487_2067_Closed_Text.style.display='none'; Codehighlighter1_487_2067_Open_Image.style.display='inline'; Codehighlighter1_487_2067_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />      </span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">class</span>
						<span style="COLOR: #000000"> XpathSample   </span>
						<span id="Codehighlighter1_487_2067_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_487_2067_Open_Text">
								<span style="COLOR: #000000">{<br /><img id="Codehighlighter1_567_2063_Open_Image" onclick="this.style.display='none'; Codehighlighter1_567_2063_Open_Text.style.display='none'; Codehighlighter1_567_2063_Closed_Image.style.display='inline'; Codehighlighter1_567_2063_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_567_2063_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_567_2063_Closed_Text.style.display='none'; Codehighlighter1_567_2063_Open_Image.style.display='inline'; Codehighlighter1_567_2063_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />          </span>
								<span style="COLOR: #0000ff">public</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">static</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">void</span>
								<span style="COLOR: #000000"> main(String[] args) </span>
								<span style="COLOR: #0000ff">throws</span>
								<span style="COLOR: #000000"> JDOMException, IOException  </span>
								<span id="Codehighlighter1_567_2063_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.blogjava.net/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_567_2063_Open_Text">
										<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               SAXBuilder sb </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">new</span>
										<span style="COLOR: #000000">  SAXBuilder();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               Document doc  </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> sb.build( </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> friends.xml </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               Element root  </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> doc.getRootElement();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />      <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               List list </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> XPath.selectNodes(root, </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> /friends/friend </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               System.out.println( </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> [info] Friends List: </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               <br /><img id="Codehighlighter1_935_1985_Open_Image" onclick="this.style.display='none'; Codehighlighter1_935_1985_Open_Text.style.display='none'; Codehighlighter1_935_1985_Closed_Image.style.display='inline'; Codehighlighter1_935_1985_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_935_1985_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_935_1985_Closed_Text.style.display='none'; Codehighlighter1_935_1985_Open_Image.style.display='inline'; Codehighlighter1_935_1985_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />               </span>
										<span style="COLOR: #0000ff">for</span>
										<span style="COLOR: #000000"> ( </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000">  ii  </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000">   </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000"> ; ii  </span>
										<span style="COLOR: #000000">&lt;</span>
										<span style="COLOR: #000000">  list.size(); ii </span>
										<span style="COLOR: #000000">++</span>
										<span style="COLOR: #000000"> )  </span>
										<span id="Codehighlighter1_935_1985_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
												<img src="http://www.blogjava.net/images/dot.gif" />
										</span>
										<span id="Codehighlighter1_935_1985_Open_Text">
												<span style="COLOR: #000000">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  Element element  </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000">  (Element)list.get(ii);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  String number  </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000">  element.getAttributeValue( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> number </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />       <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  </span>
												<span style="COLOR: #008000">//</span>
												<span style="COLOR: #008000"> String name = element.getChildText("name"); </span>
												<span style="COLOR: #008000">
														<br />
														<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
												</span>
												<span style="COLOR: #000000">                  String name  </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000">  ( (Text) XPath.selectSingleNode(element, </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> //friend[@number=' </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  number  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ']/name/text() </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> )).getTextNormalize(); <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  </span>
												<span style="COLOR: #008000">//</span>
												<span style="COLOR: #008000"> String sex = element.getChild("sex").getAttributeValue("value"); </span>
												<span style="COLOR: #008000">
														<br />
														<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
												</span>
												<span style="COLOR: #000000">                  String sex  </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000">  ((Element)XPath.selectSingleNode(element, </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> //friend[@number=' </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  number  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ']/sex </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> )).getAttributeValue( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> value </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                   <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  </span>
												<span style="COLOR: #008000">//</span>
												<span style="COLOR: #008000"> String phone = element.getChildText("phone"); </span>
												<span style="COLOR: #008000">
														<br />
														<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
												</span>
												<span style="COLOR: #000000">                  String phone  </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000">  ( (Text) XPath.selectSingleNode(element, </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> //friend[@number=' </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  number  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ']/phone/text() </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> )).getText();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                   <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  System.out.println( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> No. </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000"> number);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  System.out.println( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">  Name:[ </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  name  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ] </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  System.out.println( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">  Sex:[ </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  sex  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ] </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  System.out.println( </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">  Phone:[ </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">  phone  </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000">   </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> ] </span>
												<span style="COLOR: #000000">"</span>
												<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                  System.out.println();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />               }</span>
										</span>
										<span style="COLOR: #000000">   <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />               System.out.println( </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> [info] End of Friends List. </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span>
								</span>
								<span style="COLOR: #000000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">  </span>
				</div>
				<br />
				<font size="2">输出结果：</font>
		</div>
		<div>
				<font size="2">[info] Friends List:<br />No.1<br /> Name:[zoof]<br /> Sex:[male]<br /> Phone:[87654321]</font>
		</div>
		<div>
				<font size="2">No.2<br /> Name:[joe]<br /> Sex:[male]<br /> Phone:[87654322]</font>
		</div>
		<div>
				<font size="2">No.3<br /> Name:[joe]<br /> Sex:[female]<br /> Phone:[87654323]</font>
		</div>
		<div>
				<font size="2">[info] End of Friends List.</font>
		</div>
<img src ="http://www.blogjava.net/xyy198203/aggbug/104403.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xyy198203/" target="_blank">没有鱼的水</a> 2007-03-17 10:46 <a href="http://www.blogjava.net/xyy198203/articles/104403.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>