Android xml读写
2010-05-31 14:32:00 来源:WEB开发网}
}
如果您以前使用过 SAX,那么这对您肯定非常熟悉。与任何 SAX 实现相同,大多数细节都在 SAX 处理程序中。在分解 XML 文档时,处理程序从 SAX 解析器接收事件。在本例中,您创建了一个新的名称为 de >RssHandlerde > 的类,并将它注册为解析器的处理程序,如 清单 6 所示。
清单 6. SAX 处理程序
import static org.developerworks.android.BaseFeedParser.*;
public class RssHandler extends DefaultHandler{
private List< Message > messages;
private Message currentMessage;
private StringBuilder builder;
public List< Message > getMessages(){
return this.messages;
}
@Override
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();
更多精彩
赞助商链接