码迷,mamicode.com
首页 > 移动开发 > 详细

android xml产生和解析

时间:2014-07-31 12:45:26      阅读:295      评论:0      收藏:0      [点我收藏+]

标签: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;  
	    }  
}

  

android xml产生和解析,布布扣,bubuko.com

android xml产生和解析

标签:android   blog   java   os   io   数据   for   art   

原文地址:http://www.cnblogs.com/chjx1988/p/3880178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!