There are many ways to parse XML files on the Android platform, such as sax, Dom, JSON, and pull.
Let's talk about pull technology. Today we will talk about using the pull technology to create XML files.
The built-in pull parser of anroid is used to parse XML files. The running mode of the pull parser is similar to that of the SAX Parser. It provides similar ide events, such as the start element and end element, use parser. next () can enter the next element and trigger the corresponding event. The event is sent as a numerical code, so you can use a switch to process the event you are interested in. When parsing an element, call parser. nexttext () to obtain the value of a text node. Jar packages are concentrated in the Android system and can be used directly.
Use the pull parser to generate xml files. Sometimes, we need to generate an XML file. There are many ways to generate an XML file. For example, we can use only one stringbuffer to form XML content, then write the content into the file, but this method looks too shanty, or use dom api to generate an XML file, or the pull parser to generate an XML file. We recommend that you use the pull parser.
XML pull Parsing is an open source project, see: http://www.xmlpull.org/
On the Android platform, if you want to save some data, you can use an XML file. Today, the view State is saved. The XML file to be generated is as follows:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><state><view name="button"><text>send</text><id>10</id></view><view name="textview"><text>this is a demo!</text><id>11</id></view></state>
First, construct a viewstate class and define some attributes. The Code is as follows:
package mark.zhang;public class ViewState {private String name;private int id;private String text;public ViewState() {// to do}public ViewState(String name, String text, int id) {this.name = name;this.id = id;this.text = text;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setId(int id) {this.id = id;}public int getId() {return id;}public void setText(String text) {this.text = text;}public String getText() {return text;}}
In addition, to build a business class that generates XML files, the pullxmlutils code is as follows:
Package mark. zhang; import Java. io. outputstream; import Java. io. writer; import Java. util. list; import Org. xmlpull. v1.xmlserializer; import android. util. XML; public class pullxmlutils {/*** create an XML file ** @ Param items * Store View status * @ Param outstream * output * @ throws exception */public static void createxml (list <viewstate> items, outputstream outstream) throws exception {xmlserializer serializer = xml. newserializ Er (); // set the output and encoding serializer. setoutput (outstream, "UTF-8"); // The build document is similar to: <? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> Serializer. startdocument ("UTF-8", true); // start tagserializer. starttag (null, "State"); For (viewstate vs: Items) {// enhance the for loop serializer. starttag (null, "View"); serializer. attribute (null, "name",. getname (); serializer. starttag (null, "text"); serializer. text (. gettext (); serializer. endtag (null, "text"); serializer. starttag (null, "ID"); serializer. text (string. valueof (. GETID (); serializer. endtag (null, "ID"); serializer. endtag (null, "View");} // end tagserializer. endtag (null, "State"); // end document serializer. enddocument (); // close the stream outstream. flush (); outstream. close ();}/*** create an XML file, overload method ** @ Param items * stores view status * @ Param writer * streams stream * @ throws exception */public static void createxml (list <viewstate> items, writer) throws exception {xmlserializer serializer = xml. newserializer (); serializer. setoutput (writer); serializer. startdocument ("UTF-8", true); serializer. starttag (null, "State"); For (viewstate vs: Items) {serializer. starttag (null, "View"); serializer. attribute (null, "name",. getname (); serializer. starttag (null, "text"); serializer. text (. gettext (); serializer. endtag (null, "text"); serializer. starttag (null, "ID"); serializer. text (string. valueof (. GETID (); serializer. endtag (null, "ID"); serializer. endtag (null, "View");} serializer. endtag (null, "State"); serializer. enddocument (); // close the stream writer. flush (); writer. close ();}}
After most of them are finished, let's look at the activity code:
Package mark. zhang; import Java. io. file; import Java. io. filewriter; import Java. util. arraylist; import mark. zhag. r; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button;/*** @ author Mark **/public class masteractivity extends activity {private button btn_createxml = NULL; @ override public void oncreate (bundl E savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); findviews (); setevens ();}/*** set listener **/private void setevens () {btn_createxml.setonclicklistener (New listenereven ());} /*** create control object */private void findviews () {btn_createxml = (button) findviewbyid (R. id. btn_createxml);}/*** create an XML file * @ throws exception */Public void createxml () throws exception {// create a file storage path: Data/data/APP/files/uistate. xmlfile file = new file (getfilesdir (), "uistate. XML "); arraylist <viewstate> vstate = new arraylist <viewstate> (); // Add data vstate. add (New viewstate ("button", "send", 10); vstate. add (New viewstate ("textview", "This is a demo! ", 11); // method 1/* fileoutputstream outstream = new fileoutputstream (File); pullxmlutils. createxml (vstate, outstream); * // method 2 filewriter writer = new filewriter (File); pullxmlutils. createxml (vstate, writer); // method 3: Can be used to display/* stringwriter swriter = new stringwriter (); log. D ("mark", swriter. tostring (); */} final class listenereven implements onclicklistener {@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. btn_createxml: Try {createxml ();} catch (exception e) {e. printstacktrace () ;}break; default: Break ;}}}}
Note: getfiledir () is the contextwrapper class method, which is the indirect parent class of activity. Getfiledir () can be used to obtain/data/APP package name/files.
Okay. Check the/data/APP package name/Files directory. Is it like this:
Open it and check that it should be the same as expected (you need to rearrange the file and use the source/format of Eclipse !).
If you are a researcher and want to continue to get started with the XML parsing technology, you have two options:
Look: http://hi.baidu.com/chenjinbo1983/blog/item/5160b0f167478bb7a40f52b7.html
Etc.: My next blog ,,,