If you want to reprint this blog, please indicate the source.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------
Today to achieve the province, city, district selection, to the network Baidu a bit, found that many implementations are used SQLite database, but my side in order to ensure and iOS over the data unified, as long as the plist file over there, that is, we often say that the XML file, Just want to find a network to read the XML file to achieve, found that basically no, even if there, but also all the resources are enumerated in the array, rather than real-time reading.
Implementation on the network:
1. Use SQLite to access the DB database.
2. Enumerate all the XML resources.
My implementation:
1. Read the XML file dynamically.
2. What to read and stop reading if you get the resources you want.
3. Read asynchronously.
Improved:
1. Read the resource is not cached, this depends on the specific needs.
2. The code implementation algorithm can be optimized.
3.xml file structure can be optimized, this is the iOS directly to my resources, but looked very silent, city.xml relatively few, directly manually modified a bit, area.xml file more, there is no change.
Suggestions:
This blog is just to provide you with a realization of the idea, we are not good copy, the best according to their own needs to achieve concrete, preferably a more reasonable structure of the XML file, in the implementation of a more excellent algorithm, remember, this blog is just to provide a train of thought! ~ ~
The basic structure of city.xml
<?xml version= "1.0" encoding= "UTF-8"?><array><dict> <key>state</key><string > Beijing </string><key>cities</key><array><string> Tongzhou </string><string> Fangshan </string><string> changping </string><string> Shunyi </string><string> Huairou </string> <string> daxing </string><string> Miyun </string><string> pinggu </string><string> yanqing </string><string> Dongcheng </string><string> Chongwen </string><string> Xicheng </string> <string> Chaoyang </string><string> Xuanwu </string><string> Shijingshan </string><string> Fengtai </string><string> Mentougou </string><string> Haidian </string></array></dict> ....</array>
1. Reading the city code
/** * Get Province data list */private list<string> getprovince () {//result store list<string> resultlist = new arraylist<string > (); Flag whether to read the contents of the next node Boolean nextread = false; try {xmlpullparser xrpcity = Xml.newpullparser (); Xrpcity.setinput (Getassets (). Open ("City.xml"), "UTF-8"); while ( Xrpcity.geteventtype ()! = xmlpullparser.end_document) {//If the start tag if (xrpcity.geteventtype () = = XMLPULLP Arser. Start_tag) {//Gets the label name String ' = Xrpcity.getname (); Determines whether the label name equals friend if ("Key". Equals (name)) {Xrpcity.next (); String value = Xrpcity.gettext (); if ("state". Equals (value)) {Nextread = true; }}else if ("string". Equals (name) && nextread) {xrpcity.next (); Resultlist.add (Xrpcity.gettext ()); Nextread = False; }}//Next label xrpcity.next (); }} catch (Xmlpullparserexception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} catch (Ioex Ception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} return resultlist;}
2. Reading the city code
/** * Get city list */private list<string> getcity (String provincename) {//result store list<string> resultlist = new ArrayLi St<string> (); Flag whether to read the contents of the next node Boolean nextread = false; Read tag for City node content Boolean readcity = false; try {xmlpullparser xrpcity = Xml.newpullparser (); Xrpcity.setinput (Getassets (). Open ("City.xml"), "UTF-8"); while ( Xrpcity.geteventtype ()! = xmlpullparser.end_document) {//If the start tag if (xrpcity.geteventtype () = = XMLPULLP Arser. Start_tag) {//Gets the label name String ' = Xrpcity.getname (); if ("Key". Equals (name)) {Xrpcity.next (); String value = Xrpcity.gettext (); if ("state". Equals (value)) {Nextread = true; if (readcity) {break; } readcity = false; }}else if ("string". Equals (name) && NextreAD) {xrpcity.next (); Nextread = false; if (Provincename.equals (Xrpcity.gettext ())) {readcity = true; }}else if ("string". Equals (name) && readcity) {xrpcity.next (); Resultlist.add (Xrpcity.gettext ()); }}//Next label xrpcity.next (); }} catch (Xmlpullparserexception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} catch (Ioex Ception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} return resultlist;}
The basic structure of area.xml resources
<?xml version= "1.0" encoding= "UTF-8"?><array> .... <dict><key>areas</key><array><string> Lianshan County </string><string> Yangshan County </string><string> Fogang County </string><string> Qingcheng District </string><string> Liannan County </ string><string> Qingxin County </string><string> Yingde </string><string> Lianzhou </string>< /array><key>city</key><string> Qingyuan </string></dict> .....</array>
1. Code for the Read area
/** * Get list */private list<string> getregion (String citname) {//result store list<string> resultlist = new ARRAYLIST&L T String> (); Flag whether to read the contents of the next node Boolean nextread = false; Read tag for City node content Boolean readcity = false; try {xmlpullparser xrpcity = Xml.newpullparser (); Xrpcity.setinput (Getassets (). Open ("Area.xml"), "UTF-8"); while ( Xrpcity.geteventtype ()! = xmlpullparser.end_document) {//If the start tag if (xrpcity.geteventtype () = = XMLPULLP Arser. Start_tag) {//Gets the label name String ' = Xrpcity.getname (); if ("Key". Equals (name)) {Xrpcity.next (); String value = Xrpcity.gettext (); if ("Areas". Equals (value)) {Nextread = true; Readcity = false; }else if ("City". Equals (value)) {readcity = true; Nextread = false; }}else If("string". Equals (name) && nextread) {Xrpcity.next (); Resultlist.add (Xrpcity.gettext ()); }else if ("string". Equals (name) && readcity) {xrpcity.next (); if (Citname.equals (Xrpcity.gettext ())) {break; }else{resultlist.clear (); }}}//Next label xrpcity.next (); }} catch (Xmlpullparserexception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} catch (Ioex Ception e) {//TODO auto-generated catch Blocke.printstacktrace (); resultlist = null;} return resultlist;}
The above is the main implementation, will be the function of a small project, to Csdn, after giving the demo address, and resources.
Android read XML file to implement province, city, district selection