【问题标题】:KXmlParser throws "Unexpected token" exception at the start of RSS pasingKXmlParser 在 RSS 传输开始时抛出“Unexpected token”异常
【发布时间】:2013-03-06 17:31:25
【问题描述】:

我正在尝试使用此 URL 解析来自 Android v.17 上 Monster 的 RSS 提要:

http://rss.jobsearch.monster.com/rssquery.ashx?q=java

为了获取内容,我以以下方式使用 HttpUrlConnection

this.conn = (HttpURLConnection) url.openConnection();
this.conn.setConnectTimeout(5000);
this.conn.setReadTimeout(10000);
this.conn.setUseCaches(true);
conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
is = new InputStreamReader(url.openStream());

据我所知(我也验证过)一个合法的 RSS

Cache-Control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:5958
Content-Type:text/xml
Date:Wed, 06 Mar 2013 17:15:20 GMT
P3P:CP=CAO DSP COR CURa ADMa DEVa IVAo IVDo CONo HISa TELo PSAo PSDo DELa PUBi BUS LEG PHY ONL UNI PUR COM NAV INT DEM CNT STA HEA PRE GOV OTC
Server:Microsoft-IIS/7.5
Vary:Accept-Encoding
X-AspNet-Version:2.0.50727
X-Powered-By:ASP.NET

它的开头是这样的(如果您想查看完整的 XML,请单击上面的 URL):

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Monster Job Search Results java</title>
    <description>RSS Feed for Monster Job Search</description>
    <link>http://rss.jobsearch.monster.com/rssquery.ashx?q=java</link>

但是当我尝试解析它时:

final XmlPullParser xpp = getPullParser();
xpp.setInput(is);
for (int type = xpp.getEventType(); type != XmlPullParser.END_DOCUMENT; type = xpp.next()) { /* pasing goes here */ }

代码立即阻塞type = xpp.next(),并出现以下异常

03-06 09:27:27.796: E/AbsXmlResultParser(13363): org.xmlpull.v1.XmlPullParserException: 
   Unexpected token (position:TEXT @1:2 in java.io.InputStreamReader@414b4538) 

这实际上意味着它无法处理第 1 行的第二个字符 &lt;?xml version="1.0" encoding="utf-8"?&gt;

这是 KXmlParser.java (425-426) 中的违规行。类型 == TEXT 计算结果为 true

if (depth == 0 && (type == ENTITY_REF || type == TEXT || type == CDSECT)) {
    throw new XmlPullParserException("Unexpected token", this, null);
}

有什么帮助吗?我确实尝试将解析器设置为 XmlPullParser.FEATURE_PROCESS_DOCDECL = false 但这没有帮助

我在网上和这里做了研究,找不到任何有用的东西

【问题讨论】:

    标签: android rss xmlpullparser


    【解决方案1】:

    您收到错误的原因是 xml 文件实际上并非以 &lt;?xml version="1.0" encoding="utf-8"?&gt; 开头。它以三个特殊字节EF BB BF 开头,它们是Byte order mark

    InputStreamReader 不会自动处理这些字节,因此您必须手动处理它们。最简单的方法是使用BOMInpustStream 库中的BOMInpustStream

    this.conn = (HttpURLConnection) url.openConnection();
    this.conn.setConnectTimeout(5000);
    this.conn.setReadTimeout(10000);
    this.conn.setUseCaches(true);
    conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
    is = new InputStreamReader(new BOMInputStream(conn.getInputStream(), false, ByteOrderMark.UTF_8));  
    

    我检查了上面的代码,它对我很有效。

    【讨论】:

    • 这正是我喜欢 Stackoverflow 的原因!总能找到比自己聪明的人!当之无愧的赏金(尽管我不能在明天之前授予它)!谢谢!
    • 我有这个错误,但我在字符串变量中有 xml,我该怎么办?
    • 或者你可以做 data.replaceAll("^.*
    • @vmironov 您使用哪个工具分析文件?如果可能的话,你能提供名字和下载链接吗?
    • @viperbone 这只是一个普通的十六进制查看器
    猜你喜欢
    • 2018-02-14
    • 2018-03-08
    • 2019-01-24
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多