呼叫介面,解析返回的的string型別xml文件
阿新 • • 發佈:2019-02-15
package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Test {
private static final Logger log = Logger.getLogger(Test.class);
public static Map<String, String> queryFromWebserviceByTel(String tel){
String body = "";
Map<String, String> map= new HashMap<String, String>();
String url ="220.189.240.226:8080/OA/Aspx/xla_kd_list.asmx";
String requestXml= "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"+
"<soap12:Body>"+
"<zhjt_list xmlns=\"http://tempuri.org/\">"+
"<account>"+tel+"</account>"+
"</zhjt_list>"+
"</soap12:Body>"+
"</soap12:Envelope>";
//建立httpclient物件
CloseableHttpClient client = HttpClients.createDefault();
//建立post方式請求物件
HttpPost httpPost = new HttpPost("http://220.189.240.226:8080/OA/Aspx/xla_kd_list.asmx");
HttpEntity requestEntity = new StringEntity(requestXml, "UTF-8");
System.out.println(requestXml);
httpPost.setEntity(requestEntity);
//裝填引數
httpPost.setHeader("Content-type", "text/xml");
try {
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定編碼轉換結果實體為String型別
body = EntityUtils.toString(entity, "utf-8");
}
System.out.println(body);
String string = body.replace("<", '<' + "");
String string1 = string.replace(">", '>' + "");
String str = string1.replaceAll("\"","\'");
System.out.println(str);
Document dom=DocumentHelper.parseText(str);
Element root=dom.getRootElement();
Element child = root.element("Body").element("zhjt_listResponse").element("zhjt_listResult");
//獲取根節點下面的所有子節點(不包過子節點的子節點)
List<Element> list = child.elements() ;
//遍歷List的方法
for (Element e:list){
System.out.println(e.getName());
map.put(e.getName(), e.getText());
}
}catch(Exception e){
log.info("獲取資訊出錯了");
}
return map;
}
public static void listNodes(Element node,Map<String, String> map){
System.out.println("當前節點的名稱:" + node.getName());
//首先獲取當前節點的所有屬性節點
List<Attribute> list = node.attributes();
//遍歷屬性節點
for(Attribute attribute : list){
System.out.println("屬性"+attribute.getName() +":" + attribute.getValue());
}
//如果當前節點內容不為空,則輸出
if(!(node.getTextTrim().equals(""))){
System.out.println( node.getName() + ":" + node.getText());
map.put(node.getName(), node.getText());
}
System.out.println(map.get("status"));
//同時迭代當前節點下面的所有子節點
//使用遞迴
Iterator<Element> iterator = node.elementIterator();
while(iterator.hasNext()){
Element e = iterator.next();
listNodes(e);
}
System.out.println(map);
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Test {
private static final Logger log = Logger.getLogger(Test.class);
public static Map<String, String> queryFromWebserviceByTel(String tel){
String body = "";
Map<String, String> map= new HashMap<String, String>();
String url ="220.189.240.226:8080/OA/Aspx/xla_kd_list.asmx";
String requestXml= "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"+
"<soap12:Body>"+
"<zhjt_list xmlns=\"http://tempuri.org/\">"+
"<account>"+tel+"</account>"+
"</zhjt_list>"+
"</soap12:Body>"+
"</soap12:Envelope>";
//建立httpclient物件
CloseableHttpClient client = HttpClients.createDefault();
//建立post方式請求物件
HttpPost httpPost = new HttpPost("http://220.189.240.226:8080/OA/Aspx/xla_kd_list.asmx");
HttpEntity requestEntity = new StringEntity(requestXml, "UTF-8");
System.out.println(requestXml);
httpPost.setEntity(requestEntity);
//裝填引數
httpPost.setHeader("Content-type", "text/xml");
try {
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定編碼轉換結果實體為String型別
body = EntityUtils.toString(entity, "utf-8");
}
System.out.println(body);
String string = body.replace("<", '<' + "");
String string1 = string.replace(">", '>' + "");
String str = string1.replaceAll("\"","\'");
System.out.println(str);
Document dom=DocumentHelper.parseText(str);
Element root=dom.getRootElement();
Element child = root.element("Body").element("zhjt_listResponse").element("zhjt_listResult");
//獲取根節點下面的所有子節點(不包過子節點的子節點)
List<Element> list = child.elements() ;
//遍歷List的方法
for (Element e:list){
System.out.println(e.getName());
map.put(e.getName(), e.getText());
}
}catch(Exception e){
log.info("獲取資訊出錯了");
}
return map;
}
public static void listNodes(Element node,Map<String, String> map){
System.out.println("當前節點的名稱:" + node.getName());
//首先獲取當前節點的所有屬性節點
List<Attribute> list = node.attributes();
//遍歷屬性節點
for(Attribute attribute : list){
System.out.println("屬性"+attribute.getName() +":" + attribute.getValue());
}
//如果當前節點內容不為空,則輸出
if(!(node.getTextTrim().equals(""))){
System.out.println( node.getName() + ":" + node.getText());
map.put(node.getName(), node.getText());
}
System.out.println(map.get("status"));
//同時迭代當前節點下面的所有子節點
//使用遞迴
Iterator<Element> iterator = node.elementIterator();
while(iterator.hasNext()){
Element e = iterator.next();
listNodes(e);
}
System.out.println(map);
}
}