网站建设资讯

NEWS

网站建设资讯

android保存数据到xml以及pull解析模板

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;
    }
    /***
     * 读取unit.xml文件
     * @return 单位集合对象
     * @throws Exception
     */
    public List getUnitList() throws Exception{
        List unitDTOList = null;
        UnitDTO unitDTO = null;
        XmlPullParser pullParser = Xml.newPullParser();

        pullParser.setInput(mContext.openFileInput("unit.xml"), "UTF-8");
        int event = pullParser.getEventType();//触发第一个事件
        while(event !=XmlPullParser.END_DOCUMENT){
            switch(event){
                case XmlPullParser.START_DOCUMENT:
                    unitDTOList = new ArrayList();
                    break;
                case XmlPullParser.START_TAG:
                    if("doc".equals(pullParser.getName())){
                        unitDTO = new UnitDTO();
                    }
                    if("name".equals(pullParser.getName())){
                        unitDTO.setName(pullParser.nextText());
                    }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)
                    {
//                     添加对象到list中
                        unitDTOList.add(unitDTO);
                        unitDTO = null;
                    }
                    break;
            }
            event = pullParser.next();

        }
        return unitDTOList;
    }

//保存单位集合到unit.xml文件中
public void save(List values, String fileName){
    if(values==null||values.isEmpty()){
        return;
    }
    if(TextUtils.isEmpty(fileName)){
        fileName="unit.xml";
    }
    try {
        XmlSerializer serializer = Xml.newSerializer();//获取XML写入信息的序列化对象
        FileOutputStream outStream = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
        serializer.setOutput(outStream, "UTF-8");//设置要写入的OutputStream
        serializer.startDocument("UTF-8", true);//设置文档标签
        serializer.startTag(null, "docs");//设置开始标签,第一个参数为namespace
        for (UnitDTO doc : values) {
            serializer.startTag(null, "doc");

            serializer.startTag(null, "name");
            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");
            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();
    }
}
}

网页名称:android保存数据到xml以及pull解析模板
路径分享:http://cdweb.net/article/ggogps.html