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

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

    https://dom4j.github.io/

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

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

    1. /**
    2.    * xml格式post请求
    3.    * @param url
    4.     * @param xml String类型的xml格式数据
    5.    * @return String类型xml格式数据
    6.    */
    7.   public static String HttpPostWithXml(String url, String xml) {
    8.         String returnValue = "请求数据格式Xml,这是默认返回值,接口调用失败";
    9.      CloseableHttpClient httpClient = HttpClients.createDefault();
    10.       CloseableHttpResponse response = null;
    11.  
    12.       String xmlparmete=
    13.                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    14.                         "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
    15.                         "\t<SOAP-ENV:Body>\n" +
    16.                         "\t\t<ns1:callService>\n"+
    17.                         ParameterUtils.getFunctionOptStr(ParameterUtils.getCommonStr(xml))+
    18.                         "\t\t\t<appToken>"+"xxx"+"</appToken>\n" +
    19.                         "\t\t\t<appKey>"+ "xxxx"+"</appKey>\n" +
    20.                         "\t\t\t<service>"+"xxxxxx"+"</service>\n" +
    21.                         "\t\t</ns1:callService>\n" +
    22.                         "\t</SOAP-ENV:Body>\n" +
    23.                         "</SOAP-ENV:Envelope>";
    24.        System.out.println(xmlparmete);
    25.         try {
    26.            //创建HttpClient对象
    27.             httpClient = HttpClients.createDefault();
    28.          //创建HttpPost对象
    29.              HttpPost httpPost = new HttpPost(url);
    30.             //设置setEntity
    31.             if (xml != null && xml.trim().length() > 0) {
    32.                 StringEntity stringEntity = new StringEntity(xmlparmete, "UTF-8");
    33.                 stringEntity.setContentType("text/xml");
    34.                 httpPost.setEntity(stringEntity);
    35.             //发送HttpPost请求,获取返回值
    36.                 response = httpClient.execute(httpPost);
    37.                HttpEntity entity =  response.getEntity();
    38.                returnValue =EntityUtils.toString(entity); 
    39.             }else {
    40.                 System.out.println("xml请求正文为空");
    41.             }
    42.         } catch (Exception e) {
    43.             e.printStackTrace();
    44.         }
    45.         return returnValue;
    46.     }

    来自电脑端

添加新回复
回复需要登录

[聊天-公共聊天室] 记忆丶搜索中:@TabKey9,小心老虎律师函警告⚠