Android saves data to XML and pull parsing templates

Source: Internet
Author: User

Import android.content.context;import android.text.textutils;import android.util.xml;import  com.webjet.yzqg.domain.BaseDocs;import com.webjet.yzqg.domain.UnitDTO;import  org.xmlpull.v1.xmlpullparser;import org.xmlpull.v1.xmlserializer;import java.io.fileoutputstream; import java.io.inputstream;import java.util.arraylist;import java.util.list;/** *  Created by administrator on 2016/7/20 0020. */public class unitservice  {    private Context mContext;    public  Unitservice (Context mcontext) {        this.mcontext =  mcontext;    }    /***     *  Read Unit.xml file      *  @return   Unit Collection Object      * @ Throws exception     */ &nBsp;  public list<unitdto> getunitlist ()  throws Exception{         List<UnitDTO> unitDTOList = null;         UnitDTO unitDTO = null;         xmlpullparser pullparser = xml.newpullparser ();         pullparser.setinput (Mcontext.openfileinput ("Unit.xml"),  "UTF-8");         int event = pullparser.geteventtype ();//Trigger First event          while (event !=xmlpullparser.end_document) {             switch (Event) {                 case XmlPullParser.START_DOCUMENT:           &nbsP;         unitdtolist = new arraylist<unitdto > ();                     break;                 case XmlPullParser.START_TAG:                     if ("Doc". Equals (Pullparser.getname ())) {                          unitdto = new unitdto ();                     }                     if ("name"). Equals (Pullparser.getname ())) {                         unitdto.setname (Pullparser.nexttext ());       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}IF ("id". Equals ( Pullparser.getname ())) {                         unitdto.setid (New integer (pullParser.nextText ()));                     }if ("ParentID". Equals (Pullparser.getname ())) {                          Unitdto.setparentid (New integer (Pullparser.nexttext ()));                     }                     if ("Unitlevel". Equals (Pullparser.getname ())) {                      Unitdto.setunitlevel (New integer (Pullparser.nexttext ()));                 }   if ("Location". Equals (Pullparser.getname ())) {                         unitdto.setlocation (Pullparser.nexttext ());                     }                     break;                 case xmlpullparser.end_tag:                      String value = pullparser.getname ();                     if (Value.equals ("Doc") &&unitDTO!= Null&&unitdtolist!=null)                      {//                       Add objects to list                           unitdtolist.add (unitdto);                         unitdto = null;                     }                     break;             }             event = pullparser.next ();        }         return unitdtolist;    }// Save Unit collection to Unit.xml file Public void save (list<unitdto> values, string filename) {     if (values==null| | Values.isempty ()) {        return;    }     if (Textutils.isempty (fileName)) {        filename= "Unit.xml ";    }    try {         Xmlserializer serializer = xml.newserializer ();//Gets the Xml write information of the serialized object          fileoutputstream outstream = mcontext.openfileoutput (fileName,  Context.mode_private);         serializer.setoutput (outStream,  " UTF-8 ");//Set the Outputstream        serializer.startdocument to be written (" UTF-8 ",  true);//Set document label         serializer.starttag (null,  "docs");// Set the start tag, the first parameter is namespace        for  (unitdto doc :  values)  {            serializer.starttag (null ,  "Doc");             serializer.starttag (null ,  "name");   &Nbsp;         serializer.text (Doc.getname ());             serializer.endtag (null,  "name");             serializer.starttag (null,  "id");             serializer.text (Doc.getid ()  +  "");             serializer.endtag (null,  "id");             serializer.starttag (null,  "parentId");             serializer.text (Doc.getParentID ()  +   "");             serializer.endtag (NULL,   "ParentID");             serializer.starttag ( null,  "Unitlevel"); &nbSp;           serializer.text (Doc.getUnitLevel ()  +   "");             serializer.endtag (NULL,   "Unitlevel");             serializer.starttag (null,  "Location");             Serializer.text (Doc.getlocation ()  +  "");             serializer.endtag (null,  "location");             serializer.endtag (null,  "Doc");        }         serializer.endtag (null,  "docs");         serializer.enddocument ();         outstream.flush ();          outstream.close ();    }catch  (exception e) {         e.printstacktrace ();     }}}


This article is from the "Five Piglets" blog, please be sure to keep this source http://wuxiaozhu.blog.51cto.com/7942143/1828671

Android saves data to XML and pull parsing templates

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.