У меня есть некоторые данные, хранящиеся в 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;