在BlackBerry应用程序中,我使用SAX解析器来解析我的XML。我知道如何使用ContentHandler接口进行SAX解析。但我想使用DefaultHandler类解析我的XML 所以我只能提供我需要的那些方法的定义。 任何人都可以提供一个示例代码,我如何使用DefaultHandler 在BlackBerry中进行SAX解析的课程
答案 0 :(得分:0)
用于xml,如
<items>
<item>
<title>title content </title>
..
</item>
...
</items>
class XMLRSSHandler extends DefaultHandler {
static final String TITLE = "title";
static final String ITEM = "item";
boolean isItem = false;
boolean isTitle = false;
Vector items = new Vector();
String value = "";
private Item currentItem = null;
public XMLRSSHandler() {
}
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if (name.equalsIgnoreCase(ITEM)) {
currentItem = new Item();
} else if (currentItem != null) {
if (name.equalsIgnoreCase(TITLE)) {
isTitle = true;
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (isTitle) {
value = new String(ch, start, length).trim();
}
}
public void endElement(String uri, String localName, String name) throws SAXException {
if (isTitle && name.equalsIgnoreCase(TITLE)) {
isTitle = false;
currentItem.setTitle(value);
return;
}
if (currentAItem != null
&& (name.equalsIgnoreCase(ITEM)) {
Logger.debug("Loaded Item " + currentItem.getTitle());
items.addElement(currentArticle);
currentItem= null;
}
}
}