Android read XML file to implement province, city, district selection

Source: Internet
Author: User
Tags sqlite database

If you want to reprint this blog. Please indicate the source.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------

Today to achieve provinces, cities. Area of choice, to the network Baidu a bit. Found that a lot of implementations are used by the SQLite database, but my side in order to ensure that the data and iOS there is unified, just use the plist file over there. This is what we often say about XML files. Just want to find a network to read the XML file to achieve. Found basically no, even if there is. It also enumerates all the useful arrays of resources, rather than reading them in real time.

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 is required to read what, assuming that the desired resources are obtained. The read is stopped.

3. Read asynchronously.

Improved:

1. The read resource is not cached. Let's look at the detailed requirements.

2. The code implementation algorithm can be optimized.

3.xml file structure can be optimized, this is the resources that iOS directly to me. But looked very without words, city.xml less, directly manually changed a bit, Area.xml file is 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 implement in detail, preferably a more reasonable structure of the XML file, in the implementation of a better algorithm. Remember, this blog is just to provide a way of thinking! ~ ~

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) {//assumed to be the start tag if (xrpcity.geteventtype () = = XMLPULLP Arser.                        Start_tag) {//Gets the label name String ' = Xrpcity.getname ();                     Infer 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) {//assumed to be 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) {//assumed to be 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;}

Above is the basic implementation, will be the function of a small project, to Csdn after. Give the demo address, and resources.

Address such as the following: http://download.csdn.net/detail/jiguangcanhen/8152421

The project is ant architecture, gradle architecture of lazy to get it done.

Android read XML file to implement province, city, district selection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.