我是靠谱客的博主 含蓄诺言,最近开发中收集的这篇文章主要介绍顺丰下单后处理接收到的xml,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

xml字符串转Java对象

开始接触顺丰下单,下单完成后返回的居然是xml,早听说数据传送的两种格式json和xml。首先看到xml一脸迷茫,不知道从哪下手,最后查阅了资料,成功的将xml字符串转换成Java对象,不说那么多了,直接上代码:

import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import lombok.Data;
/**
* 顺丰接口response对象
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Response")
@Data
public class SfExpressResponse implements Serializable {
private static final long serialVersionUID = 1L;
//响应状态
@XmlElement(name = "Head")
private String Head;
//响应失败原因
@XmlElement(name = "ERROR")
private ERROR ERROR;
//响应结果
@XmlElement(name = "Body")
private Body Body;
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class ERROR {
@XmlAttribute(name = "code")
private String code;
@XmlValue
private String text;
}
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class Body {
@XmlElement(name = "OrderResponse")
private OrderResponse OrderResponse;
@XmlElement(name = "RouteResponse")
private RouteResponse RouteResponse;
}
@XmlRootElement(name="OrderResponse")
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class OrderResponse {
//订单号
@XmlAttribute(name = "orderid")
private String orderId;
//运单号
@XmlAttribute(name = "mailno")
private String mailNo;
//原寄地区域代码(可用于顺丰电子运单标签打印)
@XmlAttribute(name = "origincode")
private String originCode;
//目的地区域代码(可用于顺丰电子运单标签打印)
@XmlAttribute(name = "destcode")
private String destCode;
//筛单结果:1:人工确认 2:可收派 3:不可以收派
@XmlAttribute(name = "filter_result")
private String filterResult;
@XmlElement(name = "rls_info")
private Rls_info rls_info;
}
@XmlRootElement(name="rls_info")
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class Rls_info {
@XmlAttribute(name = "invoke_result")
private String invoke_result;
@XmlElement(name = "rls_detail")
private Rls_detail rls_detail;
}
@XmlRootElement(name="rls_detail")
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class Rls_detail {
@XmlAttribute(name = "sourceTransferCode")
private String sourceTransferCode;
@XmlAttribute(name = "sourceCityCode")
private String sourceCityCode;
@XmlAttribute(name = "sourceDeptCode")
private String sourceDeptCode;
@XmlAttribute(name = "sourceTeamCode")
private String sourceTeamCode;
@XmlAttribute(name = "destCityCode")
private String destCityCode;
@XmlAttribute(name = "destDeptCode")
private String destDeptCode;
@XmlAttribute(name = "destRouteLabel")
private String destRouteLabel;
@XmlAttribute(name = "cargoTypeCode")
private String cargoTypeCode;
@XmlAttribute(name = "limitTypeCode")
private String limitTypeCode;
@XmlAttribute(name = "expressTypeCode")
private String expressTypeCode;
@XmlAttribute(name = "twoDimensionCode")
private String twoDimensionCode;
@XmlAttribute(name = "proCode")
private String proCode;
}
@XmlRootElement(name="RouteResponse")
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class RouteResponse {
//运单号
@XmlAttribute(name = "mailno")
private String mailNo;
//路由
@XmlElement(name = "Route")
private List<Route> Route ;
}
@XmlRootElement(name="Route")
@XmlAccessorType(XmlAccessType.NONE)
@Data
public static class Route {
//路由节点发生的时间
@XmlAttribute(name = "accept_time")
private String acceptTime;
//路由节点具体描述
@XmlAttribute(name = "remark")
private String remark;
//路由节点操作码
@XmlAttribute(name = "opcode")
private String opcode;
}
}

上述,是完整的接收下单返回的xml字符串的值,若有缺失,可以仿照上边的属性进行补充。
在请求顺丰下单时,需要将数据转换成xml格式,我用的是在网上找的,如有遗漏请见谅:
工具类:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import net.sf.json.JSONObject;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/**
* xml与json的转换
*
*/
public class XmlUtil{
/**
* add by scy 通过JAXB生成XML字符串
* @param clazz
* @return
*/
public static String obj2XML(Object obj){
StringWriter sw = new StringWriter();
String xml = null;
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "gb2312");
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
xml = sw.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xml;
}
/**
* add by scy 通过JAXB将XML转化为Object
* @param xml
* @param clazz
* @return
*/
public static <T> Object xml2Obj(String xml,Class<T> clazz){
Object obj = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller um = jaxbContext.createUnmarshaller();
StringReader sr = new StringReader(xml);
obj = clazz.newInstance();
obj = um.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
}

获取数据生成xml请求字符串:

import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import com.common.util.StringUtil;
import lombok.Data;
@Data//这个是引入的jar中的注解相当于给属性添加get/set方法
public class ShunFengUtil {
@Value("${sf.clientCode}")
private String clientCode;
//商户号
(短的字母拼接的字符串)custid
是一串数字
@Value("${sf.checkword}")
private String checkword;
//验证码
(这个是一长串字符)
@Value("${sf.custid}")
private String custid="";
//月付id 我这里使用的是寄付月结
/**
* 顺丰下单接口
* @param params
* @return
*/
public static String getOrderServiceRequestXml(Map<String, String> params) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<Request service='OrderService' lang='zh-CN'>");
strBuilder.append("<Head>" + StringUtil.getSystemPropertie("clientCode") + "</Head>");
strBuilder.append("<Body>");
strBuilder.append("<Order").append(" ");
strBuilder.append("orderid='" + params.get("orderid") + "'").append(" ");
//返回顺丰运单号
strBuilder.append("express_type='1'").append(" ");
//寄件方信息
strBuilder.append("j_province='河南省'").append(" ");
strBuilder.append("j_city='郑州市'").append(" ");
strBuilder.append("j_county='高新区'").append(" ");
strBuilder.append("j_company='****业'").append(" ");
strBuilder.append("j_contact='张三'").append(" ");
strBuilder.append("j_tel='158******85'").append(" ");
strBuilder.append("j_address='常春路'").append(" ");
//收件方信息
strBuilder.append("d_province='" + params.get("d_province") + "'").append(" ");
strBuilder.append("d_city='" + params.get("d_city") + "'").append(" ");
strBuilder.append("d_county='" + params.get("d_county") + "'").append(" ");
strBuilder.append("d_company='" + params.get("d_company") + "'").append(" ");
strBuilder.append("d_tel='" + params.get("d_tel") + "'").append(" ");
strBuilder.append("d_contact='" + params.get("d_contact") + "'").append(" ");
strBuilder.append("d_address='" + params.get("d_address") + "'").append(" ");
//货物信息
strBuilder.append("express_type='1'").append(" ");
strBuilder.append("pay_method='1'").append(" ");
strBuilder.append("custid ='" + StringUtil.getSystemPropertie("custid") + "'").append(" ");
strBuilder.append("parcel_quantity='1'").append(" ");
strBuilder.append("is_docall='0'").append(" ");
strBuilder.append("sendstarttime=''").append(" ");
strBuilder.append("order_source='药品'").append(">");
strBuilder.append("<AddedService name='COD' value='1.01' value1='3712662207' />");
strBuilder.append("</Order>");
strBuilder.append("</Body>");
strBuilder.append("</Request>");
return strBuilder.toString();
}
/**
* 获取顺丰订单结果查询接口xml
* @param params
* @return
*/
private String getOrderSearchServiceRequestXml(Map<String,String> params){
String orderNo = params.get("orderNo");
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<Request service='OrderSearchService' lang='zh-CN'>");
strBuilder.append("<Head>" + clientCode + "</Head>");
strBuilder.append("<Body>");
strBuilder.append("<OrderSearch").append(" ");
strBuilder.append("orderid='" + orderNo.toString().trim() + "" + "'").append(" > ");
strBuilder.append("</OrderSearch>");
strBuilder.append("</Body>");
strBuilder.append("</Request>");
return strBuilder.toString();
}
/**
* 获取顺丰物流查询接口xml
*
* @param params
* @return
*/
public static String getRouteServiceRequestXml(Map<String, String> params) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<Request service='RouteService' lang='zh-CN'>");
strBuilder.append("<Head>" +
StringUtil.getSystemPropertie("clientCode")
+ "</Head>");
strBuilder.append("<Body>");
strBuilder.append("<RouteRequest").append(" ");
strBuilder.append("tracking_type='1'").append(" ");
strBuilder.append("method_type='1'").append(" ");
strBuilder.append("tracking_number='" + params.get("mailno") + "'").append(" >");
strBuilder.append("</RouteRequest>");
strBuilder.append("</Body>");
strBuilder.append("</Request>");
return strBuilder.toString();
}
/**
* 取消订单
*
* @param params
* @return
*/
public static String getConfirmRequestXml(Map<String, String> params) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<Request service='OrderConfirmService' lang='zh-CN'>");
strBuilder.append("<Head>").append(params.get("clientCode")).append("</Head>");
strBuilder.append("<Body>");
strBuilder.append("<OrderConfirm").append(" ");
strBuilder.append("orderid='").append("orderNo").append("' ");
strBuilder.append("dealtype='2'>").append(" ");
strBuilder.append("</OrderConfirm>");
strBuilder.append("</Body>");
strBuilder.append("</Request>");
return strBuilder.toString();
}
}

顺丰的电子面单打印的话,官网上就有Java的demo

最后

以上就是含蓄诺言为你收集整理的顺丰下单后处理接收到的xml的全部内容,希望文章能够帮你解决顺丰下单后处理接收到的xml所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部