调用接口,解析返回的的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);
}
}
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
- 调用接口,解析返回的的string类型xml文档
- 解析String类型的XML
- 利用pull解析器解析服务器返回的String类型的XML
- 详解 Web service Axis2 JAVA调用.net接口 返回String的xml格式数据
- java 解析接口返回xml类型值
- java调用http,解析返回的xml
- php 调用接口的post方法,还有解析返回的xml数据
- java 解析String类型的xml
- Dom4j解析String类型的xml数据
- java解析string类型的xml
- 如何解析String类型的XML
- DOM4J解析String类型的xml文件
- 接口的调用及xml的解析
- 如何通过SAXParser类解析调用 Google Weather API接口返回的XML结果
- asp通过json调用webservice接口,并获取返回的xml数据及解析
- Android--<String>XX</String>类型的XML解析
- API返回XML文档的解析并取值
- 解析支付宝单笔交易查询接口返回的XML信息
- dataframe.replace re.sub() 替换练习
- 欢迎使用CSDN-markdown编辑器
- 【K-D树 求矩阵的和】BZOJ-4066 简单题
- Windows下,JAVA-protobuf-3.5.0
- Mysql循环查询结果并设置排序编号
- 调用接口,解析返回的的string类型xml文档
- LDAP的相关概念与objectClass介绍
- C语言简单迷宫问题,深搜,通俗易懂。
- Android 不Root查看App /data/data/目录文件
- Codeforces Round #448 (Div. 2) A. Pizza Separation
- 数据库开发(MySql)
- 07 08-泛型-泛型限定(上限和下限) 09 10-泛型限定(上限的体现和下限的体现1 2) 11-泛型限定(通配符的体现) 12-集合查阅的技巧)
- AD15生成顶层丝印、底层丝印
- Android Native层log经验总结