Android configures the database with an XML file

Source: Internet
Author: User
<span id="Label3"></p><p><p>Some time ago, I encapsulated two databases, one is the ORM database, the other is the event stream database, the corresponding address of the project is as Follows:</p></p><p><p>ORM Database: Https://github.com/wenjiang/SimpleAndroidORM</p></p><p><p>Event Stream Database: Https://github.com/wenjiang/EventStreamDB</p></p><p><p>Interested people can go up and see.</p></p><p><p>If you want to talk about these two items, the length of an article is not fit, so just a little bit of Each.</p></p><p><p>Two databases that you write yourself have a place to use: configure the database in an XML file.</p></p><p><p>The use of a file configuration database is available in many language frameworks, as this is a handy feature: whenever a database is configured in one place, no specific code is involved, and maintainability is Higher.</p></p><p><p>In android, the idea is to take advantage of XML Files.</p></p><p><p>In theory, this XML file can be placed in any folder, but preferably in the assets folder, because the files within this folder are read-only, and such configuration files are, of course, read-only.</p></p><p><p>Now we're going to specify what's in this XML file.</p></p><p><p>We create a database.xml file within the assets folder, which is the configuration file for the Database.</p></p><p><p>XML files allow us to define our own tags, as long as we have the corresponding XML parser on the LINE.</p></p><p><p>There are three common types of database configurations: database name, version, and Table.</p></p><p><p>The contents of the Database.xml file are as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Database</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">Database name</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">dbname</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">value</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "zwb.db"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">dbname</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">Database Version</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">version</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">value</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "1"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">version</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #008000;"><span style="color: #008000;"><!--</span></span><span style="color: #008000;"><span style="color: #008000;">Database Tables</span></span><span style="color: #008000;"><span style="color: #008000;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">List</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Mapping</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">class</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "com.zwb.args.dbpratice.model.Status"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">Mapping</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Mapping</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">class</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "com.zwb.args.dbpratice.model.User"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">Mapping</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">List</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Database</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>of course, These tags can be defined by themselves, such as mapping can be changed to table, but if some tags are a group, it is best to put in the list tag, so that the XML parser to facilitate parsing.</p></p><p><p>Now that we have our own XML file, we can begin to write the XML parser.</p></p><p><p>The first is to read the Database.xml file under Assets.</p></p><p><p>Android provides a read method for the assets Folder:</p></p><pre><pre> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">; </span> <span style="color: #0000ff;">Try</span> <span style="color: #000000;">{ </span>=<span style="color: #000000;"> context.getresources () . getassets (). Open (</span>"database.xml"<span style="color: #000000;">); </span> <span style="color: #0000ff;">Catch</span> <span style="color: #000000;">(ioexception e) { </span><span style="color: #0000ff;">throw</span><span style="color: #0000ff;">new</span> basesqliteexception ("database.xml is not exist" <span style="color: #000000;">); }</span></pre></pre><p><p>When reading system files or resources in Android, you can obtain the resources object through the Getresources method, and then get the corresponding resource through this Object.</p></p><p><p>After you get the inputstream of the Database.xml file, you can start reading the contents of the File.</p></p><p><p>Reading an XML file can take advantage of the xmlpullparserfactory class.</p></p><pre><span style="color: #000000;"><span style="color: #000000;">xmlpullparserfactory factory; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{factory</span></span>=<span style="color: #000000;"><span style="color: #000000;">xmlpullparserfactory.newinstance (); Factory.setnamespaceaware (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Xmlpullparser xpp</span></span>=<span style="color: #000000;"><span style="color: #000000;">Factory.newpullparser (); Xpp.setinput (in,</span></span>"UTF-8"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Evttype =<span style="color: #000000;"><span style="color: #000000;">Xpp.geteventtype (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">keep looping until the end of the document</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(evttype! =<span style="color: #000000;"><span style="color: #000000;">Xmlpullparser.end_document) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(evttype) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">XmlPullParser.START_TAG:String TAG</span></span>=<span style="color: #000000;"><span style="color: #000000;">Xpp.getname (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(tag.equals ("dbname"<span style="color: #000000;">) <span style="color: #000000;">) {dbName</span></span>= Xpp.getattributevalue (0<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(tag.equals ("version"<span style="color: #000000;">) <span style="color: #000000;">) {version</span></span>= integer.valueof (xpp.getattributevalue (0<span style="color: #000000;"><span style="color: #000000;">)); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(tag.equals ("mapping"<span style="color: #000000;">) <span style="color: #000000;">{tableset.add (xpp.getattributevalue (</span></span>0<span style="color: #000000;"><span style="color: #000000;">)); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">xmlpullparser.end_tag:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get information for the next node</span></span>Evttype =<span style="color: #000000;"><span style="color: #000000;">Xpp.next (); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception e) {logutil.e (e.tostring ()); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;">{List</span></span><String> tablelist =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Arraylist<string><span style="color: #000000;"><span style="color: #000000;">(); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(String Table:tableset) {tablelist.add (table); }</span></span></pre><p><p>The XML file we defined earlier is an XML file in the DOM format, so it is necessary to verify that the XML file is legitimate, or the parsing failure may Occur. The test method is to set Setnamespaceaware to True.</p></p><p><p>Now we have parsed the XML file and put the corresponding node information inside the Tablelist.</p></p><p><p>later, we will talk about how to build database information through these parsed Information.</p></p><p><p>Android configures the database with an XML file</p></p></span>

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.