使用 Android 解析 XML
我正在开发一个 Android 2.2 应用程序。
我在 res/xml 上有以下 XML 文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<turo>
<name>Rodo</name>
<latitude>37.212123</latitude>
<longitude>0.1231231</longitude>
</turo>
</data>
我快疯了,因为我找不到示例来了解如何解析该文件。
我还有以下类来存储检索到的数据:
public class Turo{
private String name;
private Location location;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Turo(String name){
setName(name);
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
}
这是我解析它的方法(未完成):
public Vector<Turo> getTurosFromXML(Activity activity, int xmlId) throws XmlPullParserException, IOException {
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(xmlId);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
...
}
}
你能帮我吗?
I'm developing an Android 2.2 application.
I have the following XML file on res/xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<turo>
<name>Rodo</name>
<latitude>37.212123</latitude>
<longitude>0.1231231</longitude>
</turo>
</data>
I'm getting crazy because I can't find an example to see how can I parse this file.
I also have the following class to store data retrieved:
public class Turo{
private String name;
private Location location;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Turo(String name){
setName(name);
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
}
This my method to parse it (it's incompleted):
public Vector<Turo> getTurosFromXML(Activity activity, int xmlId) throws XmlPullParserException, IOException {
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(xmlId);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
...
}
}
Can you help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这个示例:
}
Android UI XML 文件将是:
本示例中解析的
XML
是这样的:注意:从
Android-er
中提取的信息You can try this example:
}
The Android UI XML file would be:
And the
XML
parsed in this example is this one:NOTE: Info extracted from
Android-er