ResultHandle.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. JSONArray jsonArray = JSON.parseArray(result);
  26. return jsonArray;
  27. }
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. return null;
  32. }
  33. /**
  34. *
  35. * 把soap字符串格式化为SOAPMessage
  36. *
  37. * @param soapString
  38. * @return
  39. * @see [类、类#方法、类#成员]
  40. */
  41. public static SOAPMessage formatSoapString(String soapString) {
  42. MessageFactory msgFactory;
  43. try {
  44. msgFactory = MessageFactory.newInstance();
  45. SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
  46. new ByteArrayInputStream(soapString.getBytes("UTF-8")));
  47. reqMsg.saveChanges();
  48. return reqMsg;
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. return null;
  52. }
  53. }
  54. private static JSONObject PrintBody(Iterator<SOAPElement> iterator, String side) {
  55. JSONObject result = null;
  56. JSONObject xmlJSONObj = null;
  57. while (iterator.hasNext()) {
  58. Object o=iterator.next();
  59. if(o!=null) {
  60. SOAPElement element=null;
  61. try{
  62. element = (SOAPElement) o;
  63. // System.out.println("Node Name:" + element.getNodeName());
  64. // System.out.println("Value:" + element.getValue());
  65. if(element.getValue()!=null){
  66. xmlJSONObj = XML.toJSONObject(element.getValue());
  67. if (xmlJSONObj != null){
  68. return xmlJSONObj;
  69. }
  70. }
  71. }catch(Exception e){}
  72. if ( element !=null) {
  73. result = PrintBody(element.getChildElements(), side + "-----");
  74. }
  75. }
  76. }
  77. return result;
  78. }
  79. }