首页 > 代码库 > android xml产生和解析
android xml产生和解析
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。