Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2093281
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2011-10-31 11:42:53

(From: http://blog.csdn.net/ichliebephone/article/details/5971533)

一.基础知识

在《Android XML解析学习——Sax方式》中我们学习了Android上使用JavaSAX进行XML解析的方式。而在Android平台上使用SAX解析XML,除了使用JavaAPI外,还可以使用Android SDK带的 ="EN-US">API来实现。Android SDK中和SAX解析相关的包为android.sax,在这个包中Android提供了都有的SAX API,使用它们可以更加方便的进行SAX方式的XML解析。

xml文件中在不同的位置处有相同的元素标签名时,在相应的事件回调处理函数中往往就需要进行判断处理。比如USGSxml形式的地震数据为:

  1. xml version="1.0"?>  
  2. <feed xmlns="" xmlns:georss="">  
  3.   <updated>2010-09-15T04:41:18Zupdated>  
  4.   <title>USGS M2.5+ Earthquakestitle>  
  5.   <subtitle>Real-time, worldwide earthquake list for the past daysubtitle>  
  6.   <link rel="self" href="" mce_href=""/>  
  7.   <link href="" mce_href=""/>  
  8.   <author><name>U.S. Geological Surveyname>author>  
  9.   <id>id>  
  10.   <icon>/favicon.icoicon>  
  11.   <entry>  
  12.       <id>urn:earthquake-usgs-gov:ak:10078833id>  
  13.       <title>M 2.9, Southern Alaskatitle>  
  14.       <updated>2010-09-15T04:14:03Zupdated>  
  15.       <link rel="alternate" type="text/html" href="recenteqsww/Quakes/ak10078833.php" mce_href="recenteqsww/Quakes/ak10078833.php"/>  
  16.       <summary type="html">  
  17.           
  18.       summary>  
  19.       <georss:point>59.9094 -153.1241georss:point>  
  20.       <georss:elev>-98900georss:elev>  
  21.       <category label="Age" term="Past hour"/>  
  22.   entry>  
  23.    <entry>  
  24.       
  25.   entry>  
  26. feed>  

 

我们看到在开始前就有</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang="EN-US"><span style="font-family: Times New Roman;"><link></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等元素标签,而</span><span lang="EN-US"><span style="font-family: Times New Roman;"><entry></entry></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">元素中也包含这些标签名,在</span><span lang="EN-US"><span style="font-family: Times New Roman;">SAX</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">解析时都会产生相应的事件,但我们实际需要处理的为</span><span lang="EN-US"><span style="font-family: Times New Roman;"><entry></entry></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">元素中的这些标签产生的事件,因此我们设置了一个变量</span></span></p> <p> </p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="keyword">private</span><span> Boolean startEntryElementFlag = </span><span class="keyword">false</span><span>;  </span></span></li></ol></div> </span></span></p> <p> </p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span style="font-size: small;"><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">来进行判断。对以上的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">xml</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">数据来说,这样的处理还不会出现问题,但是如果需要解析一个更加复杂的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">XML</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">文档,则类似的需要对不同位置处的相同标签名进行判断这样的处理可能会带来各种各样的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">Bug</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">而使用</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">android.sax</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">包中的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">API</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">来进行</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">SAX</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">方式的解析则不会有以上的问题。实际上使用</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">android.sax</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">包还有点结合了我们以后会详细介绍的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">DOM</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">方式,首先获取需要解析部分的根元素,然后使用</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">getChild</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">方法获取具体的某个子元素,之后就可以为具体的元素设置事件处理的回调函数,比如为一个元素分别设置元素开始的事件处理</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">setStartElementListener</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">,元素包含的文本内容结束的事件处理</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">setEndTextElementListener</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">和元素结束的事件处理</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">setEndElementListener</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">概括的来说,</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">android.sax</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">包进行</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">XML</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">解析的过程为用</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">DOM</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">的方式获取具体位置处的元素,然后为其设置需要的事件处理函数。具体的实现我们可以看实例开发部分的代码。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">Android SDK</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">中提供的和</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">XML</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">相关的还有一个类:</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">android.util.Xml</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">,在该类中提供了比较实用的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">XML</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">相关的类方法,比如开始解析的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">parse</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">方法,和直接创建</span><span lang="EN-US"><span style="font-family: Times New Roman;">XmlPullParser</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">及</span><span lang="EN-US"><span style="font-family: Times New Roman;">XmlSerializer</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(这两块内容以后介绍)的方法等。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span lang="EN-US"><span style="font-size: small; font-family: Times New Roman;"> </span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面我们就用上面介绍的</span><span lang="EN-US"><span style="font-family: Times New Roman;">Android SDK</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span><span lang="EN-US"><span style="font-family: Times New Roman;">SAX</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方式来实现解析</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">形式的</span><span lang="EN-US"><span style="font-family: Times New Roman;">USGS</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">地震数据的</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例子。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 0cm; mso-list: l0 level1 lfo1"><span style="font-size: small;"><span style="mso-bidi-font-family: 宋体" lang="EN-US"><span style="mso-list: Ignore"><span style="font-family: Times New Roman;">二.</span></span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例开发</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们要完成的效果图如下图</span><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示:</span></span></p> <p> </p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: center"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><img src="" alt=""></span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: center"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</span><span lang="EN-US"><span style="font-family: Times New Roman;">1 ListView</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">列表显示的地震数据</span></span></span></p> <p class="MsoNormal" style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; text-align: left; "> </p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和上一部分</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例子的一样,也是解析完地震数据后用</span><span lang="EN-US"><span style="font-family: Times New Roman;">ListView</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">列表的方式显示每条地震的震级和地名信息。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">新建一个</span><span lang="EN-US"><span style="font-family: Times New Roman;">Android</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工程</span><span lang="EN-US"><span style="font-family: Times New Roman;">AndroidXMLDemoSaxII</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">添加进上一个</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工程</span><span lang="EN-US"><span style="font-family: Times New Roman;">AndroidXMLDemoSax</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span><span lang="EN-US"><span style="font-family: Times New Roman;">EarthquakeEntry.java</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件,如果需要从本地读取</span><span lang="EN-US"><span style="font-family: Times New Roman;">xml</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">数据的话,同时在</span><span lang="EN-US"><span style="font-family: Times New Roman;">assets</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件夹下添加保存为</span><span lang="EN-US"><span style="font-family: Times New Roman;">xml</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">格式了的</span><span lang="EN-US"><span style="font-family: Times New Roman;">USGS</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">地震数据</span><span lang="EN-US"><span style="font-family: Times New Roman;">USGS_Earthquake_1M2_5.xml</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,如果需要联网读取的话,在</span><span lang="EN-US"><span style="font-family: Times New Roman;">manifest.xml</span></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件中添加权限:</span></span></p> <p> </p> <div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span><uses-permission android:name=</span><span class="string">"android.permission.INTERNET"</span><span> />  </span></span></li></ol></div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="font-size: small;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">并修改</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">res/layout</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">下的</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US">main.xml</span><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white">为:</span></span></p> <p> </p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white" lang="EN-US"><div class="dp-highlighter bg_xhtml"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-xml" start="1"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">xml</span><span> </span><span class="attribute">version</span><span>=</span><span class="attribute-value">"1.0"</span><span> </span><span class="attribute">encoding</span><span>=</span><span class="attribute-value">"utf-8"</span><span class="tag">?></span><span>  </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">LinearLayout</span><span> </span><span class="attribute">xmlns:android</span><span>=</span><span class="attribute-value">""</span><span>  </span></span></li><li class="alt"><span>    <span class="attribute">android:orientation</span><span>=</span><span class="attribute-value">"vertical"</span><span>  </span></span></li><li class=""><span>    <span class="attribute">android:layout_width</span><span>=</span><span class="attribute-value">"fill_parent"</span><span>  </span></span></li><li class="alt"><span>    <span class="attribute">android:layout_height</span><span>=</span><span class="attribute-value">"fill_parent"</span><span>  </span></span></li><li class=""><span>    <span class="tag">></span><span>  </span></span></li><li class="alt"><span>  <span class="tag"><</span><span class="tag-name">ListView</span><span>  </span></span></li><li class=""><span>    <span class="attribute">android:id</span><span>=</span><span class="attribute-value">"@+id/list"</span><span>  </span></span></li><li class="alt"><span>    <span class="attribute">android:layout_width</span><span>=</span><span class="attribute-value">"fill_parent"</span><span>   </span></span></li><li class=""><span>    <span class="attribute">android:layout_height</span><span>=</span><span class="attribute-value">"fill_parent"</span><span>  </span></span></li><li class="alt"><span>  <span class="tag">/></span><span>  </span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">LinearLayout</span><span class="tag">></span><span>  </span></span></li></ol></div> </span></p> <p> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">接下来就来新建添加一个类</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">AndroidSaxEarthquakeHandler</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">,以</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">Android SDK</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">提供的包</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">android.sdk</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">的</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">API</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">来</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">完成解析地震数据的具体逻辑实现,内容如下:</span></span></p> <p> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US"><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> AndroidSaxEarthquakeHandler {  </span></span></li><li class=""><span>    <span class="comment">//xml解析用到的Tag</span><span>  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> String kRootElementName = </span><span class="string">"feed"</span><span>;  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> String kEntryElementName = </span><span class="string">"entry"</span><span>;  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> String kLinkElementName = </span><span class="string">"link"</span><span>;  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> String kTitleElementName = </span><span class="string">"title"</span><span>;  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> String kUpdatedElementName = </span><span class="string">"updated"</span><span>;  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> String kGeoRSSPointElementName = </span><span class="string">"point"</span><span>;  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> String kGeoRSSElevElementName = </span><span class="string">"elev"</span><span>;  </span></span></li><li class=""><span>    <span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String ATOM_NAMESPACE = </span><span class="string">""</span><span>;   </span></span></li><li class="alt"><span>    <span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String GEORSS_NAMESPACE = </span><span class="string">""</span><span>;  </span></span></li><li class=""><span>    <span class="comment">//用于保存xml解析获取的结果</span><span>  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> ArrayList<EarthquakeEntry> earthquakeEntryList;  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> EarthquakeEntry earthquakeEntry;  </span></span></li><li class="alt"><span>      </span></li><li class=""><span>    <span class="comment">//解析xml数据</span><span>  </span></span></li><li class="alt"><span>    <span class="keyword">public</span><span> ArrayList<EarthquakeEntry> parse(InputStream inStream)  </span></span></li><li class=""><span>    {  </span></li><li class="alt"><span>        earthquakeEntryList = <span class="keyword">new</span><span> ArrayList<EarthquakeEntry>();  </span></span></li><li class=""><span>        RootElement root = <span class="keyword">new</span><span> RootElement(ATOM_NAMESPACE, kRootElementName);  </span></span></li><li class="alt"><span>        Element entry = root.getChild(ATOM_NAMESPACE, kEntryElementName);  </span></li><li class=""><span>        <span class="comment">//具体解析xml</span><span>  </span></span></li><li class="alt"><span>        <span class="comment">//处理entry标签</span><span>  </span></span></li><li class=""><span>        entry.setStartElementListener(<span class="keyword">new</span><span> StartElementListener() {        </span></span></li><li class="alt"><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class=""><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> start(Attributes attributes) {  </span></span></li><li class="alt"><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class=""><span>                earthquakeEntry = <span class="keyword">new</span><span> EarthquakeEntry();  </span></span></li><li class="alt"><span>            }  </span></li><li class=""><span>        });  </span></li><li class="alt"><span>        entry.setEndElementListener(<span class="keyword">new</span><span> EndElementListener() {    </span></span></li><li class=""><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class="alt"><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> end() {  </span></span></li><li class=""><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class="alt"><span>                earthquakeEntryList.add(earthquakeEntry);  </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        });  </span></li><li class=""><span>        <span class="comment">//处理title标签</span><span>  </span></span></li><li class="alt"><span>        entry.getChild(ATOM_NAMESPACE, kTitleElementName).setEndTextElementListener(<span class="keyword">new</span><span> EndTextElementListener() {  </span></span></li><li class=""><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class="alt"><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> end(String currentData) {  </span></span></li><li class=""><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class="alt"><span>                <span class="comment">//提取强度信息</span><span>  </span></span></li><li class=""><span>                String magnitudeString = currentData.split(<span class="string">" "</span><span>)[</span><span class="number">1</span><span>];  </span></span></li><li class="alt"><span>                <span class="keyword">int</span><span> end =  magnitudeString.length()-</span><span class="number">1</span><span>;  </span></span></li><li class=""><span>                magnitudeString = magnitudeString.substring(<span class="number">0</span><span>, end);  </span></span></li><li class="alt"><span>                <span class="keyword">double</span><span> magnitude = Double.parseDouble(magnitudeString);  </span></span></li><li class=""><span>                earthquakeEntry.setMagnitude(magnitude);  </span></li><li class="alt"><span>                <span class="comment">//提取位置信息</span><span>  </span></span></li><li class=""><span>                String place = currentData.split(<span class="string">","</span><span>)[</span><span class="number">1</span><span>].trim();  </span></span></li><li class="alt"><span>                earthquakeEntry.setPlace(place);                  </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        });  </span></li><li class=""><span>        <span class="comment">//处理updated标签</span><span>  </span></span></li><li class="alt"><span>        entry.getChild(ATOM_NAMESPACE, kUpdatedElementName).setEndTextElementListener(<span class="keyword">new</span><span> EndTextElementListener() {  </span></span></li><li class=""><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class="alt"><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> end(String currentData) {  </span></span></li><li class=""><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class="alt"><span>                <span class="comment">//构造更新时间</span><span>  </span></span></li><li class=""><span>                SimpleDateFormat sdf = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy-MM-dd'T'HH:mm:ss'Z'"</span><span>);  </span></span></li><li class="alt"><span>                Date qdate = <span class="keyword">new</span><span> GregorianCalendar(</span><span class="number">0</span><span>,</span><span class="number">0</span><span>,</span><span class="number">0</span><span>).getTime();  </span></span></li><li class=""><span>                <span class="keyword">try</span><span> {  </span></span></li><li class="alt"><span>                  qdate = sdf.parse(currentData);  </span></li><li class=""><span>                } <span class="keyword">catch</span><span> (ParseException e) {  </span></span></li><li class="alt"><span>                  e.printStackTrace();  </span></li><li class=""><span>                }  </span></li><li class="alt"><span>                earthquakeEntry.setDate(qdate);               </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        });  </span></li><li class=""><span>        <span class="comment">//处理point标签</span><span>  </span></span></li><li class="alt"><span>        entry.getChild(GEORSS_NAMESPACE, kGeoRSSPointElementName).setEndTextElementListener(<span class="keyword">new</span><span> EndTextElementListener() {    </span></span></li><li class=""><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class="alt"><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> end(String currentData) {  </span></span></li><li class=""><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class="alt"><span>                <span class="comment">//提取经纬度信息</span><span>  </span></span></li><li class=""><span>                String[] latLongitude = currentData.split(<span class="string">" "</span><span>);  </span></span></li><li class="alt"><span>                Location location = <span class="keyword">new</span><span> Location(</span><span class="string">"dummyGPS"</span><span>);  </span></span></li><li class=""><span>                location.setLatitude(Double.parseDouble(latLongitude[<span class="number">0</span><span>]));  </span></span></li><li class="alt"><span>                location.setLongitude(Double.parseDouble(latLongitude[<span class="number">1</span><span>]));  </span></span></li><li class=""><span>                earthquakeEntry.setLocation(location);  </span></li><li class="alt"><span>            }  </span></li><li class=""><span>        });  </span></li><li class="alt"><span>        <span class="comment">//处理elev标签</span><span>  </span></span></li><li class=""><span>        entry.getChild(GEORSS_NAMESPACE, kGeoRSSElevElementName).setEndTextElementListener(<span class="keyword">new</span><span> EndTextElementListener() {  </span></span></li><li class="alt"><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class=""><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> end(String currentData) {  </span></span></li><li class="alt"><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class=""><span>                <span class="comment">//提取海拔高度信息</span><span>  </span></span></li><li class="alt"><span>                <span class="keyword">double</span><span> evel;  </span></span></li><li class=""><span>                <span class="comment">//因为USGS数据有可能会输错,比如为"--10000",多了一个"-"号</span><span>  </span></span></li><li class="alt"><span>                <span class="keyword">try</span><span> {  </span></span></li><li class=""><span>                    evel = Double.parseDouble(currentData);  </span></li><li class="alt"><span>                } <span class="keyword">catch</span><span> (Exception e) {  </span></span></li><li class=""><span>                    <span class="comment">// TODO: handle exception</span><span>  </span></span></li><li class="alt"><span>                    e.printStackTrace();  </span></li><li class=""><span>                    evel = <span class="number">0</span><span>;  </span></span></li><li class="alt"><span>                }  </span></li><li class=""><span>                Log.v(<span class="string">"Sax_Elev"</span><span>, String.valueOf(evel));  </span></span></li><li class="alt"><span>                earthquakeEntry.setElev(evel);            </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        });  </span></li><li class=""><span>        <span class="comment">//处理link标签</span><span>  </span></span></li><li class="alt"><span>        entry.getChild(ATOM_NAMESPACE, kLinkElementName).setStartElementListener(<span class="keyword">new</span><span> StartElementListener() {         </span></span></li><li class=""><span>            <span class="annotation">@Override</span><span>  </span></span></li><li class="alt"><span>            <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> start(Attributes attributes) {  </span></span></li><li class=""><span>                <span class="comment">// TODO Auto-generated method stub</span><span>  </span></span></li><li class="alt"><span>                <span class="comment">//获取link链接</span><span>  </span></span></li><li class=""><span>                String webLink = attributes.getValue(<span class="string">"href"</span><span>);  </span></span></li><li class="alt"><span>                earthquakeEntry.setLink(webLink);  </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        });  </span></li><li class=""><span>        <span class="comment">//调用android.util.Xml开始解析</span><span>  </span></span></li><li class="alt"><span>        <span class="keyword">try</span><span> {  </span></span></li><li class=""><span>            Xml.parse(inStream, Xml.Encoding.UTF_8, root.getContentHandler());  </span></li><li class="alt"><span>        } <span class="keyword">catch</span><span> (Exception e) {  </span></span></li><li class=""><span>            <span class="comment">// TODO Auto-generated catch block</span><span>  </span></span></li><li class="alt"><span>            e.printStackTrace();  </span></li><li class=""><span>        }  </span></li><li class="alt"><span>          </span></li><li class=""><span>        <span class="keyword">return</span><span> earthquakeEntryList;  </span></span></li><li class="alt"><span>    }  </span></li><li class=""><span>}  </span></li></ol></div> </span></p> <p><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US"> </span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Courier New';" lang="EN-US"><span style="font-size: small;"> </span></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">开头处定义了解析需要的元素标签名称,因为</span><span style="font-size: 10.5pt; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">getChild</span><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">方法获取子元素时需要命名空间,因此还新定义了</span><span style="font-size: 10.5pt; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">USGS</span><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">的</span><span style="font-size: 10.5pt; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">xml</span><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">数据中包含的两个命名空间:</span></p> <p> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;"><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String ATOM_NAMESPACE = </span><span class="string">""</span><span>;   </span></span></li><li class=""><span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String GEORSS_NAMESPACE = </span><span class="string">""</span><span>;  </span></span></li></ol></div> </span></p> <p><span style="font-size: 10.5pt; color: black; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;"> </span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">在定义的用于解析</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">xml</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">数据的方法中</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><strong><span style="background: white; color: #7f0055; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">public</span></strong><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US"> ArrayList<EarthquakeEntry> </span><span style="background: silver; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: silver; mso-fareast-font-family: 'Courier New';" lang="EN-US">parse</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">(InputStream inStream)</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">首先获取</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">xml</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">文档的根元素:</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">RootElement root = </span><strong><span style="background: white; color: #7f0055; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">new</span></strong><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US"> RootElement(</span><em><span style="background: white; color: #0000c0; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">ATOM_NAMESPACE</span></em><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">, </span><span style="background: white; color: #0000c0; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">kRootElementName</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white; mso-fareast-font-family: 'Courier New';" lang="EN-US">);</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">有了根元素之后,就可以使用类似</span><span style="background: white; color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-highlight: white;" lang="EN-US">DOM</span><span style="background: white; color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-highlight: white;">的</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">getChild</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">方法获取具体的某个位置处的子元素,并且可以为具体的子元素注册事件处理器和在对应的回调函数中实现具体的处理逻辑。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">从程序中我们可以看到,我们只为</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US"><entry></span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">元素和</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US"><entry></entry></span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">包含的子元素注册了事件处理器,因此即使</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">xml</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">文档开始处有</span><span lang="EN-US"><span style="font-family: Times New Roman;"><updated></span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">、</span><span lang="EN-US"><span style="font-family: Times New Roman;"><title></span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">和</span><span lang="EN-US"><span style="font-family: Times New Roman;"><link></span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">等同名的元素标签,但也不会进行处理。因此和上一部分的</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">相比,就不再需要设置标志变量用来判断,而且看起来也更加简单了。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">虽然写法不同了,但是对具体标签的处理逻辑和上一部分</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">中是一样的,因为处理的</span><span lang="EN-US"><span style="font-family: Times New Roman;">xml</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">文档内容没有变。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-size: 10.5pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">程序的最后调用</span><span style="font-size: 10.5pt; font-family: "Times New Roman"; mso-fareast-font-family: 宋体; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">android.util.Xml</span><span style="font-size: 10.5pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">类的类方法</span><span style="font-size: 10.5pt; font-family: "Times New Roman"; mso-fareast-font-family: 宋体; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">parse</span><span style="font-size: 10.5pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">直接进行解析,也更加方便了。</span></p> <p> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-size: 10.5pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;"><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="comment">//调用android.util.Xml开始解析</span><span>  </span></span></li><li class=""><span>    <span class="keyword">try</span><span> {  </span></span></li><li class="alt"><span>        Xml.parse(inStream, Xml.Encoding.UTF_8, root.getContentHandler());  </span></li><li class=""><span>    } <span class="keyword">catch</span><span> (Exception e) {  </span></span></li><li class="alt"><span>        <span class="comment">// TODO Auto-generated catch block</span><span>  </span></span></li><li class=""><span>        e.printStackTrace();  </span></li><li class="alt"><span>    }  </span></li></ol></div> </span></p> <p><span style="font-size: 10.5pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;"> </span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">其中</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">Xml</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">类</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">parse</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">方法的</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US">ContentHandler</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">参数由根元素通过</span><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Courier New';" lang="EN-US">getContentHandler()</span><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">方式获得。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">最后添</span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">加</span><span lang="EN-US"><span style="font-family: Times New Roman;">AndroidXMLDemoSaxII.java</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">文件中的内容,内容和上一个</span><span lang="EN-US"><span style="font-family: Times New Roman;">Demo</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">工程</span><span lang="EN-US"><span style="font-family: Times New Roman;">AndroidXMLDemoSax</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">中的</span><span lang="EN-US"><span style="font-family: Times New Roman;">AndroidXMLDemoSax.java</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">基本一样,</span></p> <p> </p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> AndroidXMLDemoSaxII </span><span class="keyword">extends</span><span> Activity {  </span></span></li><li class=""><span>    <span class="comment">/** Called when the activity is first created. */</span><span>  </span></span></li><li class="alt"><span>    <span class="comment">//定义显示的List相关变量</span><span>  </span></span></li><li class=""><span>    ListView list;  </span></li><li class="alt"><span>    ArrayAdapter<EarthquakeEntry> adapter;  </span></li><li class=""><span>    ArrayList<EarthquakeEntry> earthquakeEntryList;  </span></li><li class="alt"><span>    <span class="annotation">@Override</span><span>  </span></span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> onCreate(Bundle savedInstanceState) {  </span></span></li><li class="alt"><span>        <span class="keyword">super</span><span>.onCreate(savedInstanceState);  </span></span></li><li class=""><span>        setContentView(R.layout.main);  </span></li><li class="alt"><span>          </span></li><li class=""><span>        <span class="comment">//获取地震数据流</span><span>  </span></span></li><li class="alt"><span>        InputStream earthquakeStream = readEarthquakeDataFromFile();  </span></li><li class=""><span>        <span class="comment">//Android Sax方式进行解析</span><span>  </span></span></li><li class="alt"><span>        AndroidSaxEarthquakeHandler androidSaxHandler = <span class="keyword">new</span><span> AndroidSaxEarthquakeHandler();  </span></span></li><li class=""><span>        earthquakeEntryList = androidSaxHandler.parse(earthquakeStream);  </span></li><li class="alt"><span>        <span class="comment">//用ListView进行显示</span><span>  </span></span></li><li class=""><span>        list = (ListView)<span class="keyword">this</span><span>.findViewById(R.id.list);  </span></span></li><li class="alt"><span>        adapter = <span class="keyword">new</span><span> ArrayAdapter<EarthquakeEntry>(</span><span class="keyword">this</span><span>, android.R.layout.simple_list_item_1, earthquakeEntryList);  </span></span></li><li class=""><span>        list.setAdapter(adapter);  </span></li><li class="alt"><span>    }  </span></li><li class=""><span>      </span></li><li class="alt"><span>    <span class="keyword">private</span><span> InputStream readEarthquakeDataFromFile()  </span></span></li><li class=""><span>    {  </span></li><li class="alt"><span>        <span class="comment">//从本地获取地震数据</span><span>  </span></span></li><li class=""><span>        InputStream inStream = <span class="keyword">null</span><span>;  </span></span></li><li class="alt"><span>        <span class="keyword">try</span><span> {  </span></span></li><li class=""><span>            inStream = <span class="keyword">this</span><span>.getAssets().open(</span><span class="string">"USGS_Earthquake_1M2_5.xml"</span><span>);  </span></span></li><li class="alt"><span>        } <span class="keyword">catch</span><span> (IOException e) {  </span></span></li><li class=""><span>            <span class="comment">// TODO Auto-generated catch block</span><span>  </span></span></li><li class="alt"><span>            e.printStackTrace();  </span></li><li class=""><span>        }  </span></li><li class="alt"><span>        <span class="keyword">return</span><span> inStream;  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>    <span class="keyword">private</span><span> InputStream readEarthquakeDataFromInternet()  </span></span></li><li class=""><span>    {  </span></li><li class="alt"><span>        <span class="comment">//从网络上获取实时地震数据</span><span>  </span></span></li><li class=""><span>        URL infoUrl = <span class="keyword">null</span><span>;  </span></span></li><li class="alt"><span>        InputStream inStream = <span class="keyword">null</span><span>;  </span></span></li><li class=""><span>        <span class="keyword">try</span><span> {  </span></span></li><li class="alt"><span>            infoUrl = <span class="keyword">new</span><span> URL(</span><span class="string">""</span><span>);  </span></span></li><li class=""><span>            URLConnection connection = infoUrl.openConnection();  </span></li><li class="alt"><span>            HttpURLConnection httpConnection = (HttpURLConnection)connection;  </span></li><li class=""><span>            <span class="keyword">int</span><span> responseCode = httpConnection.getResponseCode();  </span></span></li><li class="alt"><span>            <span class="keyword">if</span><span>(responseCode == HttpURLConnection.HTTP_OK)  </span></span></li><li class=""><span>            {  </span></li><li class="alt"><span>                inStream = httpConnection.getInputStream();  </span></li><li class=""><span>            }  </span></li><li class="alt"><span>        } <span class="keyword">catch</span><span> (MalformedURLException e) {  </span></span></li><li class=""><span>            <span class="comment">// TODO Auto-generated catch block</span><span>  </span></span></li><li class="alt"><span>            e.printStackTrace();  </span></li><li class=""><span>        } <span class="keyword">catch</span><span> (IOException e) {  </span></span></li><li class="alt"><span>            <span class="comment">// TODO Auto-generated catch block</span><span>  </span></span></li><li class=""><span>            e.printStackTrace();  </span></li><li class="alt"><span>        }  </span></li><li class=""><span>        <span class="keyword">return</span><span> inStream;  </span></span></li><li class="alt"><span>    }  </span></li><li class=""><span>}  </span></li></ol></div> <p> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"> </p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">只是把进行</span><span lang="EN-US"><span style="font-family: Times New Roman;">SAX</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">解析的部分换成了如下方式:</span></p> <div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><a href="http://blog.csdn.net/ichliebephone/article/details/5971533#" class="ViewSource" title="view plain" target="_blank">view plain</a></div></div><ol class="dp-j" start="1"><li class="alt"><span><span class="comment">//Android Sax方式进行解析</span><span>  </span></span></li><li class=""><span>AndroidSaxEarthquakeHandler androidSaxHandler = <span class="keyword">new</span><span> AndroidSaxEarthquakeHandler();  </span></span></li><li class="alt"><span>earthquakeEntryList = androidSaxHandler.parse(earthquakeStream);  </span></li></ol></div>  <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New';">完成了,可以保存运行看下效果。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt; text-align: left;" align="left"><span style="color: black; font-family: "Courier New"; mso-bidi-font-family: 'Times New Roman';" lang="EN-US"> </span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; mso-list: l0 level1 lfo1;"><span style="mso-bidi-font-family: 宋体;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-family: Times New Roman;">三.</span></span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">总结</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span lang="EN-US"><span style="font-family: Times New Roman;">Android</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">平台提供了相当强大的</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">解析支持,不仅包含了</span><span lang="EN-US"><span style="font-family: Times New Roman;">Java SDK</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">中用来</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">处理的</span><span lang="EN-US"><span style="font-family: Times New Roman;">API</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">,而且</span><span lang="EN-US"><span style="font-family: Times New Roman;">Android SDK</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">还提供了特有的用于</span><span lang="EN-US"><span style="font-family: Times New Roman;">SAX</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">解析</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">的包</span><span lang="EN-US"><span style="font-family: Times New Roman;">android.sax</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">。使用这个包中的</span><span lang="EN-US"><span style="font-family: Times New Roman;">API</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">可以更加方便解析,特别是当要解析的</span><span lang="EN-US"><span style="font-family: Times New Roman;">xml</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">文档中在不同的层级位置处有多个相同名称的标签但需要分别进行不同处理时,同时也有更好的鲁棒性,减少解析时产生</span><span lang="EN-US"><span style="font-family: Times New Roman;">Bug</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">的可能性。</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">以上我们介绍的都是</span><span lang="EN-US"><span style="font-family: Times New Roman;">SAX</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">方式解析</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">,而解析</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">常用的还有</span><span lang="EN-US"><span style="font-family: Times New Roman;">DOM</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">方式,这部分内容我们以后接着学习。</span></p><br> </div> <!-- <div class="Blog_con3_1">管理员在2009年8月13日编辑了该文章文章。</div> --> <div class="Blog_con2_1 Blog_con3_2"> <div> <!--<img src="/image/default/tu_8.png">--> <!-- JiaThis Button BEGIN --> <div class="bdsharebuttonbox"><A class=bds_more href="#" data-cmd="more"></A><A class=bds_qzone title=分享到QQ空间 href="#" data-cmd="qzone"></A><A class=bds_tsina title=分享到新浪微博 href="#" data-cmd="tsina"></A><A class=bds_tqq title=分享到腾讯微博 href="#" data-cmd="tqq"></A><A class=bds_renren title=分享到人人网 href="#" data-cmd="renren"></A><A class=bds_weixin title=分享到微信 href="#" data-cmd="weixin"></A></div> <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> <!-- JiaThis Button END --> </div> 阅读(1297) | 评论(0) | 转发(0) | <div class="HT_line3"></div> </div> <div class="Blog_con3_3"> <div><span id='digg_num'>0</span><a href="javascript:void(0)" id='digg' bid='2983744' url='/blog/digg.html' ></a></div> <p>上一篇:<a href="/uid-8735300-id-2983742.html">[Android] Android XML解析学习——Sax方式 (ZT)</a></p> <p>下一篇:<a href="/uid-8735300-id-2983745.html">[Android] Android XML解析学习——Pull方式 (ZT)</a></p> </div> </div> <!-- <div class="Blog_con3_4 Blog_con3_5"> <div class="Blog_tit2 Blog_tit7">热门推荐</div> <ul> <li><a href="" title="" target='blank' ></a></li> </ul> </div> --> </div> </div> <div class="Blog_right1_7" id='replyList'> <div class="Blog_tit3">给主人留下些什么吧!~~</div> <!--暂无内容--> <!-- 评论分页--> <div class="Blog_right1_6 Blog_right1_12"> </div> <!-- 评论分页--> <div class="Blog_right1_10" style="display:none"> <div class="Blog_tit3">评论热议</div> <!--未登录 --> <div class="Blog_right1_8"> <div class="nologin_con1"> 请登录后评论。 <p><a href="http://account.chinaunix.net/login" onclick="link(this)">登录</a> <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net">注册</a></p> </div> </div> </div> <div style="text-align:center;margin-top:10px;"> <script type="text/javascript" smua="d=p&s=b&u=u3118759&w=960&h=90" src="//www.nkscdn.com/smu0/o.js"></script> </div> </div> </div> </div> <input type='hidden' id='report_url' value='/blog/ViewReport.html' /> <script type="text/javascript"> //测试字符串的长度 一个汉字算2个字节 function mb_strlen(str) { var len=str.length; var totalCount=0; for(var i=0;i<len;i++) { var c = str.charCodeAt(i); if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { totalCount++; }else{ totalCount+=2; } } return totalCount; } /* var Util = {}; Util.calWbText = function (text, max){ if(max === undefined) max = 500; var cLen=0; var matcher = text.match(/[^\x00-\xff]/g), wlen = (matcher && matcher.length) || 0; //匹配url链接正则 http://*** var pattern = /http:\/\/([\w-]+\.)+[\w-]+(\/*[\w-\.\/\?%&=][^\s^\u4e00-\u9fa5]*)?/gi; //匹配的数据存入数组 var arrPt = new Array(); var i = 0; while((result = pattern.exec(text)) != null){ arrPt[i] = result[0]; i++; } //替换掉原文本中的链接 for(var j = 0;j<arrPt.length;j++){ text = text.replace(arrPt[j],""); } //减掉链接所占的长度 return Math.floor((max*2 - text.length - wlen)/2 - 12*i); }; */ var allowComment = '0'; //举报弹出层 function showJuBao(url, cid){ $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 , 'blogurl' : window.location.href }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); } $(function(){ //创建管理员删除的弹出层 $('#admin_article_del').click(function(){ $.cover(false); asyncbox.open({ id : 'class_thickbox', html : '<div class="HT_layer3_1"><ul><li class="HT_li1">操作原因:<select class="HT_sel7" id="del_type" name="del_type"><option value="广告文章">广告文章</option><option value="违规内容">违规内容</option><option value="标题不明">标题不明</option><option value="文不对题">文不对题</option></select></li><li class="HT_li1" ><input class="HT_btn4" id="admin_delete" type="button" /></li></ul></div>', title : '选择类型', width : 300, height : 150, scroll : 'no', callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); $('#admin_delete').live('click' , function(){ ///blog/logicdel/id/3480184/url/%252Fblog%252Findex.html.html var type = $('#del_type').val(); var url = '/blog/logicdel/id/2983744/url/%252Fuid%252F8735300.html.html'; window.location.href= url + '?type=' + type; }); //顶 js中暂未添加&过滤 $('#digg').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $('#digg').attr('bid'); var url = $('#digg').attr('url'); var digg_str = $.cookie('digg_id'); if(digg_str != null) { var arr= new Array(); //定义一数组 arr = digg_str.split(","); //字符分割 for( i=0 ; i < arr.length ; i++ ) { if(bid == arr[i]) { showErrorMsg('已经赞过该文章', '消息提示'); return false; } } } $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0) { var num = parseInt($('#digg_num').html(),10); num += 1; $('#digg_num').html(num); $('#digg').die(); if(digg_str == null) { $.cookie('digg_id', bid, {expires: 30 , path: '/'}); } else { $.cookie('digg_id', digg_str + ',' + bid, {expires: 30 , path: '/'}); } showSucceedMsg('谢谢' , '消息提示'); } else if(msg.error == 1) { //showErrorMsg(msg.error_content , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } else if(msg.error == 2) { showErrorMsg(msg.error_content , '消息提示'); } } }); }); //举报弹出层 /*$('.box_report').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var url = $('#report_url').val(); var cid = $(this).attr('cid'); $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); });*/ //评论相关代码 //点击回复显示评论框 $('.Blog_a10').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var tid = $(this).attr('toid');//留言作者id var bid = $(this).attr('bid');//blogid var cid = $(this).attr('cid');//留言id var tname = $(this).attr('tname'); var tpl = '<div class="Blog_right1_9">'; tpl += '<div class="div2">'; tpl += '<textarea name="" cols="" rows="" class="Blog_ar1_1" id="rmsg">文明上网,理性发言...</textarea>'; tpl += '</div>'; tpl += '<div class="div3">'; tpl += '<div class="div3_2"><a href="javascript:void(0);" class="Blog_a11" id="quota_sbumit" url="/Comment/PostComment.html" tid="'+tid+'" bid="'+bid+'" cid="'+cid+'" tname="'+tname+'" ></a><a href="javascript:void(0)" id="qx_comment" class="Blog_a12"></a></div>'; tpl += '<div class="div3_1"><a href="javascript:void(0);" id="mface"><span></span>表情</a></div>'; tpl += '<div class="clear"></div>'; tpl += '</div>'; tpl += '</div>'; $('.z_move_comment').html(''); $(this).parents('.Blog_right1_8').find('.z_move_comment').html(tpl).show(); }); //引用的评论提交 $('#quota_sbumit').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var tid = $(this).attr('tid');//被引用人的id var qid = $(this).attr('cid');//引用的id var url = $(this).attr('url'); var text = $('#rmsg').val(); var tname = $(this).attr('tname'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : tid , 'qid' : qid , 'text': text , 'tname' : tname }, dataType: 'json', success: function(data){ if(data.code == 1){ var tpl = '<div class="Blog_right1_8">'; tpl+= '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl+= '<div class="Blog_right_font1">'; tpl+= '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl+= '<p class="Blog_p7"><a href="' + data.info.quota.url + '">' + data.info.quota.username + '</a>:'+ data.info.quota.content + '</p>'; tpl+= '<p class="Blog_p8">' + data.info.content + '</p><span class="span_text1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + ' tname = ' + data.info.username + ' >回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '" >删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '" >举报</a></span></div>'; tpl+= '<div class="z_move_comment" style="display:none"></div>'; tpl+= '<div class="Blog_line1"></div></div>'; $('#replyList .Blog_right1_8:first').before(tpl); $('.z_move_comment').html('').hide(); } else if(data.code == -1){ //showErrorMsg(data.info , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } }, error: function(){//请求出错处理 } }); }); //底部发表评论 $('#submitmsg').click(function(){ if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var bid = $(this).attr('bid'); var toid = $(this).attr('toid'); var text = $('#reply').val(); var url = $(this).attr('url'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : toid ,'text': text}, dataType: 'json', success: function(data){ if(data.code == 1) { var tpl = '<div class="Blog_right1_8">'; tpl += '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl += '<div class="Blog_right_font1">'; tpl += '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl += '<p class="Blog_p6">' + data.info.content + '</p>'; tpl += '<div class="div1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + '>回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '">删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '">举报</a></div>'; tpl += '<div class="z_move_comment" style="display:none"></div>'; tpl += '</div><div class="Blog_line1"></div></div>'; $('.Blog_tit3:first').after(tpl); $('#reply').val('文明上网,理性发言...'); } else if(data.code == -1) { showErrorMsg(data.info , '消息提示'); } }, error: function(){//请求出错处理 } }); }); //底部评论重置 $('#reset_comment').click(function(){ $('#reply').val('文明上网,理性发言...'); }); //取消回复 $('#qx_comment').live('click' , function(){ $('.z_move_comment').html('').hide(); }); $('#rmsg, #reply').live({ focus:function(){ if($(this).val() == '文明上网,理性发言...'){ $(this).val(''); } }, blur:function(){ if($(this).val() == ''){ $(this).val('文明上网,理性发言...'); } } }); //删除留言确认 $('.comment_del_mark').live('click' , function(){ var url = $(this).attr('url'); asyncbox.confirm('删除留言','确认', function(action){ if(action == 'ok') { location.href = url; } }); }); //删除时间确认 $('.del_article_id').click(function(){ var delurl = $(this).attr('delurl'); asyncbox.confirm('删除文章','确认', function(action){ if(action == 'ok') { location.href = delurl; } }); }); /* //字数限制 $('#rmsg, #reply').live('keyup', function(){ var id = $(this).attr('id'); var left = Util.calWbText($(this).val(), 500); var eid = '#errmsg'; if(id == 'reply') eid = '#rerrmsg'; if (left >= 0) $(eid).html('您还可以输入<span>' + left + '</span>字'); else $(eid).html('<font color="red">您已超出<span>' + Math.abs(left) + '</span>字 </font>'); }); */ //加载表情 $('#face').qqFace({id : 'facebox1', assign : 'reply', path : '/image/qqface/'}); $('#mface').qqFace({id : 'facebox', assign : 'rmsg', path:'/image/qqface/'}); /* $('#class_one_id').change(function(){ alert(123213); var id = parseInt($(this).val() , 10); if(id == 0) return false; $('.hidden_son_class span').each(function( index , dom ){ if( dom.attr('cid') == id ) { } }); }); */ //转载文章 var turn_url = "/blog/viewClassPart.html"; $('#repost_bar').click(function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var id = $(this).attr('bid'); asyncbox.open({ id : 'turn_class_thickbox', url : turn_url, title : '转载文章', width : 330, height : 131, scroll : 'no', data : { 'id' : id }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); /* //转发文章 $('#repost_bar').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var url = $(this).attr('url'); asyncbox.confirm('转载文章','确认', function(action){ if(action == 'ok'){ $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0){ showSucceedMsg('转发成功!', '消息提示'); }else if(msg.error == 1){ //location.href = '/index.php?r=site/login'; showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); }else{ showErrorMsg(msg.error_content, '消息提示'); } } }); } }); }); */ }); </script> <!--该部分应该放在输出代码块的后面才起作用 --> <script type="text/javascript"> SyntaxHighlighter.autoloader( 'applescript /highlight/scripts/shBrushAppleScript.js', 'actionscript3 as3 /highlight/scripts/shBrushAS3.js', 'bash shell /highlight/scripts/shBrushBash.js', 'coldfusion cf /highlight/scripts/shBrushColdFusion.js', 'cpp c /highlight/scripts/shBrushCpp.js', 'c# c-sharp csharp /highlight/scripts/shBrushCSharp.js', 'css /highlight/scripts/shBrushCss.js', 'delphi pascal /highlight/scripts/shBrushDelphi.js', 'diff patch pas /highlight/scripts/shBrushDiff.js', 'erl erlang /highlight/scripts/shBrushErlang.js', 'groovy /highlight/scripts/shBrushGroovy.js', 'java /highlight/scripts/shBrushJava.js', 'jfx javafx /highlight/scripts/shBrushJavaFX.js', 'js jscript javascript /highlight/scripts/shBrushJScript.js', 'perl pl /highlight/scripts/shBrushPerl.js', 'php /highlight/scripts/shBrushPhp.js', 'text plain /highlight/scripts/shBrushPlain.js', 'py python /highlight/scripts/shBrushPython.js', 'ruby rails ror rb /highlight/scripts/shBrushRuby.js', 'scala /highlight/scripts/shBrushScala.js', 'sql /highlight/scripts/shBrushSql.js', 'vb vbnet /highlight/scripts/shBrushVb.js', 'xml xhtml xslt html /highlight/scripts/shBrushXml.js' ); SyntaxHighlighter.all(); function code_hide(id){ var code = document.getElementById(id).style.display; if(code == 'none'){ document.getElementById(id).style.display = 'block'; }else{ document.getElementById(id).style.display = 'none'; } } </script> <!--回顶部js2011.12.30--> <script language="javascript"> lastScrollY=0; function heartBeat(){ var diffY; if (document.documentElement && document.documentElement.scrollTop) diffY = document.documentElement.scrollTop; else if (document.body) diffY = document.body.scrollTop else {/*Netscape stuff*/} percent=.1*(diffY-lastScrollY); if(percent>0)percent=Math.ceil(percent); else percent=Math.floor(percent); document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px"; lastScrollY=lastScrollY+percent; if(lastScrollY<200) { document.getElementById("full").style.display="none"; } else { document.getElementById("full").style.display="block"; } } var gkuan=document.body.clientWidth; var ks=(gkuan-960)/2-30; suspendcode="<div id=\"full\" style='right:-30px;POSITION:absolute;TOP:500px;z-index:100;width:26px; height:86px;cursor:pointer;'><a href=\"javascript:void(0)\" onclick=\"window.scrollTo(0,0);\"><img src=\"\/image\/top.png\" /></a></div>" document.write(suspendcode); window.setInterval("heartBeat()",1); </script> <!-- footer --> <div class="Blog_footer" style='clear:both'> <div><a href="http://www.chinaunix.net/about/index.shtml" target="_blank" rel="nofollow">关于我们</a> | <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank" rel="nofollow">关于IT168</a> | <a href="http://www.chinaunix.net/about/connect.html" target="_blank" rel="nofollow">联系方式</a> | <a href="http://www.chinaunix.net/about/service.html" target="_blank" rel="nofollow">广告合作</a> | <a href="http://www.it168.com//bottomfile/flgw/fl.htm" target="_blank" rel="nofollow">法律声明</a> | <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net" target="_blank" rel="nofollow">免费注册</a> <p>Copyright 2001-2010 ChinaUnix.net All Rights Reserved 北京皓辰网域网络信息技术有限公司. 版权所有 </p> <div>感谢所有关心和支持过ChinaUnix的朋友们 <p><a href="http://beian.miit.gov.cn/">16024965号-6 </a></p> </div> </div> </div> </div> <script language="javascript"> //全局错误提示弹出框 function showErrorMsg(content, title, url){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span1"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="login_btn1" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //全局正确提示 function showSucceedMsg(content, title , url ){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span2"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //关闭指定id的窗口 function close_windows(id){ $.cover(false); $.close(id); } //公告 var tID; var tn; // 高度 var nStopTime = 5000; // 不同行间滚动时间隔的时间,值越小,移动越快 var nSpeed = 50; // 滚动时,向上移动一像素间隔的时间,值越小,移动越快 var isMove = true; var nHeight = 25; var nS = 0; var nNewsCount = 3; /** * n 用于表示是否为第一次运行 **/ function moveT(n) { clearTimeout(tID) var noticev2 = document.getElementById("noticev2") nS = nSpeed; // 只在第一次调用时运行,初始化环境(有没有参数) if (n) { // 设置行高 noticev2.style.lineHeight = nHeight + "px"; // 初始化显示位置 tn = 0; // 刚进入时在第一行停止时间 nS = nStopTime; } // 判断鼠标是否指向层 if (isMove) { // 向上移动一像素 tn--; // 如果移动到最下面一行了,则移到顶行 if (Math.abs(tn) == nNewsCount * nHeight) { tn = 0; } // 设置位置 noticev2.style.marginTop = tn + "px"; // 完整显示一行时,停止一段时间 if (tn % nHeight == 0) { nS = nStopTime; } } tID = setTimeout("moveT()", nS); } moveT(1); // 此处可以传入任何参数 </script> <script type="text/javascript"> // var _gaq = _gaq || []; // _gaq.push(['_setAccount', 'UA-20237423-2']); // _gaq.push(['_setDomainName', '.chinaunix.net']); // _gaq.push(['_trackPageview']); // // (function() { // var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; // ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; // var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); // })(); </script> <script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0ee5e8cdc4d43389b3d1bfd76e83216b' type='text/javascript'%3E%3C/script%3E")); function link(t){ var href= $(t).attr('href'); href+="?url="+encodeURIComponent(location.href); $(t).attr('href',href); //setCookie("returnOutUrl", location.href, 60, "/"); } </script> <script type="text/javascript" src="/js/jquery.qqFace.js"></script> </body> </html>