我試圖從xml文件中提取信息,這是我目前正在開發的使其工作的代碼。循環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 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什麼的代碼基本不會越來越這項運動的名稱,無論是付費功能,或者不,我已經定義的預設爲了它。我想要做的是製作部分,在預設地圖上添加預設,直到運動預設結束。我對使用xml解析並不滿意,因爲我對使用它感到困惑。當代碼獲得該運動的名稱時,我試圖獲得屬性計數,然後循環它但它沒有工作。