I'm stuck trying to figure out why my Android SAX parser doesn't like chacters like © and Ñ

I can have several parse routines that parse either the results of an http call that grabs an xml file sitting on my server or the results of a webserver that produces xml. For the case of the xml file everything is cool.

However, my webserver which returns a homecooked atom feed causes my app to get an exception:

org.apache.harmony.xml.ExpatParser$ParseException: At line 73, column 27: not well-formed (invalid token)

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

Note the UTF_8 thing.

My xml file looks like this, and the string causing the problem is 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>

Any ideas what I should be checking for? Any help would be very appreciated. Thanks!!

3

There are 3 best solutions below

0
On

Use &#169; in place of © and &#209; in place of Ñ

And your xml will be valid.

0
On

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

InputSource inputSource = new InputSource();<br/>
inputSource.setEncoding("ISO-8859-1");<br/>
inputSource.setCharacterStream(new StringReader(response));<br/>
xr.parse(inputSource);
1
On

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

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