我正在使用以下两种方法来解析我的Xml数据:
private static XMLReader prepareSAX() throws ParserConfigurationException,
SAXException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
return sp.getXMLReader();
}
public static void LectorSAX(XMLReader xr, String url) {
try {
InputSource is = new InputSource(url);
is.setEncoding("UTF-8");
xr.parse(is);
} catch (SAXException e) {
System.err.println("Error de sax LectorSAX.java: " + e);
e.printStackTrace();
} catch (IOException e) {
System.err.println("Error de io LectorSAX.java: " + e);
e.printStackTrace();
}
}
现在我的Xml数据如下:
<Product>
<Product_ID>22434</Product_ID>
<Chinese_Name>三Q 逆齡速效霜</Chinese_Name>
<English_Name>Q10 QUICK GEL MOIST & WHITENING</English_Name>
<Image_Path>http://www.abc.com/prodImage/dtl/13011015,38,8.jpg</Image_Path>
<Original_Price>880</Original_Price>
<Discounted_Price>0</Discounted_Price>
<Product_Detail><![CDATA[<html><body>三Q 逆齡速效霜</br>Q10 QUICK GEL MOIST & WHITENING<br><br></body></html>]]></Product_Detail>
</Product>
我正在遵循执行:
01-29 04:14:10.637: W/System.err(1665): Error de sax LectorSAX.java: org.apache.harmony.xml.ExpatParser$ParseException: At line 185, column 36: unknown encoding
01-29 04:14:10.758: W/System.err(1665): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
01-29 04:14:10.758: W/System.err(1665): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
01-29 04:14:10.758: W/System.err(1665): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
01-29 04:14:10.818: W/System.err(1665): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:294)
01-29 04:14:10.818: W/System.err(1665): at com.dhc.xmlparsing.XMLParser.LectorSAX(XMLParser.java:79)
我的第185行是English_Name Tag。 所以我在做错的地方,请帮帮我。
答案 0 :(得分:8)
最后得到了一个解决方案,saxParser编码“&amp;”(特殊字符)是个问题,所以我用&
替换了“&
”。现在它的工作完美,代码如下所示:
response = response.replaceAll("&", "&");
InputSource inputSource = new InputSource();
inputSource.setEncoding("UTF-8");
Log.i("TAG", "response" + response);
inputSource.setCharacterStream(new StringReader(response));
xr.parse(inputSource);
答案 1 :(得分:1)
我的代码中也面临同样的问题,我尝试了下面的解决方案。它对我有用。
InputSource inputSource = new InputSource();
inputSource.setEncoding("ISO-8859-1");
inputSource.setCharacterStream(new StringReader(response));
xr.parse(inputSource);