我一直在努力弄清楚为什么我的Android SAX解析器不喜欢像©和Ñ
这样的字符。我可以有几个解析例程来解析一个http调用的结果,该调用会抓取我服务器上的xml文件或生成xml的web服务器的结果。对于xml文件的情况,一切都很酷。
然而,我的网络服务器返回一个家庭烹饪的原子Feed导致我的应用程序获得异常:
org.apache.harmony.xml.ExpatParser $ ParseException:在第73行,第27列:格式不正确(无效令牌)
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
root.getContentHandler());
}
catch(SAXException se){
hits this-> throw new RuntimeException(se);
}
catch (Exception e) {
throw new RuntimeException(e);
}
注意UTF_8的事情。
我的xml文件如下所示,导致问题的字符串是año:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>News</title>
<id>SUCCESS</id>
<generator>News</generator>
<updated>2013-12-04T04:40:59Z</updated>
<language xmlns="">SPANISH</language>
<entry>
<title>Disfrutando el año de Gracias con la familia</title>
<link rel="alternate" type="text/html" href="http:/www.the.com/zocalo/news/1/810b28c1-289b-41d7-b4bb-148b0f52a83a/news_sp.xml" />
<link rel="enclosure" type="image/jpg" href="http://www.the.com.net/zocalo/news/1/810b28c1-289b-41d7-b4bb-148b0f52a83a/img1.png" />
<link rel="enclosure" type="image/jpg" />
etc
</entry>
</feed>
我应该检查什么想法?任何帮助将非常感激。谢谢!
答案 0 :(得分:0)
使用©
代替©
和Ñ
代替Ñ
您的xml将有效。
答案 1 :(得分:0)
最后得到了一个解决方案,saxParser编码“&amp;”(特殊字符)是个问题所以我已经替换了“&amp;”与&amp ;.现在它的工作完美,代码如下所示:
response = response.replaceAll("&", "&"); // Your Server Response
InputSource inputSource = new InputSource();
inputSource.setEncoding("UTF-8");
Log.i("TAG", "Response>>>>" + response);
inputSource.setCharacterStream(new StringReader(response));
xr.parse(inputSource);
答案 2 :(得分:0)
我的代码中也面临同样的问题,我尝试了下面的解决方案。它对我有用。
InputSource inputSource = new InputSource();<br/>
inputSource.setEncoding("ISO-8859-1");<br/>
inputSource.setCharacterStream(new StringReader(response));<br/>
xr.parse(inputSource);