Java sends and parses Soap Xml data

Recently, a project encountered a customer Soap system that needed to be connected. Checking their C# source code found that the format is as follows, which is not the same as the Xml data used before, so I studied how to package it.

 request data

                String sendMsg="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                    "  <soap:Body>\n" +
                    "    <GetUserInfo xmlns=\"http://tempuri.org/\">\n" +
                    "      <code>103431</code>\n" +
                    "      <pwd>19</pwd>\n" +
                    "    </GetUserInfo>\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";

request method

    /**
     * 拼接请求体
     * @param table 表面
     * @param fields 参数键值对 name:key value:value
     * @return 拼接后xml
     */
    public static String requestWeb(String table, Map<String, String> fields) {
        String fieldsMsg = "";
        for (Map.Entry<String, String> entry : fields.entrySet()) {
            fieldsMsg += "<" + entry.getKey() + ">" + entry.getValue() + "</" + entry.getKey() + ">";
        }
        String sendMsg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <" + table + " xmlns=\"http://tempuri.org/\">\n" +
                fieldsMsg +
                "    </" + table + ">\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
        return sendMsg;
    }


            //表名
            String table="GetUserInfo";
            Map<String, String> fields = new HashMap<>();
            //参数
            fields.put("code", username);
            fields.put("pwd", password);
            String sendMsg = SoapUtils.requestWeb(table, fields);
//发送请求时需要加上.mediaType(MediaType.parse("text/xml; charset=utf-8"))

 Parse data using dom4j

There are two data formats, single data (such as obtaining user information), and list data (acquiring document information)

    /**
     * 解析SOAP返回数据
     * @param response 数据源
     * @param table 表名
     * @return Map;空数据返回null
     * 单数据形式
     * <XXResponse>
     *     <XXResult>
     *         <ID></ID>
     *         <NAME></NAME>
     *     </XXResult>
     * </XXResponse>
     */
    public static Map<String, String> responseWeb(String response, String table) {
        //创建Reader对象
        SAXReader reader = new SAXReader();
        //加载xml
        Document document = null;
        Map<String, String> request = new HashMap();
        try {
            //获取数据流
            InputStream is = new ByteArrayInputStream(response.getBytes());
            document = reader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        while (iterator.hasNext()) {
            request = new HashMap();
            Element stu = (Element) iterator.next();
            //判断节点是否为Result 如果不是则向下遍历
            while (!stu.getName().equals((table + "Result"))) {
                if (stu.elementIterator().hasNext()) {
                    stu = stu.elementIterator().next();
                }else {
                    return null;
                }
            }
//            List<Attribute> attributes = stu.attributes();
//            System.out.println("======获取属性值======");
//            for (Attribute attribute : attributes) {
//                System.out.println(attribute.getValue());
//            }
            Log.d(TAG, "responseWeb: ======遍历子节点======");
            Iterator iterator1 = stu.elementIterator();
            while (iterator1.hasNext()) {
                Element stuChild = (Element) iterator1.next();
                Log.d(TAG, "节点名:" + stuChild.getName() + "---节点值:" + stuChild.getStringValue());
                request.put(stuChild.getName(), stuChild.getStringValue());
            }
        }
        return request;
    }
    /**
     * 解析SOAP返回数据
     *
     * @param response 数据源
     * @param table    表名
     * @return List Map;空数据返回null
     * list数据形式
     * <XXResponse>
     *     <XXResult>
     *         <T_XX_XX>
     *             <ID></ID>
     *             <NAME></NAME>
     *         </T_XX_XX>
     *         <T_XX_XX>
     *             <ID></ID>
     *             <NAME></NAME>
     *         </T_XX_XX>
     *     </XXResult>
     * </XXResponse>
     */
    public static List<Map<String, String>> responseWebList(String response, String table) {
        //创建Reader对象
        SAXReader reader = new SAXReader();
        //加载xml
        Document document = null;
        List<Map<String, String>> requestList = new ArrayList<>();
        Map<String, String> request = new HashMap();
        try {
            //获取数据流
            InputStream is = new ByteArrayInputStream(response.getBytes());
            document = reader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            //判断节点是否为Result 如果不是则向下遍历
            while (!stu.getName().equals((table + "Result"))) {
                if (stu.elementIterator().hasNext()) {
                    stu = stu.elementIterator().next();
                } else {
                    return null;
                }
            }
//            List<Attribute> attributes = stu.attributes();
//            System.out.println("======获取属性值======");
//            for (Attribute attribute : attributes) {
//                System.out.println(attribute.getValue());
//            }
            Log.d(TAG, "responseWebList: ======遍历子节点======");
            Iterator iterator1 = stu.elementIterator();
            while (iterator1.hasNext()) {
                Element stuChild = (Element) iterator1.next();
                if (stuChild.elementIterator().hasNext()) {
                    Log.d(TAG, "responseWebList: ======遍历孙节点======");
                    Iterator iterator2 = stuChild.elementIterator();
                    request = new HashMap();
                    while (iterator2.hasNext()) {
                        Element stuSon = (Element) iterator2.next();
                        Log.d(TAG, "节点名:" + stuSon.getName() + "---节点值:" + stuSon.getStringValue());
                        request.put(stuSon.getName(), stuSon.getStringValue());
                    }
                }
                requestList.add(request);
            }
        }
        return requestList;
    }


 

Guess you like

Origin blog.csdn.net/qq_17798399/article/details/122058075