You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.7 KiB
Java
120 lines
3.7 KiB
Java
package com.gszc.util;
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.gszc.constant.RequestXml;
|
|
import org.dom4j.Document;
|
|
import org.dom4j.DocumentException;
|
|
import org.dom4j.DocumentHelper;
|
|
import org.dom4j.Element;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.List;
|
|
|
|
@Component
|
|
public class XmlUtils {
|
|
|
|
/**
|
|
* 解析esb返回结果
|
|
*
|
|
* @param xml
|
|
* @return
|
|
*/
|
|
public static JSONArray getResult(String xml) {
|
|
Document doc = null;
|
|
JSONArray jsonArray = new JSONArray();
|
|
try {
|
|
doc = DocumentHelper.parseText(xml);
|
|
Element rootElt = doc.getRootElement();
|
|
String textTrim = rootElt.element("Body").element("HIPMessageServerResponse").element("return").getTextTrim();
|
|
Document document = DocumentHelper.parseText(textTrim);
|
|
String errcode = document.getRootElement().element("errcode").getTextTrim();
|
|
if("0".equals(errcode)){
|
|
throw new RuntimeException(document.getRootElement().elementText("errmsg"));
|
|
}
|
|
List<Element> rowElements = document.getRootElement().element("group").element("HRC00.02").elements("row");
|
|
for (Element element : rowElements) {
|
|
List<Element> rowItem = element.elements();
|
|
JSONObject jsonObject = new JSONObject();
|
|
for (Element row : rowItem) {
|
|
jsonObject.put(row.getName(), row.getTextTrim());
|
|
}
|
|
jsonArray.add(jsonObject);
|
|
}
|
|
} catch (DocumentException e) {
|
|
throw new RuntimeException("解析esb返回结果", e);
|
|
}
|
|
return jsonArray;
|
|
}
|
|
|
|
/**
|
|
* 是否成功
|
|
*
|
|
* @param xml
|
|
* @return
|
|
*/
|
|
public static boolean success(String xml) {
|
|
Document doc = null;
|
|
try {
|
|
doc = DocumentHelper.parseText(xml);
|
|
Element rootElt = doc.getRootElement();
|
|
String errcode = rootElt.element("errcode").getTextTrim();
|
|
if ("1".equals(errcode)) {
|
|
return true;
|
|
}
|
|
} catch (DocumentException e) {
|
|
throw new RuntimeException("是否成功", e);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 失败信息
|
|
*
|
|
* @param xml
|
|
* @return
|
|
*/
|
|
public static String errMsg(String xml) {
|
|
Document doc = null;
|
|
try {
|
|
doc = DocumentHelper.parseText(xml);
|
|
Element rootElt = doc.getRootElement();
|
|
String errmsg = rootElt.element("errmsg").getTextTrim();
|
|
return errmsg;
|
|
} catch (DocumentException e) {
|
|
throw new RuntimeException("失败信息", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 构造请求xml
|
|
*
|
|
* @param jsonObject
|
|
* @param funcId
|
|
* @return
|
|
*/
|
|
public static String buildSendXml(JSONObject jsonObject, String funcId, String appKey) {
|
|
Document doc = null;
|
|
try {
|
|
doc = DocumentHelper.parseText(RequestXml.XML);
|
|
Element rootElt = doc.getRootElement();
|
|
|
|
//设置服务编码
|
|
Element funcid = rootElt.element("funcid");
|
|
funcid.setText(funcId);
|
|
|
|
//设置请求参数
|
|
Element element = rootElt.element("group").element("HRC00.02").element("row");
|
|
jsonObject.forEach((k, v) -> element.addElement(k).setText(v.toString()));
|
|
|
|
//设置appkey
|
|
Element appKeyElt = rootElt.element("group").element("head").element("row").element("appKey");
|
|
appKeyElt.setText(appKey);
|
|
|
|
return doc.asXML();
|
|
} catch (DocumentException e) {
|
|
throw new RuntimeException("构造请求xml", e);
|
|
}
|
|
}
|
|
}
|