解析调用接口内容和构建接口请求xml
来源:互联网 发布:mac做java开发工具 编辑:程序博客网 时间:2024/05/19 13:24
如果接口返回值为java.io.InputStream类型
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public HotelListResult parseResult(InputStream input) {
HotelListResult hotelResult = new HotelListResult();
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(input);
Node countNode= doc.selectSingleNode("/TransmitData/hotelList/hotel-response/count");
if (countNode != null ){
int count = NumberUtils.toInt(countNode.getText());
}
其他基本类型内容都可以使用这种方法解析,如果解析的为集合,并且每个值都为对象,则可以使用下边方法解析
List<?> hotelBaseListNode = doc.selectNodes("/TransmitData/hotelList/hotel-response/hotelbase");
if (hotelBaseListNode != null && hotelBaseListNode.size() > 0){
for (Iterator it = hotelBaseListNode.iterator(); it.hasNext();) {
Element hotelBaseNode = (Element) it.next();
String hotelid = HotelApiUtil.getNodeText(hotelBaseNode.selectSingleNode("hotelid"));
String hotelName = HotelApiUtil.getNodeText(hotelBaseNode.selectSingleNode("hotelName"));
}
}
.......
如果接口返回值为String类型
import org.dom4j.DocumentHelper;
public static HotelListResult parseResult(String xml) {
try {
Document doc = DocumentHelper.parseText(xml);
Node countNode= doc.selectSingleNode("/TransmitData/hotelList/hotel-response/count");
if(countNode!=null ){
int count = NumberUtils.toInt(countNode.getText());
}
如果解析的为集合
List<?> hotelBaseListNode = doc.selectNodes("/TransmitData/hotelList/hotel-response/hotelbase");
if (hotelBaseListNode != null && hotelBaseListNode.size() > 0){
for (Iterator it = hotelBaseListNode.iterator(); it.hasNext();) {
Element hotelBaseNode = (Element) it.next();
String hotelid = HotelApiUtil.getNodeText(hotelBaseNode.selectSingleNode("hotelid"));
String hotelName = HotelApiUtil.getNodeText(hotelBaseNode.selectSingleNode("hotelName"));
}
}
}
...........
构建接口请求xml
public static String buildRequest(HotelListRequest hotelRequest) {
String strXml = "";
try {
Document xmldoc = DocumentHelper.createDocument();
// 创建根节点
Element hotelRoot = xmldoc.addElement("TransmitData");
hotelRoot.addAttribute("version", "V.01");
Element hotelListElement = hotelRoot.addElement("hotelList");
Element hotelRequestElement = hotelListElement.addElement("hotel-request");
hotelRequestElement.addElement("cityName").setText(hotelRequest.getCityId());
.............
strXml = xmldoc.asXML();
return strXml;
}
- 解析调用接口内容和构建接口请求xml
- ios--请求接口并解析xml
- 接口的调用及xml的解析
- 请求接口返回内容
- 接口调用请求
- 接口测试中 对xml下发内容的解析断言
- php xml接口调用
- 【一周工作总结】循环调用接口,和一些Xml格式文件的解析。
- http请求接口,http接口远程调用
- java 调用 webService Xfire接口,xml解析为object
- 调用接口,解析返回的的string类型xml文档
- java HttpUrlConnection get和post 模拟请求 调用接口
- post和get请求调用Http接口并拿到数据
- 解析XML类接口说明
- Asterisk XML解析接口说明
- XML解析之ContentHandler接口
- 解析xml(通讯接口)
- XML解析之ContentHandler接口
- QML Item及透明度
- linux进程调度浅析
- 链表的操作
- ld
- 为数据中心选择正确的网络管理系统
- 解析调用接口内容和构建接口请求xml
- 第八章 例8.5
- C++语法基础--关联容器--map(二)--查找并读取map中的元素count(),find()与 从map对象中删除元素erase()
- ubuntu11.10版本系统root权限获取问题
- servlet文件的压缩 HttpServlet
- HDOJ--1872--稳定排序【排序算法】
- Thread Local Storage
- 【jiasuba】U盘问题不用愁 加速吧为你来解决
- javascript 闭包基础分享