gpt4 book ai didi

android - org.apache.harmony.xml.ExpatParser$ParseException(无效 token )

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:56 24 4
gpt4 key购买 nike

我正在使用以下两种方法来解析我的 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。所以我哪里做错了,请帮助我。

最佳答案

终于找到了解决方案,这是 saxParser 编码“&”(特殊字符)的问题,所以我用 & 替换了“&”。现在它运行良好,代码如下所示:

    response = response.replaceAll("&", "&amp;");
InputSource inputSource = new InputSource();
inputSource.setEncoding("UTF-8");
Log.i("TAG", "response" + response);
inputSource.setCharacterStream(new StringReader(response));
xr.parse(inputSource);

关于android - org.apache.harmony.xml.ExpatParser$ParseException(无效 token ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com