123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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();
- 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<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;
- }
- }
|