by frankie » Mon Apr 19, 2010 12:02 pm
thanks for the swift reply.
My xml file is somthing like this.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title> News - Top Stories </title>
<link>http://www.news.com/news/</link>
<language>en</language>
<lastBuildDate>April 19, 2010 3:15 PM</lastBuildDate>
<image>
<url></url>
<link></link>
</image>
<item>
<title><![CDATA[EU says half of normal flights may run today]]></title>
<link></link>
<guid isPermaLink="false">20352</guid>
<pubDate>April 19, 2010 3:09 PM</pubDate>
<AlsoSeeLink1>http://</AlsoSeeLink1>
<MobileText><![CDATA[Some European airports were reopening to limited traffic on Monday after volcanic ash forced their closures, a day after the European Union said that if weather forecasts confirm the skies are clearing, air traffic over the continent could return to about 50 per cent of normal levels.]]></MobileText>
<StoryImage></StoryImage>
<DateLine><![CDATA[]]></DateLine>
<description><![CDATA[Some European airports were reopening to limited traffic on Monday after volcanic ash forced their closures, a day after the European Union said that if weather forecasts confirm the skies are clearing, air traffic over the continent could return to about 50 per cent of normal levels.]]></description>
</item>
The statement to parse is :
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,root.getContentHandler());
and
my handler is
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
builder.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
super.endElement(uri, localName, name);
if (this.currentMessage != null){
if (localName.equalsIgnoreCase(TITLE)){
currentMessage.setTitle(builder.toString());
} else if (localName.equalsIgnoreCase(LINK)){
currentMessage.setLink(builder.toString());
} else if (localName.equalsIgnoreCase(DESCRIPTION)){
currentMessage.setDescription(builder.toString());
} else if (localName.equalsIgnoreCase(PUB_DATE)){
currentMessage.setDate(builder.toString());
} else if (localName.equalsIgnoreCase(ITEM)){
messages.add(currentMessage);
}
builder.setLength(0);
}
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
messages = new ArrayList<Message>();
builder = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if (localName.equalsIgnoreCase(ITEM)){
this.currentMessage = new Message();
}
}
}