Repository navigation
Expand file tree
/
Copy pathWsUtils.java
More file actions
58 lines (51 loc) · 1.86 KB
/
Copy pathWsUtils.java
File metadata and controls
58 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.wcode.util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* @author Y-Aron
* @version 1.0.0
* @create 2019/10/24 17:20
*/
public class WsUtils {
public static String execute(String url, String xml) throws IOException {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "text/xml; charset=UTF-8");
HttpEntity body = new StringEntity(xml);
post.setEntity(body);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse resp = httpClient.execute(post);
return EntityUtils.toString(resp.getEntity(), "utf-8");
}
public static Element getRootElement(String xml) throws DocumentException {
Document document = DocumentHelper.parseText(xml);
return document.getRootElement();
}
public static List<Element> getElements(Element root, String name) {
List<Element> elements = new LinkedList<>();
getElements(root, name, elements);
return elements;
}
private static void getElements(Element el, String name, List<Element> elements){
if (name.equals(el.getName())) {
elements.add(el);
} else {
Iterator it = el.elementIterator();
while (it.hasNext()) {
getElements((Element) it.next(), name, elements);
}
}
}
}