I am trying to parse Preferences Xml file in android but not able to parse that getAttributesCount method
{ k = xmlResourceParser.getAttributeCount(); }
is always returning zero due to which I am not able to parse that file. How to successfully parse that Preferences xml file?
XmlResourceParser xmlResourceParser = Context.getResources().getXml(paramInt);
String key = null;
String title = null;
String summary = null;
String attributeName = null;
try {
do {
xmlResourceParser.next();
i = xmlResourceParser.next();
} while (i != xmlResourceParser.START_TAG && i != xmlResourceParser.END_DOCUMENT);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
if (i == 1) {
return;
}
if (i == 2) {
k = xmlResourceParser.getAttributeCount();
Log.d("attributesCount", String.valueOf(k));
for (int j = 0; j < k; j++) {
Log.d("attriute name", xmlResourceParser.getAttributeName(j));
if (xmlResourceParser.getAttributeNamespace(j).equals(ANDROID_SCHEME)) {
attributeName = xmlResourceParser.getAttributeName(j);
if (attributeName.equals("key")) {
key = xmlResourceParser.getAttributeValue(i);
Log.d("key", key);
}
if (attributeName.equals("title")) {
title = xmlResourceParser.getAttributeValue(i);
Log.d("Title inside method", title);
}
if (attributeName.equals("summary")) {
summary = xmlResourceParser.getAttributeValue(i);
}
}
}
}
This is the Xml file that I want to parse. In Android I am using this xml file for user settings.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--below line is to create preference category-->
<PreferenceCategory android:title="General">
<!--in below line we are creating a list preference
and we are adding default selected value in list for 3 rd index-->
<!--dialog title is to set title for our dialog box
entries is used to add list of data which we
are adding from our strings file
entry values is to add values to our entries.
key is use to add key to our list preferences
summary is use to add description to our option
title is use to add title to our list preferences.-->
<!--this list preference is for remind me option-->
<ListPreference
android:defaultValue="3"
android:dialogTitle="@string/remind_to_take_a_break"
android:entries="@array/pref_remind_me_to_take_a_break"
android:entryValues="@array/pref_duration"
android:key="@string/key_upload_quality"
android:summary="@string/remind_me"
android:title="@string/remind_me" />
<!--on below line we are creating a switch preference
default value is use to set switch on or off
key is use to set key
title is use to add title to our switch-->
<!--this switch preference option is to remind for a bed time-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/remind_me_for_bed_time"
android:title="@string/remind_for_bed_time" />
<!--below switch preference is
use for mobile data usage-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/limit_data_usage"
android:summary="@string/stream_video"
android:title="@string/limit_mobile_usage" />
<!--below list preference is use for
double tap to seek option-->
<ListPreference
android:defaultValue="1"
android:dialogTitle="@string/double_tap_to_seek"
android:entries="@array/pref_seek_values"
android:entryValues="@array/pref_duration"
android:key="@string/pref_seek_val"
android:summary="@string/seconds"
android:title="@string/double_tap_to_seek" />
<!--below option is use to create a list
preference for Upload preferences-->
<ListPreference
android:defaultValue="1"
android:dialogTitle="@string/uploads"
android:entries="@array/pref_uploads"
android:entryValues="@array/pref_duration"
android:key="@string/pref_uploads"
android:summary="@string/specify_network_prefs"
android:title="Uploads" />
<!--below switch preferences is use to restrict mode-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/prefs_restricted_mode"
android:summary="@string/restricated_mode"
android:title="@string/restricated_mode_description" />
<!--below switch pref is use for enable stats option-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/prefs_enable_stats"
android:title="@string/enable_stats" />
</PreferenceCategory>
</PreferenceScreen>
Please show us your XML. Perhaps there are no attributes?
It's not a good idea to use constants like 1 and 2 to identify the events coming from the pull parser. It means that anyone reading the code has to go to the documentation to find, for example, that 2 means START_TAG.
You've shown some of the processing for the first start tag that you encounter. This code doesn't appear to be in a loop so we can't see what you do after processing the first start tag.
I'm no expert in Android XML file formats, but in the examples I've seen, the first element start tag doesn't have any attributes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.