调用接口,解析返回的的string类型xml文档

来源:互联网 发布:淘宝优化十大违规 编辑:程序博客网 时间:2024/04/30 10:07

解析接受的string类型xml文档

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 = new HashMap<String, String>();
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);

}








}

阅读全文
0 0
原创粉丝点击