Plist, or property list, is a common storage file format used in Apple mobile phone software development. Its essence is XML files, but it follows a certain format. For example, it contains some specified elements, such as plist, array, string, and key. Apple provides a mechanism for parsing such files, but it is a pity that it is not provided in Android development tools. We often encounter a situation where we need to unify resource files when developing apple and android mobile phone software. At this time, we can use the open-source android-plist-parser solution provided by GitHub.
For example, we want to parse a plist file for provinces, cities, and cities nationwide:
Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE plist PUBLIC "-// Apple // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version = "1.0"> <dict> <key> 0 </key> <dict> <key> Beijing </key> <dict> <key> 0 </key> <dict> <key> Beijing </key> <array> <string> Dongcheng District </string> <string> Xicheng district </string> <string> Chaoyang District </string> <string> Fengtai District </string> <string> Shijingshan district </string> <string> Haidian District </string> <string> Mentougou District </string> <string> Fangshan District </string> <string> Tongzhou District </str Ing> <string> Shunyi District </string> <string> Changping District </string> <string> Daxing district </string> <string> Huairou district </string> <string> Pinggu district </ string> <string> Miyun county </string> <string> Yanqing County </string> </array> </dict> ......... </dict> </plist> <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE plist PUBLIC "-// Apple // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version = "1.0"> <dict> <key> 0 </key> <dict> <key> Beijing </key> <dict> <key> 0 </key> <dict> <key> Beijing </key> <array> <string> Dongcheng District </string> <string> Xicheng district </string> <string> Chaoyang District </string> <string> Fengtai District </string> <string> Shijingshan district </string> <string> Haidian District </string> <string> Mentougou District </string> <string> Fangshan District </string> <string> Tongzhou District </string> <string> Shunyi District </string> <string> Changping District </string> <string> Daxing district </string> <string> Huairou district </string> <string> Pinggu district </string> <string> Miyun county </string> <string> Yanqing County </string> </array> </dict> ......... </dict> </plist>
So how to parse it in android?
I. Download android-plist-parser from GitHub
2. decompress the downloaded package and import it to the class path of the android project.
3. parse the file and print the parsing information on the console. (Because the file is not too small, it runs in the new thread to prevent the master thread from getting stuck)
New Thread (new Runnable () {@ Override public void run () {PListXMLParser parser = new PListXMLParser (); // The SAX-based implementation of PListXMLHandler handler = new PListXMLHandler (); parser. setHandler (handler); try {parser. parse (getAssets (). open ("area. plist "); // area. plist is the file to be parsed. The file must be placed in the assets folder} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} PList actualPList = (PListXMLHandler) parser. getHandler ()). getPlist (); Dict root = (Dict) actualPList. getRootElement (); Map <String, PListObject> provinceCities = root. getConfigMap (); for (int I = 0; I <provinceCities. keySet (). size (); I ++) {Dict provinceRoot = (Dict) provinceCities. get (String. valueOf (I); Map <String, PListObject> province = provinceRoot. getConfigMap (); String provinceName = province. keySet (). iterator (). next (); System. out. println ("province:" + provinceName); // print the province Dict cityRoot = (Dict) province. get (provinceName); Map <String, PListObject> cities = cityRoot. getConfigMap (); for (int j = 0; j <cities. keySet (). size (); j ++) {Dict city = (Dict) cities. get (String. valueOf (j); String cityName = city. getConfigMap (). keySet (). iterator (). next (); System. out. println ("city:" + cityName); // print the city Array districts = city. getConfigurationArray (cityName); for (int k = 0; k <districts. size (); k ++) {com. longevitysoft. android. xml. plist. domain. string district = (com. longevitysoft. android. xml. plist. domain. string) districts. get (k); System. out. println ("region:" + district. getValue (); // print region }}}}}). start (); new Thread (new Runnable () {@ Override public void run () {PListXMLParser parser = new PListXMLParser (); // implement PListXMLHandler handler = new PListXMLHandler (); parser. setHandler (handler); try {parser. parse (getAssets (). open ("area. plist "); // area. plist is the file to be parsed. The file must be placed in the assets folder} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} PList actualPList = (PListXMLHandler) parser. getHandler ()). getPlist (); Dict root = (Dict) actualPList. getRootElement (); Map <String, PListObject> provinceCities = root. getConfigMap (); for (int I = 0; I <provinceCities. keySet (). size (); I ++) {Dict provinceRooSystem. out. println ("province:" + provinceName); // print the province Dict cityRoot = (Dict) province. get (provinceName); Map <String, PListObject> cities = cityRoot. getConfigMap (); for (int j = 0; j <cities. keySet (). size (); j ++) {Dict city = (Dict) cities. get (String. valueOf (j); String cityName = city. getConfigMap (). keySet (). iterator (). next (); System. out. println ("city:" + cityName); // print the city Array districts = city. getConfigurationArray (cityName); for (int k = 0; k <districts. size (); k ++) {com. longevitysoft. android. xml. plist. domain. string district = (com. longevitysoft. android. xml. plist. domain. string) districts. get (k); System. out. println ("region:" + district. getValue (); // print region }}}}}). start (); t = (Dict) provinceCities. get (String. valueOf (I); Map <String, PListObject> province = provinceRoot. getConfigMap (); String provinceName = province. keySet (). iterator (). next ();