您好,登錄后才能下訂單哦!
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<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();//觸發第一個事件 while(event !=XmlPullParser.END_DOCUMENT){ switch(event){ case XmlPullParser.START_DOCUMENT: 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()); }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<UnitDTO> 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(); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。