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 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(); if(count==1){ com.alibaba.fastjson.JSONObject object = JSON.parseObject(result); JSONArray jsonArray = new JSONArray(); jsonArray.add(object); return jsonArray; } 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 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; } }