1
我在資產文件夾中有一個「config.xml」文件。我使用以下代碼從中讀取:如何在XML文件中編寫?
public static String readAppConfigKey(Context context, String section,
String key) {
String value = "";
AssetManager assetManager = context.getAssets();
InputStream istr;
try {
istr = assetManager.open("config.xml");
XmlPullParserFactory factory;
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xmlParser = factory.newPullParser();
xmlParser.setInput(istr, "UTF-8");
String strPrevElement = "";
String strElement = "";
String strKey = "";
xmlParser.next();
int eventType = xmlParser.getEventType();
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
if (xmlParser.getName().compareTo(strElement) != 0) {
// after any change
strPrevElement = strElement;
strElement = xmlParser.getName();
}
strKey = xmlParser.getAttributeValue(null, "key");
if (strPrevElement.compareTo(section) == 0
&& strKey != null && strKey.compareTo(key) == 0) {
value = xmlParser.getAttributeValue(null, "value");
return value;
}
}
eventType = xmlParser.next();
}
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
return value;
}
如何使用XmlPullParser在其中編寫?
謝謝,
請問您可以查看我的答案。 – nickfox