Reputation: 4566
I've written an RSS feed parser in Java (running on Android) and it parses some feeds perfectly, and others not at all. I get the following error when it tries to parse Slashdot (http://rss.slashdot.org/Slashdot/slashdot)
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: unbound prefix
If I try to parse Wired (http://feeds.wired.com/wired/index)
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: syntax error
If I try to parse AndroidGuys (http://feeds.feedburner.com/androidguyscom)
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: syntax error
Here is some code for my parser.
public void updateArticles(Context ctx, Feed feed, int numDaysToGet) {
try {
targetFlag = TARGET_ARTICLES;
tweetDB = new TweetMonsterDBAdapter(ctx);
tweetDB.open();
currentFeed = feed;
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // or "Etc/GMT-1"
Date currentDate = new Date();
long dateInMillis = currentDate.getTime();
oldestDate.setTime(dateInMillis-(dayInMillis*numDaysToGet));
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
xr.parse(new InputSource(currentFeed.url.openStream()));
} catch (IOException e) {
Log.e("TweetMonster", e.toString());
} catch (SAXException e) {
tweetDB.close();
Log.e("TweetMonster", e.toString());
} catch (ParserConfigurationException e) {
Log.e("TweetMonster", e.toString());
}
tweetDB.close();
}
It doesn't even get into my startElement method.
Upvotes: 2
Views: 1873
Reputation: 2186
In case it helps others, I too had exactly same error message pop up from an iPhone port to Android and was simply because the XML was missing the declartion line at the top of the XML for an unregistered type.
e.g. for the blah
type tag found somewhere in the XML like
<blah:abc>content</blah:abc>
we'd need something like the following...
<parentelement xmlns:blah="http://www.blah.com/dtds/blah.dtd" version="2.0" >
<blah:abc>content</blah:abc>
</parentelement>
If it's in an external RSS feed you have no control over you could read the file contents in and append the relevant missing text before running it through the parser as normal whilst also contacting the RSS producer to ask them to conform to the standard.
Upvotes: 1
Reputation: 516
If you want to troubleshoot such issues, I recommend that you print the response from the server. I previously got "ExpatParser$ParseException: At line 1, column 0: syntax error" when I had configured HttpClient not to follow redirects, and the received response was something like "This page has moved" instead of the XML required.
You could do something like:
BufferedReader br = new BufferedReader(new InputStreamReader(currentFeed.url.openStream()));
String str = null;
while ((str = br.readLine()) != null)
System.out.println(str);
just to see what the response actually is.
Upvotes: 2