使用SAX Parser进行Android XML解析

时间:2011-10-11 19:06:26

标签: java android xml xml-parsing sax

我一直在尝试使用此处的SAX解析器解析此(http://app.calvaryccm.com/mobile/android/v1/devos)URL:http://android-er.blogspot.com/2010/05/simple-rss-reader-iii-show-details-once.html我一直在研究如何处理XML中的description标记。我已经尝试了这个有和没有CDATA标签,似乎没有任何帮助。这几乎就像链接被读入描述一样。

第一部分工作正常:

enter image description here

当我尝试访问内部页面时,会出现问题。这几乎就像在描述标签之前读取链接标签一样。

enter image description here

我在使描述标记正确显示时遇到问题。谢谢你的帮助!

编辑此应用程序的完整源代码位于:http://dl.dropbox.com/u/19136502/CCM.zip

1 个答案:

答案 0 :(得分:3)

哎呀,经过大约3个小时的挖掘和分析你的源代码,我找到了你之所以有如此奇怪的结果的原因。

首先查看您解析的链接中的RSS内容:http://app.calvaryccm.com/mobile/android/v1/devos

其内容的某些部分:

<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>CCM Daily Devotions</title> <link>http://www.calvaryccm.com/resources/dailydevotions.aspx</link> <description>Calvary Chapel Melbourne's Daily Devotionals</description> <webMaster>webmaster@calvaryccm.com (Calvary Chapel Melbourne)</webMaster> <copyright>(c)2011, Calvary Chapel Melbourne. All rights reserved</copyright> <ttl>60</ttl> <item> <guid isPermaLink="false">b3e91cbf-bbe9-4667-bf4c-8ff831ba09f1</guid>
<title>Teachable Moments</title> <description>Based on &amp;ldquo;Role Models, Part 4&amp;rdquo; by Pastor Mark Balmer; 10/8-9/11, Message #6078; Daily Devotional #6 - &amp;ldquo;Teachable Moments&amp;rdquo; Preparing the Soil (Introduction): My husband and I took seriously our understanding of God&amp;rsquo;s instructions to teach His commandments to our children. (Deuteronomy 6:7) We went to our local Christian bookstore and bought children&amp;rsquo;s Bibles, studies, coloring books, games&amp;mdash;anything that would help us to communicate biblical situations in their lives. Planting and Watering the Seed (Growth): Each parent needs to take seriously God&amp;rsquo;s commthe Crop (Action/Response): Life is God&amp;rsquo;s classroom for teachable moments. A long delay in traffic can be a frustrating irritation, or it can be an opportunity to teach our children that God&amp;rsquo;s than taught. Cultivating (Additional Reading): Psalm 78:1-8;&amp;nbsp;Psalm 145:4 klw Calvary Chapel of Melbourne; 2955 Minton Road; W. Melbourne, FL 32904; 321-952-9673 NLT = New Living Translation. </description> <link>http://www.calvaryccm.com/resources/dailydevotions.aspx</link> <pubDate>Sun, 16 Oct 2011 12:00:00 GMT</pubDate> </item>

请密切注意此标记/rss/channel/item/description,您可以看到以下内容:rsquo;'squo;&amp;ldquo;或{{1这些是转义字符(左单引号,右单引号,&符号,右双引号,左双引号......甚至新行),它们驻留在XML内容中。

因此当rdquo;遍历这些字符时,它会考虑逃避解析,这会导致您现在面临的奇怪结果。

解决方案怎么样?首先,我可以考虑首先获取XML Parser的内容,然后取消那些角色(添加SLASH字符),现在我认为你可以成功解析它。
这个解决方案似乎运行良好,但是,我认为它可能不会,因为来自服务器的RSS文本内容响应是非常奇怪的格式(格式不正确)。因此,如果您可以联系此Web管理员,请告知他们在发出RSS订阅之前很好地格式化URL(例如添加SLASH以转义字符,删除所有NEW-LINE字符......)。

其他解决方案是使用某些第三方来处理RSS content StringEscapeUtilshttp://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.htmlApache Commons中的JTidy转义/转义内容。
但我不认为这些库在您的情况下最有效。

这就是我能说的全部。

@ p / s:只是对您的源代码提出了一些意见,我认为您需要考虑让您的代码清晰易读,更好地进行维护,并进行适当的重新打包。