在尝试解析 RDF 提要(Android 解析器上的新手)时,我完全被困了几天。尽管使用:
Element item = root.getChild("", ITEM);
因为我的项目来自根目录而不是 CHANNEL,正如这里指定的那样。无论如何,我只检索 0 条消息
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://...">
<title>Notícies</title>
<link>...</link>
<description></description>
<syn:updatePeriod>daily</syn:updatePeriod>
<syn:updateFrequency>1</syn:updateFrequency>
<syn:updateBase>2009-08-25T16:01:24Z</syn:updateBase>
<image rdf:resource="..."/>
<items>
<rdf:Seq>
<rdf:li rdf:resource="..."/>
<rdf:li rdf:resource="..."/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="...">
<title>...</title>
<link>....</link>
<description>...</description>
<dc:publisher>No publisher</dc:publisher>
<dc:creator>administrador</dc:creator>
<dc:rights></dc:rights>
<dc:date>2013-03-22T16:18:36Z</dc:date>
<dc:type>Noticia</dc:type>
</item>
<item rdf:about="...">
<title>...</title>
<link>....</link>
<description>...</description>
<dc:publisher>No publisher</dc:publisher>
<dc:creator>administrador</dc:creator>
<dc:rights></dc:rights>
<dc:date>2013-03-22T16:18:36Z</dc:date>
<dc:type>Noticia</dc:type>
</item>
<item rdf:about="...">
<title>...</title>
<link>....</link>
<description>...</description>
<dc:publisher>No publisher</dc:publisher>
<dc:creator>administrador</dc:creator>
<dc:rights></dc:rights>
<dc:date>2013-03-22T16:18:36Z</dc:date>
<dc:type>Noticia</dc:type>
</item>
...
</rdf:RDF>
使用我的 SAX 解析器如下:
public class AndroidSaxFeedParser extends BaseFeedParser {
static final String RDF_NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
static final String RSS_NAMESPACE = "";
static final String RSS = "RDF";
public AndroidSaxFeedParser(String feedUrl) {
super(feedUrl);
}
public List<Message> parse() {
final Message currentMessage = new Message();
//RootElement root = new RootElement(RSS);
RootElement root = new RootElement(RDF_NAMESPACE, RSS);
final List<Message> messages = new ArrayList<Message>();
//Element channel = root.getChild(CHANNEL);
//Element channel = root.getChild(RDF_NAMESPACE, CHANNEL);
Log.e("Valor Root", root.toString());
Element item = root.getChild("", ITEM);
//Element item = channel.getChild(RDF_NAMESPACE,ITEM);
item.setEndElementListener(new EndElementListener(){
public void end() {
messages.add(currentMessage.copy());
}
});
item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setTitle(body);
}
});
item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setLink(body);
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setDescription(body);
}
});
item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentMessage.setDate(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
我试过了
元素 item = root.getChild("RDF_NAMESPACE", ITEM); 元素 item = root.getChild("", ITEM); 元素项 = root.getChild(ITEM);
但是这 3 个选项返回 0 条消息。任何帮助将不胜感激。提前谢谢了。