2018-06-20 15:01:59 +08:00

154 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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; //<String,Vector>
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<String,String>
*/
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;
}
}