package com.tenwa.sdk.utils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Map; import java.util.Properties; import java.util.Vector; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; @SuppressWarnings("rawtypes") public class XmlPacket{ protected String RETCOD; protected String ERRMSG; protected String FUNNAM; protected String LGNNAM; protected String DATTYP; protected Map data; // public String getRETCOD() { return RETCOD; } public void setRETCOD(String rETCOD) { RETCOD = rETCOD; } public String getERRMSG() { return ERRMSG; } public void setERRMSG(String eRRMSG) { ERRMSG = eRRMSG; } public String getFUNNAM() { return FUNNAM; } public void setFUNNAM(String fUNNAM) { FUNNAM = fUNNAM; } public String getLGNNAM() { return LGNNAM; } public void setLGNNAM(String lGNNAM) { LGNNAM = lGNNAM; } public String getDATTYP() { return DATTYP; } public void setDATTYP(String dATTYP) { DATTYP = dATTYP; } public Map getData() { return data; } public void setData(Map data) { this.data = data; } public XmlPacket(){ data = new Properties(); } public boolean isError(){ if(RETCOD.equals("0")){ return false; }else{ return true; } } @SuppressWarnings("unchecked") public void putProperty(String sSectionName, Map mpData){ if(data.containsKey(sSectionName)){ Vector vt = (Vector)data.get(sSectionName); vt.add(mpData); }else{ Vector vt = new Vector(); vt.add(mpData); data.put(sSectionName, vt); } } /** * 取得指定接口的数据记录 * @param sSectionName * @param index 索引,从0开始 * @return Map */ public Map getProperty(String sSectionName, int index){ if(data.containsKey(sSectionName)){ return (Map)((Vector)data.get(sSectionName)).get(index); }else{ return null; } } public Vector getProperty(String sSectionName){ if(data.containsKey(sSectionName)){ return ((Vector)data.get(sSectionName)); }else{ return null; } } /** * 取得制定接口数据记录数 * @param sSectionName * @return */ public int getSectionSize(String sSectionName){ if(data.containsKey(sSectionName)){ Vector sec = (Vector)data.get(sSectionName); return sec.size(); } return 0; } /** * 解析xml字符串,并转换为报文对象 * @param message */ public static XmlPacket valueOf(String message) { SAXParserFactory saxfac = SAXParserFactory.newInstance(); try { SAXParser saxparser = saxfac.newSAXParser(); ByteArrayInputStream is = new ByteArrayInputStream(message.getBytes("GBK")); XmlPacket xmlPkt= new XmlPacket(); saxparser.parse(is, new SaxHandler(xmlPkt)); is.close(); return xmlPkt; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }