12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package cn.com.taiji.dataService.utils;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.XML;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import javax.xml.soap.*;
- import java.io.ByteArrayInputStream;
- import java.util.Iterator;
- /**
- * @author chen mh
- * @date 2023/7/18 10:08
- */
- public class ResultHandle {
- public static JSONArray handle(String response){
- try {
- SOAPMessage msg = formatSoapString(response);
- SOAPBody body = msg.getSOAPBody();
- Iterator<SOAPElement> iterator = body.getChildElements();
- JSONObject jsonObject = PrintBody(iterator, null);
- System.out.println(jsonObject);
- DataJson t = JSON.parseObject(jsonObject.toString(), DataJson.class);
- int count = t.getRoot().getResults().getCount();
- if(count>0){
- String result = t.getRoot().getResults().getResult();
- JSONArray jsonArray = JSON.parseArray(result);
- return jsonArray;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- *
- * 把soap字符串格式化为SOAPMessage
- *
- * @param soapString
- * @return
- * @see [类、类#方法、类#成员]
- */
- public static SOAPMessage formatSoapString(String soapString) {
- MessageFactory msgFactory;
- try {
- msgFactory = MessageFactory.newInstance();
- SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
- new ByteArrayInputStream(soapString.getBytes("UTF-8")));
- reqMsg.saveChanges();
- return reqMsg;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- private static JSONObject PrintBody(Iterator<SOAPElement> iterator, String side) {
- JSONObject result = null;
- JSONObject xmlJSONObj = null;
- while (iterator.hasNext()) {
- Object o=iterator.next();
- if(o!=null) {
- SOAPElement element=null;
- try{
- element = (SOAPElement) o;
- // System.out.println("Node Name:" + element.getNodeName());
- // System.out.println("Value:" + element.getValue());
- if(element.getValue()!=null){
- xmlJSONObj = XML.toJSONObject(element.getValue());
- if (xmlJSONObj != null){
- return xmlJSONObj;
- }
- }
- }catch(Exception e){}
- if ( element !=null) {
- result = PrintBody(element.getChildElements(), side + "-----");
- }
- }
- }
- return result;
- }
- }
|