【发布时间】:2011-09-01 21:06:51
【问题描述】:
我有一些数据存储在一个 xml 文件中,如下所示:
<group name="example">
<article name="foo">
<content>Lorem ipsum dolor sit amet.</content>
<link>
<title>Lipsum</title>
<url>http://www.lipsum.com/feed/html</url>
</link>
</article>
<article name="bar">
<content>Lorem ipsum dolor sit amet.</content>
<link>
<title>Google</title>
<url>http://www.google.com</url>
</link>
</article>
</group>
说我要解析具体的文章name="foo"在android中。到目前为止,我一直在使用 android.sax 包,但我不知道它是否具有此功能。或者,如果有其他方法可以做到这一点,我将不胜感激。
编辑: 这是我到目前为止的代码。我觉得我错过了什么。问题是它从所有文章中添加信息,而不仅仅是我想要的特定文章。
public Article parse(final String articleTitle) {
RootElement root = new RootElement("learning");
Element group = root.getChild("group");
final Element article = group.getChild("article");
article.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
if (attributes.getValue("name").equals(articleTitle)) {
currentArticle = new Article();
currentArticle.setTitle(articleTitle);
setupArticle(currentArticle);
}
}
private void setupArticle(final Article currentArticle) {
article.getChild("content").setEndTextElementListener(
new EndTextElementListener() {
@Override
public void end(String body) {
currentArticle.setContent(body);
}
});
Context context = Application.getAppContext();
InputStream raw;
try {
raw = context.getAssets().open("learning_articles.xml");
Xml.parse(raw, Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return currentArticle;
【问题讨论】:
标签: android xml xml-parsing sax