第三方接口请求XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.org/Ec/">
<SOAP-ENV:Body>
<ns1:callService>
<paramsJson>{json}</paramsJson>
<appToken>appToken</appToken>
<appKey>appKey</appKey>
<service>{service}</service>
</ns1:callService>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
封装请求参数
根据第三方接口提示进行拼接xml参数。
/**
* 获取请求的json的参数
*
* @param shipmentTmsDTO
* @return String
*/
private String getJsonShipmentTmsDTO(ShipmentTmsDTO shipmentTmsDTO) {
String jsonShipmentTmsDTO = JsonTools.objectToJson(shipmentTmsDTO);
String jsonShipmentTmsDtoParam = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
"<SOAP-ENV:Body>" +
"<ns1:callService>" +
"<paramsJson>" +
jsonShipmentTmsDTO +
"</paramsJson>" +
"<appToken>******</appToken>" +
"<appKey>******</appKey>" +
"<service>createOrder</service> " +
"</ns1:callService>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
return jsonShipmentTmsDtoParam;
}
发送请求
String shipmentTmsDtoString = getJsonShipmentTmsDTO(shipmentTmsDTO);
String returnMsg = HttpsUtils.sendMsgXml(PUSH_SHIPMENT_TO_TMS_URL, shipmentTmsDtoString);
PUSH_SHIPMENT_TO_TMS_URL 为推送的第三方接口URL
第三方接口响应XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.org/Ec/">
<SOAP-ENV:Body>
<ns1:callServiceResponse>
<response>{json}</response>
</ns1:callServiceResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
解析第三方接口返回的XML数据
通过节点获取XML中的json数据
// String ————》XML
Document doc = XmlUtil.StringTOXml(returnMsg);
String nodePath = "/Envelope/Body/callServiceResponse/response";
// getNodeValue
String nodeValue = XmlUtil.getNodeValue(doc, nodePath);
工具类(sendMsgXml,getNodeValue)
/**
*
* @param urlStr 接口地址
* @param xmlInfo xml格式参数数据
* @return
*/
public static String sendMsgXml(String urlStr, String xmlInfo) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "utf-8");
out.write(xmlInfo);
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
buffer.append(line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer+"";
}
/**
*
* @param document
* @return 某个节点的值 前提是需要知道xml格式,知道需要取的节点相对根节点所在位置
*/
public static String getNodeValue(Document document, String nodePaht) {
XPathFactory xpfactory = XPathFactory.newInstance();
XPath path = xpfactory.newXPath();
String servInitrBrch = "";
try {
servInitrBrch = path.evaluate(nodePaht, document);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return servInitrBrch;
}