XML 文字列の子ノードを取得するには、どのような方法が望ましいでしょうか? Android で XmlPullParser を使用した解析の良い例がないようです。たとえば、これを解析したい場合:
<Point>
<Id>81216</Id>
<Name>Lund C</Name>
</Point>
私は仕事をする方法を思いついた:
List<Point> points = new ArrayList<Point>();
String id = null
name = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && xpp.getName().equals("Id")) {
try {
id = xpp.nextText();
} catch (Exception e) {
e.printStackTrace();
}
}
else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals("Name")) {
try {
name = xpp.nextText();
} catch (Exception e) {
e.printStackTrace();
}
else if(eventType == XmlPullParser.END_TAG && xpp.getName().equals("Point")) {
Point point = new Point(id, name);
points.add(point);
}
try {
eventType = xpp.next();
} catch (exception e) {
e.printStackTrace();
}
}
for (Point p : points) {
tv.append(p.getId() + " | " + p.getName() + "\n");
}
しかし、それは本当に醜いようです。これを行うより良い方法はありますか?