需要解析报文格式:
<?xml version="1.0" encoding="GBK"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Header>
<out:system xmlns:out="http://www.ylzinfo.com/">
</out:system>
</soap:Header>
<soap:Body>
<out:business xmlns:out="http://www.ylzinfo.com/">
<result showtype="1"/>
<resultset name="structs">
<row label="操作验证串有效标志" columnname="busSeqFlag" typename="Varchar2" displaysize="20"/>
</resultset>
<resultset name="retrieve">
<row busSeqFlag="0" />
</resultset>
</out:business>
</soap:Body>
</soap:Envelope>
具体的解析方法:
public static HashMap parseData_object(String xmlStr) {
xmlStr=xmlStr.replaceAll("\\<=", "<=").replaceAll("\\>=", ">=").replaceAll("rownum\\<", "rownum<").replaceAll("rownum\\>", "rownum>");
HashMap retmap = new HashMap();
Vector retcolname = new Vector();
Vector retcollable = new Vector();
Vector retcolrow = new Vector();
Vector retcolvalue = null;
String lxmation ="";
// StringBuffer result = new StringBuffer();
String v_col_value = null; // 字段值
retmap.put("msg", "ok");// 赋初始值
// 解析xml文档
try {
Document doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
List<Element> rows = root.elements();
List<Element> bodyData = rows.get(1).elements();
for (Element bodyChild : bodyData) {
List<Element> structsList = new ArrayList<Element>();
List<Element> retrieveList = new ArrayList<Element>();
List<String> informationList = new ArrayList<String>();
List<String> errorMsgList = new ArrayList<String>();
String totalnum = null;
List<Element> business = bodyChild.elements();
for (Element e : business) {
if ("structs".equals(e.attributeValue("name"))) {
structsList.add(e);
} else if ("retrieve".equals(e.attributeValue("name"))) {
retrieveList.add(e);
}
// 总条数
if (e.attribute("totalnum") != null) {
totalnum = e.attribute("totalnum").getValue();
}
// 提示信息
if (e.attribute("information") != null) {
informationList.add(e.attribute("information")
.getValue());
}
if ("faultcode".equals(e.getName())) {
lxmation=e.getText();
}
if ("2".equals(e.attributeValue("resultcode"))) {
lxmation=e.attributeValue("resultcode");
}
if (e.attribute("resultmsg") != null) {
if (!"".equals(e.attributeValue("resultmsg"))) {
errorMsgList
.add(e.attributeValue("resultmsg") == null ? null
: e.attributeValue("resultmsg"));
}
}
// 出错信息
if ("faultstring".equals(e.getName())) {
List<Element> errorList = e.elements();
for (Element errorElement : errorList) {
errorMsgList
.add(errorElement.attribute("msg") == null ? null
: errorElement.attribute("msg")
.getValue());
}
}
}
if (informationList.size() == 0 && errorMsgList.size() == 0) {
String[] v_columnLable = new String[100];// 列名 与lable对应
String[] v_columnType = new String[100];// 列名 与typename对应
String[] v_columnName = new String[100];// 列名 与columnname对应
int i = 0;
//
int k = 0;
for (Element structs : structsList) {
for (Iterator<?> struct = structs.elementIterator(); struct
.hasNext(); i++) {
Element e = (Element) struct.next();
Attribute attributeLable = e.attribute("label");
Attribute attributeName = e.attribute("columnname");
Attribute attributeType = e.attribute("typename");
v_columnName[i] = attributeName.getValue();// 列名
v_columnType[i] = attributeType.getValue();// 类型
if (attributeLable == null) {
v_columnLable[i] = null;
retcollable.add(i, null);
} else {
v_columnLable[i] = attributeLable.getValue();// 标签
retcollable.add(i, v_columnLable[i]);
}
}
for (; v_columnName[k] != null; k++) {
if (v_columnLable[k] != null
|| !v_columnLable[k].equals("")) // 中文标题
{
retcolname.add(k, v_columnName[k]);
} else {
retcolname.add(k, v_columnLable[k]);
}
}
}
for (Element retrieve : retrieveList) {
// 解析每个字段名
int p = 0;
for (Iterator<?> retrieveRow = retrieve
.elementIterator(); retrieveRow.hasNext(); p++) // 记录数--行数
{
Element roweValueElement = (Element) retrieveRow
.next();// soap 结构体 retrieve
HashMap mapvalue = new HashMap();
for (int clildSize = 0; clildSize < roweValueElement
.attributeCount(); clildSize++)// 列数
{
try {
v_col_value = roweValueElement.attribute(
v_columnName[clildSize]).getValue();
} catch (NullPointerException e) {
v_col_value = "";
}
if (v_columnType[clildSize]
.equalsIgnoreCase("blob")) {
String num = null;
if (!v_col_value.equals("null")) {
v_col_value = v_col_value.replaceAll(
" ", "");
}
}
mapvalue.put(v_columnName[clildSize],
v_col_value);
}
retcolrow.add(mapvalue);
mapvalue = null;
}
}
}
// 查询结果出错 或提示信息
else {
for (String information : informationList) {
retmap.put("msg", information);
}
for (String errorMsg : errorMsgList) {
retmap.put("msg", errorMsg);
}
}
}
} catch (Exception e) {
e.printStackTrace();
Writer w = new StringWriter();
e.printStackTrace(new PrintWriter(w));
String msgg=e.toString();
msgg=msgg.replaceAll("\\<", " ").replaceAll("\\>", " ").replaceAll("\\?", " ").replaceAll("\\=", " ");
retmap.put("msg", msgg);
}
retmap.put("retcolrow", retcolrow);// 返回列值vector ,第一个Vector
// 为一个HashMap,以列名标识各列名和值
retmap.put("retcollable", retcollable);// 返回列标签vector
retmap.put("retcolname", retcolname);// 返回列名vector
retmap.put("lxmation", lxmation);//
return retmap;
}