Domanda

I am Using following two methods for parsing my Xml Data:

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();
    }
}

Now My Xml Data Is as following:

<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>

I am Getting Following Exeption:

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)

My 185th line is English_Name Tag. so where i am doing Wrong,please help me out.

È stato utile?

Soluzione

Finally got a solution,it was problem with encoding "&"(special character) by saxParser so i have replace "&" with &amp;. now its working perfectly, code is shown below:

    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);

Altri suggerimenti

I am also facing same problem in my code, i tried below solution. It worked for me.

InputSource inputSource = new InputSource();
inputSource.setEncoding("ISO-8859-1");
inputSource.setCharacterStream(new StringReader(response));
xr.parse(inputSource);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top