Android generation and Pull parsing xml
1. How can I generate the following xml for a single object? <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Account> <id> 1 </id> <password> 123456 </password> <name> legend </name> <createDate> 11:50:42 </createDate> </ account> first defines an account class, attributes include id, name, password, and createDate. Public class Account {private String id; private String password; private String name; private String createDate; public Account () {super ();} public Account (String id, String password, string name, String createDate) {super (); this. id = id; this. password = password; this. name = name; this. createDate = createDate;} public String getId () {return id;} public void setId (String id) {this. id = I D;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getCreateDate () {return createDate;} public void setCreateDate (String createDate) {this. createDate = createDate;} @ Override public String toString () {return "Ac Count [id = "+ id +", password = "+ password +", name = "+ name + ", createDate = "+ createDate +"] \ n ";}} defines this class, And XmlSerializer can be used to write xml data. Write a method to save the generated xml in the xmlparser_account.xml file. /*** Generate xml for a single object * @ param account */private static void XmlFileCreator (Account account) {File newxmlfile = new File (Environment. getExternalStorageDirectory () + "/xmlparser_account.xml"); try {if (! Newxmlfile. exists () newxmlfile. createNewFile ();} catch (IOException e) {Log. e ("IOException", "exception in createNewFile () method");} FileOutputStream fileos = null; try {fileos = new FileOutputStream (newxmlfile);} catch (FileNotFoundException e) {Log. e ("FileNotFoundException", "can't create FileOutputStream");} // XmlSerializer is used to write xml data XmlSerializer serializer = Xml. newSerializer (); try {// XmlSerializer uses UTF-8 encoding serializer. setOutput (fileos, "UTF-8"); serializer. startDocument (null, Boolean. valueOf (true); serializer. setFeature ("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer. startTag (null, "account"); // xml-tree, starting from startTag, endTag ends serializer. startTag (null, "id"); serializer. text (account. getId (); serializer. endTag (null, "id"); serializer. startTag (nul L, "password"); serializer. text (account. getPassword (); serializer. endTag (null, "password"); serializer. startTag (null, "name"); serializer. text (account. getName (); serializer. endTag (null, "name"); serializer. startTag (null, "createDate"); serializer. text (account. getCreateDate (); serializer. endTag (null, "createDate"); serializer. endTag (null, "account"); serializer. endDocument (); // write xml data to FileOutpu TStream serializer. flush (); // close fileos and release the resource fileos. close ();} catch (Exception e) {Log. e ("Exception", "error occurred while creating xml file") ;}} generates an account object. A single object generates an xml SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss "); Account account = new Account (" 1 "," 123456 "," Legend of the beautiful ", sdf. format (new Date (); XmlFileCreator (account); view the saved file II. parse the xml consisting of a single object to put the generated xmlparser_account.xml file in res/ In xml/, parse this xml into an Account object. XmlResourceParser and XmlResourceParser inherit the xmlpullparse class. Similar to the sax parsing, the Pull parser uses event-driven parsing. When the pull parser starts parsing, it calls its next () method, obtain the next parsing event (including four parsing events: Start document, end document, start tag, and end tag). Here we just talk about Pull parsing. /*** Parse the xml and xml groups composed of a single object * @ return */private List <Account> getListData () {List <Account> accountList = new ArrayList <Account> (); XmlResourceParser xrp = getResources (). getXml (R. xml. xmlparser_account); try {// until the end of the document Account account = null; while (xrp. getEventType ()! = XmlResourceParser. END_DOCUMENT) {String tagName = xrp. getName (); if (xrp. getEventType () = XmlResourceParser. START_DOCUMENT) {}// if the start label if (xrp. getEventType () = XmlResourceParser. START_TAG) {Log. I ("", tagName); if (tagName. equals ("account") {account = new Account ();} else if (account! = Null) {if (tagName. equals ("id") {String id = xrp. nextText (); // get the attribute value account through the attribute name. setId (id);} else if (tagName. equals ("password") {String password = xrp. nextText (); // obtain the attribute value account through the attribute index. setPassword (password);} else if (tagName. equals ("name") {String name = xrp. nextText (); account. setName (name);} else if (tagName. equals ("createDate") {String createDate = xrp. nextText (); account. setC ReateDate (createDate) ;}} if (xrp. getEventType () == XmlResourceParser. END_TAG) {if (tagName. equals ("account") & account! = Null) {accountList. add (account); account = null ;}} xrp. next (); // get the next event for parsing} catch (XmlPullParserException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return accountList;} print the result and check 1Log. I ("", getListData (). toString (); log as follows: 3. the xml group composed of a single object is similar to this <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Accounts> <account> <id> 2 </id> <password> 123456 </password> <name> legend </name> <createDate> 02:54:41 </createDate> </account> <id> 3 </id> <password> 567890 </password> <name> beauty </name> <createDate> 02:54:41 </createDate> </account> </accounts> Generate an xml group composed of a single object group. xml is similar to that of a single object, write a method to save the generated xml in the xmlparser_accounts.xml file. /*** Generate an xml array of a single object ** @ param data */private static void XmlFileCreator (List <Account> data) {File newxmlfile = new File (Environment. getExternalStorageDirectory () + "/xmlparser_accounts.xml"); try {if (! Newxmlfile. exists () newxmlfile. createNewFile ();} catch (IOException e) {Log. e ("IOException", "exception in createNewFile () method");} FileOutputStream fileos = null; try {fileos = new FileOutputStream (newxmlfile);} catch (FileNotFoundException e) {Log. e ("FileNotFoundException", "can't create FileOutputStream");} XmlSerializer serializer = Xml. newSerializer (); try {serializer. setOutput (file OS, "UTF-8"); serializer. startDocument (null, Boolean. valueOf (true); serializer. setFeature ("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer. startTag (null, "accounts"); for (Account account: data) {serializer. startTag (null, "account"); serializer. startTag (null, "id"); serializer. text (account. getId (); serializer. endTag (null, "id"); serializer. startTag (null, "password "); Serializer. text (account. getPassword (); serializer. endTag (null, "password"); serializer. startTag (null, "name"); serializer. text (account. getName (); serializer. endTag (null, "name"); serializer. startTag (null, "createDate"); serializer. text (account. getCreateDate (); serializer. endTag (null, "createDate"); serializer. endTag (null, "account");} serializer. endTag (null, "accounts"); serializer. endDocument (); Serializer. flush (); fileos. close ();} catch (Exception e) {Log. e ("Exception", "error occurred while creating xml file");} generate Account account1 = new Account ("2", "123456 ", "legend", sdf. format (new Date (); Account account2 = new Account ("3", "567890", "beauty", sdf. format (new Date (); List <Account> accountList = new ArrayList <Account> (); accountList. add (account1); accountList. add (account2); XmlF IleCreator (accountList); the generated file is as follows: 4. parse the xml group composed of a single object. 2. parse the xml group composed of a single object as a single object, please refer to section 5. generate an xml group consisting of a single object with attribute. The account also contains how to generate an attribute value, which is actually very simple, modify the xml group composed of a single object. <? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Accounts> <account id = "2"> <password> 123456 </password> <name> legend </name> <createDate> 04:50:45 </createDate> </account> <account id = "3"> <password> 567890 </password> <name> beauty </name> <createDate> 04:50:45 </createDate> </account> </ accounts> change to for (Account account Account: data) {serializer. startTag (null, "account"); serializer. attribute (null, "id", account. getId (); // serializer. startT Ag (null, "id"); // serializer. text (account. getId (); // serializer. endTag (null, "id"); serializer. startTag (null, "password"); serializer. text (account. getPassword (); serializer. endTag (null, "password"); serializer. startTag (null, "name"); serializer. text (account. getName (); serializer. endTag (null, "name"); serializer. startTag (null, "createDate"); serializer. text (account. getCreateDate (); serializer. e NdTag (null, "createDate"); serializer. endTag (null, "account");} 6. parsing an xml group composed of a single object with attribute is similar to parsing an xml group composed of a single object, modify the id part for parsing. // if the start label if (xrp. getEventType () = XmlResourceParser. START_TAG) {Log. I ("", tagName); if (tagName. equals ("account") {account = new Account (); String id = xrp. getAttributeValue (null, "id"); account. setId (id);} else if (account! = Null) {if (tagName. equals ("id") {// String id = xrp. nextText (); // account. setId (id);} else if (tagName. equals ("password") {String password = xrp. nextText (); account. setPassword (password);} else if (tagName. equals ("name") {String name = xrp. nextText (); account. setName (name);} else if (tagName. equals ("createDate") {String createDate = xrp. nextText (); account. setCreateDate (createDate );}}}