android xml產生和解析

來源:互聯網
上載者:User

標籤:android   blog   java   os   io   資料   for   art   

    public static void writeToXml(Map<String, Object> map,Writer writer) throws Exception, IllegalStateException, IOException{ 
        XmlSerializer serializer = Xml.newSerializer(); 
        serializer.setOutput(writer); 
     serializer.startDocument("UTF-8", true); 
     serializer.startTag(null, "condition");   
     for(Entry<String, Object> entry:map.entrySet()){
             serializer.startTag(null, entry.getKey()); 
             serializer.text(entry.getValue().toString()); 
             serializer.endTag(null,  entry.getKey()); 
     }
       serializer.startTag(null, "condition");  
    serializer.endDocument(); 
    writer.flush(); 
    writer.close(); 
  }  

 

 

public static List<Map<String, Object>> getNurseInfo(String in, String format)throws Exception {InputStream inputstream = XjxPublicUnit.StringTOInputStream(in, format);return getNurseInfotomap(inputstream);}public static List<NurseInfo> getNurseInfo(InputStream xml)throws Exception {List<NurseInfo> myNurseInfos = null;    NurseInfo myNurseInfo = null;XmlPullParser pullParser = Xml.newPullParser();pullParser.setInput(xml, "UTF-8"); // 為Pull解譯器設定要解析的XML資料int event = pullParser.getEventType();while (event != XmlPullParser.END_DOCUMENT) {switch (event) {case XmlPullParser.START_DOCUMENT:if (myNurseInfo == null) {myNurseInfo = new NurseInfo();}myNurseInfos = new ArrayList<NurseInfo>();//初始化集合   break;case XmlPullParser.START_TAG:if (myNurseInfo == null && "row".equals(pullParser.getName())) {myNurseInfo = new NurseInfo();}if ("jlid".equals(pullParser.getName())) {myNurseInfo.setJlid(pullParser.nextText());}if ("lsh".equals(pullParser.getName())) {myNurseInfo.setLsh(pullParser.nextText());}if ("rqsj".equals(pullParser.getName())) {myNurseInfo.setRqsj(pullParser.nextText());}if ("s".equals(pullParser.getName())) {myNurseInfo.setS(pullParser.nextText());}if ("f".equals(pullParser.getName())) {myNurseInfo.setF(pullParser.nextText());}if ("JLR".equals(pullParser.getName())) {myNurseInfo.setJlr(pullParser.nextText());}if ("bbreathingmachine".equals(pullParser.getName())) {myNurseInfo.setBbreathingmachine(pullParser.nextText());}if ("hx".equals(pullParser.getName())) {myNurseInfo.setHx(pullParser.nextText());}if ("sz".equals(pullParser.getName())) {myNurseInfo.setSz(pullParser.nextText());}if ("xybhd".equals(pullParser.getName())) {myNurseInfo.setXybhd(pullParser.nextText());}if ("tk_y".equals(pullParser.getName())) {myNurseInfo.setTk_y(pullParser.nextText());}if ("tk_z".equals(pullParser.getName())) {myNurseInfo.setTk_z(pullParser.nextText());}if ("dgfs_y".equals(pullParser.getName())) {myNurseInfo.setDgfs_y(pullParser.nextText());}if ("dgfs_z".equals(pullParser.getName())) {myNurseInfo.setDgfs_z(pullParser.nextText());}if ("rl_mc".equals(pullParser.getName())) {myNurseInfo.setRl_mc(pullParser.nextText());}if ("rllx".equals(pullParser.getName())) {myNurseInfo.setRllx(pullParser.nextText());}if ("rl_l".equals(pullParser.getName())) {myNurseInfo.setRl_l(pullParser.nextText());}if ("cl_mc".equals(pullParser.getName())) {myNurseInfo.setCl_mc(pullParser.nextText());}if ("rllx".equals(pullParser.getName())) {myNurseInfo.setCllx(pullParser.nextText());}if ("cl_l".equals(pullParser.getName())) {myNurseInfo.setCl_l(pullParser.nextText());}if ("rank".equals(pullParser.getName())) {myNurseInfo.setRank(pullParser.nextText());}break;case XmlPullParser.END_TAG:if ("row".equals(pullParser.getName())){myNurseInfos.add(myNurseInfo);//將book添加到books集合                myNurseInfo = null;    }}event = pullParser.next();}return myNurseInfos;}public static List<Map<String, Object>> getNurseInfotomap(InputStream inputStream) throws Exception {List<Map<String, Object>> myNurseInfos = null;Map<String, Object> myNurseInfo = null; XmlPullParser parser = Xml.newPullParser();    parser.setInput(inputStream, "UTF-8");    int event = parser.getEventType();//產生第一個事件  while(event!=XmlPullParser.END_DOCUMENT){   switch(event){    case XmlPullParser.START_DOCUMENT://判斷當前事件是否是文檔開始事件      myNurseInfos = new ArrayList<Map<String, Object>>();  break;      case XmlPullParser.START_TAG://判斷當前事件是否是標籤元素開始事件      if("row".equals(parser.getName())){//判斷開始標籤元素是否是book      myNurseInfo = new HashMap<String, Object>();                        }    if(myNurseInfo!=null){    if (!("row".equals(parser.getName()))){//判斷開始標籤元素是否是jlid      myNurseInfo.put(parser.getName(),parser.nextText());   }   }  break;   case XmlPullParser.END_TAG://判斷當前事件是否是標籤元素結束事件    if("row".equals(parser.getName())){//判斷結束標籤元素是否是book      myNurseInfos.add(myNurseInfo);//將book添加到books集合      myNurseInfo = null;      }      break;                }                     event = parser.next();//進入下一個元素並觸發相應事件          }//end while      return myNurseInfos;  }package com.xjx.unit;import java.io.ByteArrayInputStream;import java.io.InputStream;public class XjxPublicUnit { public static InputStream StringTOInputStream(String in,String strFormat) throws Exception{                  ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes(strFormat));          return is;      }  }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.