哪位大佬,手头有没有封装的比较好的 Java实现xml数据格式请求的方法

@Ta 04-11 15:15 307点击
rt
来自电脑端
回复列表(5)
  • 1
    @Ta / 04-13 13:25
    手拼,
  • 2
    @Ta / 04-13 15:21

    https://dom4j.github.io/

    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    
    public class Foo {
    
        public Document createDocument() {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("root");
    
            Element author1 = root.addElement("author")
                .addAttribute("name", "James")
                .addAttribute("location", "UK")
                .addText("James Strachan");
    
            Element author2 = root.addElement("author")
                .addAttribute("name", "Bob")
                .addAttribute("location", "US")
                .addText("Bob McWhirter");
    
            return document;
        }
    }
    
  • 3
    @Ta / 04-13 16:41
    Dom4j
  • 4
    @Ta / 04-15 17:12
    找我我有,  我封装好了 /..  qq找我吧478187626
  • 5
    @Ta / 04-16 08:55

    @爱穿我的鞋@老虎会游泳,哈哈谢谢老虎谢谢@爱穿我的鞋

    /**
         * xml格式post请求
         * @param url
         * @param xml String类型的xml格式数据
         * @return String类型xml格式数据
         */
        public static String HttpPostWithXml(String url, String xml) {
            String returnValue = "请求数据格式Xml,这是默认返回值,接口调用失败";
            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
    
            String xmlparmete=
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
                            "\t<SOAP-ENV:Body>\n" +
                            "\t\t<ns1:callService>\n"+
                            ParameterUtils.getFunctionOptStr(ParameterUtils.getCommonStr(xml))+
                            "\t\t\t<appToken>"+"xxx"+"</appToken>\n" +
                            "\t\t\t<appKey>"+ "xxxx"+"</appKey>\n" +
                            "\t\t\t<service>"+"xxxxxx"+"</service>\n" +
                            "\t\t</ns1:callService>\n" +
                            "\t</SOAP-ENV:Body>\n" +
                            "</SOAP-ENV:Envelope>";
            System.out.println(xmlparmete);
            try {
                //创建HttpClient对象
                 httpClient = HttpClients.createDefault();
                //创建HttpPost对象
                 HttpPost httpPost = new HttpPost(url);
                //设置setEntity
                if (xml != null && xml.trim().length() > 0) {
                    StringEntity stringEntity = new StringEntity(xmlparmete, "UTF-8");
                    stringEntity.setContentType("text/xml");
                    httpPost.setEntity(stringEntity);
                //发送HttpPost请求,获取返回值
                    response = httpClient.execute(httpPost);
                   HttpEntity entity =  response.getEntity();
                   returnValue =EntityUtils.toString(entity); 
                }else {
                    System.out.println("xml请求正文为空");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return returnValue;
        }
    

    来自电脑端

添加新回复
回复需要登录

[聊天-公共聊天室] MINE:http://fuligou.wang京东内部优惠卷:http://fuligou.wang!天猫专…