我试图从xml文件中提取信息,这是我目前正在开发的代码,以使其正常工作。
private void parseXML(XmlPullParser parser)
throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
Map<String, Map<String, Double>> sports = new HashMap<String, Map<String, Double>>();
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String sportName = null;
boolean paid = false;
Map<String, Double> preset = null;
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
preset = new HashMap<String, Double>();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("sport")) {
sportName = parser.getAttributeValue(0);
paid = Boolean.parseBoolean(parser
.getAttributeValue(1));
} else if (name.equals("preset")) {
// TODO figure out how to loop this so it finished up the sport and then moves on to the next one
String presetName = parser
.getAttributeValue(0);
double value = Double.parseDouble(parser
.nextText());
preset.put(presetName, value);
parser.next();
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
}
sports.put(sportName, preset);
eventType = parser.next();
}
}
}
这是从
中提取信息的xml<?xml version="1.0" encoding="utf-8"?>
<sports>
<sport name="Baseball" paid="false">
<preset name="Pitching Mound">726.0</preset>
<preset name="Base Distance">1080.0</preset>
</sport>
<sport name="Basketball" paid="false">
<preset name="NBA Free Throw Line">181.08</preset>
<preset name="NBA 3pt Line">265.8</preset>
</sport>
<sport name="Cricket" paid="true">
<preset name="Cricket Pitch">2012.0</preset>
</sport>
</sports>
代码本质上做的是获取运动名称,无论是否为付费功能,以及我为其定义的预设。我尝试做的是制作部件,在预设地图中添加预设,循环直到运动预设结束。我对使用xml解析不是很满意,因为我对使用它感到困惑。当代码获得运动的名称时,我尝试获取属性计数,然后循环它,但它没有用。
答案 0 :(得分:2)
虽然android文档说它建议使用XmlPullParser,但你可以使用你选择的解析器。
要获取属性,您可以使用parser.getAttributeValue(null, "name")
因为您有<preset name="Pitching Mound">
。名称是属性。
要使文字使用text
。
文档中有一个例子。它对每个标签都有单独的方法,而方法是相同的。
http://developer.android.com/training/basics/network-ops/xml.html
您可以使用以下内容作为参考。
public class XMLPullParserHandler {
private String text;
public XMLPullParserHandler() {
}
public Void parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("sports")) {
}
if (tagname.equalsIgnoreCase("preset")) {
Log.i(".......","Attribute value is"+parser.getAttributeValue(null, "name"));
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("sports")) {
// add employee object to list
} else if (tagname.equalsIgnoreCase("sport")) {
// no value
}
else if (tagname.equalsIgnoreCase("preset")) {
Log.i("Preset is",text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
日志输出
03-25 02:03:00.412: I/Attribute value is(1119): Pitching Mound
03-25 02:03:00.412: I/Preset is(1119): 726.0
03-25 02:03:00.412: I/Attribute value is(1119): Base Distance
03-25 02:03:00.422: I/Preset is(1119): 1080.0
03-25 02:03:00.422: I/Attribute value is(1119): NBA Free Throw Line
03-25 02:03:00.422: I/Preset is(1119): 181.08
03-25 02:03:00.422: I/Attribute value is(1119): NBA 3pt Line
03-25 02:03:00.422: I/Preset is(1119): 265.8
03-25 02:03:00.422: I/Attribute value is(1119): Cricket Pitch
03-25 02:03:00.422: I/Preset is(1119): 2012.0
编辑:
要获得付费属性,只需在case XmlPullParser.START_TAG:
else if (tagname.equalsIgnoreCase("sport")) {
Log.i("Attribute value is"," "+parser.getAttributeValue(null, "paid"));
}