Created
April 23, 2014 00:03
-
-
Save codebycliff/11198553 to your computer and use it in GitHub Desktop.
Android get hashmap resource from xml.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Map<String, String> getHashMapResource(Context c, int hashMapResId) { | |
Map<String, String> map = null; | |
XmlResourceParser parser = c.getResources().getXml(hashMapResId); | |
String key = null, value = null; | |
try { | |
int eventType = parser.getEventType(); | |
while (eventType != XmlPullParser.END_DOCUMENT) { | |
if (eventType == XmlPullParser.START_DOCUMENT) { | |
Log.d("utils", "Start document"); | |
} | |
else if (eventType == XmlPullParser.START_TAG) { | |
if (parser.getName().equals("map")) { | |
boolean isLinked = parser.getAttributeBooleanValue(null, "linked", false); | |
map = isLinked | |
? new LinkedHashMap<String, String>() | |
: new HashMap<String, String>(); | |
} | |
else if (parser.getName().equals("entry")) { | |
key = parser.getAttributeValue(null, "key"); | |
if (null == key) { | |
parser.close(); | |
return null; | |
} | |
} | |
} | |
else if (eventType == XmlPullParser.END_TAG) { | |
if (parser.getName().equals("entry")) { | |
map.put(key, value); | |
key = null; | |
value = null; | |
} | |
} | |
else if (eventType == XmlPullParser.TEXT) { | |
if (null != key) { | |
value = parser.getText(); | |
} | |
} | |
eventType = parser.next(); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
return map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment