在 android sax 解析器中使用 RootElement 中的命名空间解析 xml
Parse xml with namespace in RootElement in android sax parser
我尝试解析这个 rss news using android sax parser。
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement("rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild("channel");
Element item = channel.getChild(ITEM);
但我发现异常:
05-08 18:29:25.004 2820-2820/com.xmlsimplesaxparser V/SimpleRss2ParserDemo﹕ android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'rss', Got: 'http://backend.userland.com/rss2:rss'
当我添加命名空间时:
static final String NAMESPACE ="http://backend.userland.com/rss2";
:
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement(NAMESPACE, "rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild(NAMESPACE, "channel");
Element item = channel.getChild(NAMESPACE, ITEM);
我得到 NullPointerException
。如何在 sax 解析器中使用命名空间?
我更正了这个问题:需要为所有标签添加命名空间
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement(NAMESPACE, "rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild(NAMESPACE, "channel");
//Element mainTitle = channel.getChild(NAMESPACE,"title");
Element item = channel.getChild(NAMESPACE,ITEM);
item.setEndElementListener(new EndElementListener() {
public void end() {
messages.add(currentMessage.copy());
}
});
item.getChild(NAMESPACE,TITLE).setEndTextElementListener(new EndTextElementListener() { //this add namespace too
public void end(String body) {
currentMessage.setTitle(body);
}
});
我尝试解析这个 rss news using android sax parser。
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement("rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild("channel");
Element item = channel.getChild(ITEM);
但我发现异常:
05-08 18:29:25.004 2820-2820/com.xmlsimplesaxparser V/SimpleRss2ParserDemo﹕ android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'rss', Got: 'http://backend.userland.com/rss2:rss'
当我添加命名空间时:
static final String NAMESPACE ="http://backend.userland.com/rss2";
:
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement(NAMESPACE, "rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild(NAMESPACE, "channel");
Element item = channel.getChild(NAMESPACE, ITEM);
我得到 NullPointerException
。如何在 sax 解析器中使用命名空间?
我更正了这个问题:需要为所有标签添加命名空间
final RSSItem currentMessage = new RSSItem();
RootElement root = new RootElement(NAMESPACE, "rss");
final List<RSSItem> messages = new ArrayList<RSSItem>();
Element channel = root.getChild(NAMESPACE, "channel");
//Element mainTitle = channel.getChild(NAMESPACE,"title");
Element item = channel.getChild(NAMESPACE,ITEM);
item.setEndElementListener(new EndElementListener() {
public void end() {
messages.add(currentMessage.copy());
}
});
item.getChild(NAMESPACE,TITLE).setEndTextElementListener(new EndTextElementListener() { //this add namespace too
public void end(String body) {
currentMessage.setTitle(body);
}
});