2018-06-19 19:56:54 +08:00

107 lines
2.4 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;
import lombok.Data;
@SuppressWarnings("rawtypes")
@Data
public class XmlPacket{
protected String RETCOD;
protected String ERRMSG;
protected String FUNNAM;
protected String LGNNAM;
protected String DATTYP;
protected Map data; //<String,Vector>
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;
}
}