Reputation: 31
I am trying to create a rss reader for android. So I am connecting to a rss ulr and get some XML information. This is the link - http://www.bulgarianhistory.org/feed/ If you open it to see the source, you will notice that there is one tag content:encoded. How can I get the information within this tag? My code just skips it! I am using SAX. And this is my Parser class:
public class RSSParser extends DefaultHandler {
private final static String TAG_ITEM = "item";
private final static String[] xmltags = { "title", "link", "pubDate", "description", "content" };
private RSSItem currentitem = null;
private ArrayList<RSSItem> itemarray = null;
private int currentindex = -1;
private boolean isParsing = false;
private StringBuilder builder = new StringBuilder();
public RSSParser(ArrayList<RSSItem> itemarray) {
super();
this.itemarray = itemarray;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
if(isParsing && -1 != currentindex && null != builder)
{
builder.append(ch,start,length);
}
}
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(localName.equalsIgnoreCase(TAG_ITEM))
{
currentitem = new RSSItem();
currentindex = -1;
isParsing = true;
itemarray.add(currentitem);
}
else
{
currentindex = itemIndexFromString(localName);
builder = null;
if(-1 != currentindex)
builder = new StringBuilder();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if(localName.equalsIgnoreCase(TAG_ITEM))
{
isParsing = false;
}
else if(currentindex != -1)
{
if(isParsing)
{
switch(currentindex)
{
case 0: currentitem.title = builder.toString(); break;
case 1: currentitem.link = builder.toString(); break;
case 2: currentitem.date = builder.toString(); break;
case 3: currentitem.description= builder.toString(); break;
case 4: currentitem.content = builder.toString(); break;
}
}
}
}
private int itemIndexFromString(String tagname){
int itemindex = -1;
for(int index= 0; index<xmltags.length; ++index)
{
if(tagname.equalsIgnoreCase(xmltags[index]))
{
itemindex = index;
break;
}
}
return itemindex;
}
}
Upvotes: 3
Views: 357
Reputation: 3679
Looks like tag with namespace is ignored by your if
. Just try printing tag name here
private int itemIndexFromString(String tagname){
int itemindex = -1;
for(int index= 0; index<xmltags.length; ++index)
{
**PRINT (tagname)**
if(tagname.equalsIgnoreCase(xmltags[index]))
{
if it prints as content:encoded
, you would need to modify xmltags []
to inlcude content:encoded
instead of content
Upvotes: 1
Reputation: 72311
I would really recommend not to use Sax
on Android, since as far as I remember it's performance is really slow on Android. You should be using XmlPullParser
which comes with Android
, so no need to add any external jars
and also you will have significant performance improvements. I have just tested and XmlPullParser
would parse the <content:encoded>
tag as any other tags, so it will work.
Upvotes: 1