ResultHandle.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cn.com.taiji.dataService.utils;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.XML;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONArray;
  6. import javax.xml.soap.*;
  7. import java.io.ByteArrayInputStream;
  8. import java.util.Iterator;
  9. /**
  10. * @author chen mh
  11. * @date 2023/7/18 10:08
  12. */
  13. public class ResultHandle {
  14. public static JSONArray handle(String response){
  15. try {
  16. SOAPMessage msg = formatSoapString(response);
  17. SOAPBody body = msg.getSOAPBody();
  18. Iterator<SOAPElement> iterator = body.getChildElements();
  19. JSONObject jsonObject = PrintBody(iterator, null);
  20. System.out.println(jsonObject);
  21. DataJson t = JSON.parseObject(jsonObject.toString(), DataJson.class);
  22. int count = t.getRoot().getResults().getCount();
  23. if(count>0){
  24. String result = t.getRoot().getResults().getResult();
  25. if(count==1){
  26. com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
  27. JSONArray jsonArray = new JSONArray();
  28. jsonArray.add(object);
  29. return jsonArray;
  30. }
  31. JSONArray jsonArray = JSON.parseArray(result);
  32. return jsonArray;
  33. }
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. return null;
  38. }
  39. /**
  40. *
  41. * 把soap字符串格式化为SOAPMessage
  42. *
  43. * @param soapString
  44. * @return
  45. * @see [类、类#方法、类#成员]
  46. */
  47. public static SOAPMessage formatSoapString(String soapString) {
  48. MessageFactory msgFactory;
  49. try {
  50. msgFactory = MessageFactory.newInstance();
  51. SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
  52. new ByteArrayInputStream(soapString.getBytes("UTF-8")));
  53. reqMsg.saveChanges();
  54. return reqMsg;
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. return null;
  58. }
  59. }
  60. private static JSONObject PrintBody(Iterator<SOAPElement> iterator, String side) {
  61. JSONObject result = null;
  62. JSONObject xmlJSONObj = null;
  63. while (iterator.hasNext()) {
  64. Object o=iterator.next();
  65. if(o!=null) {
  66. SOAPElement element=null;
  67. try{
  68. element = (SOAPElement) o;
  69. // System.out.println("Node Name:" + element.getNodeName());
  70. // System.out.println("Value:" + element.getValue());
  71. if(element.getValue()!=null){
  72. xmlJSONObj = XML.toJSONObject(element.getValue());
  73. if (xmlJSONObj != null){
  74. return xmlJSONObj;
  75. }
  76. }
  77. }catch(Exception e){}
  78. if ( element !=null) {
  79. result = PrintBody(element.getChildElements(), side + "-----");
  80. }
  81. }
  82. }
  83. return result;
  84. }
  85. }