XML解析工具类

<?xml version="1.0" encoding="UTF-8"?>
<Request service="QuerySFWaybillService" lang="zh-CN">
    <Head>xiaomiapp</Head>
    <Body>
        <Waybill type="2" orderId="" waybillNo="SF1310510730738" phone="7289"/>
    </Body>
</Request>

请求XML

 

package com.sf.vsolution.hb.controller;

import com.alibaba.fastjson.JSONObject;
import com.sf.vsolution.hb.util.ExpressApiUtil;
import com.sf.vsolution.hb.util.QiaoConfig;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class QiaoApi {
    private static QiaoConfig qiaoConfig;

    @Autowired
    private void setQiaoConfig(QiaoConfig qiaoConfig) {
        QiaoApi.qiaoConfig = qiaoConfig;
    }

    /**
     * 运费查询接口---------------------------JIUSJHSDLJKANFKJLSADH
     *
     * @param waybillNo 运单号 + 手机号后四位
     * @return
     */
    public static JSONObject getWaybillFreightByQiaoSF(String waybillNo, String phone) {
        log.info("进入运费查询接口:运单号{}", waybillNo);
        ExpressApiUtil expressApiUtil = new ExpressApiUtil();
        Document doc = DocumentHelper.createDocument();
        Element request = doc.addElement("Request");
        request.addAttribute("service", "QuerySFWaybillService");
        request.addAttribute("lang", "zh-CN");
        Element head = request.addElement("Head");
        head.setText("xiaomiapp");
        Element body = request.addElement("Body");
        //order主体以订单号查询
        Element order = body.addElement("Waybill");
        order.addAttribute("type", "2");
        order.addAttribute("orderId", "");
        order.addAttribute("waybillNo", waybillNo);
        order.addAttribute("phone", phone);
        JSONObject jObject = new JSONObject();
        Document resultDoc = null;
        try {
            //小米测试环境
            log.info("运费查询请求参数:" + doc.asXML());
            String sourcexml = expressApiUtil.expressApi(doc.asXML(), "tSVUYZybpX2DZ5HkiMLN", "xiaomiapp", "http://bsprpcs.intsit.sfdc.com.cn/bsp-rpcs/sfexpressService");

            log.info("运费查询响应参数:" + sourcexml);
            resultDoc = DocumentHelper.parseText(sourcexml);
        } catch (DocumentException e) {
            log.error("BSP返回XML解析失败!" + e.getMessage(), e);
        }
        Element resultRoot = resultDoc.getRootElement();
        Element resultHead = resultRoot.element("Head");
        String strHead = resultHead.getTextTrim();

        try {
            if ("OK".equalsIgnoreCase(strHead)) {
                Element resultBody = resultRoot.element("Body");
                Element routeResponse = resultBody.element("Waybill");
                Element fee = routeResponse.element("Fee");
                jObject.put("result", "0");
                jObject.put("FreightName", fee.attribute("name").getText());
                jObject.put("Freight", fee.attribute("value").getText());

                log.info(jObject + "");
            } else {
                Element resultError = resultRoot.element("Error");
                jObject.put("result", "1");
                jObject.put("error", resultError.getTextTrim());
            }
        } catch (Exception e) {
            log.error("运费查询异常:" + e.getMessage(), e);
            jObject.put("result", 1);
            jObject.put("error", "BSP取数错误" + waybillNo);
        }
        return jObject;
    }


    /**
     * 地址:https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService
     * 顾客编码:YJJ_nYdWQ
     * 校验码:AxScci5T7LZPnRJfbqt6Fffjj9QPbIOU
     * 运单号:SF1314131454847
     * 手机号:8195
     *
     * @param args
     */

    public static void main(String[] args) {

        JSONObject jsonObject = getWaybillFreightByQiaoSF("SF1310510730738", "7289");
        System.out.println("jsonObject = " + jsonObject);
    }


}

响应XML

<?xml version='1.0' encoding='UTF-8'?>
<Response service="QuerySFWaybillService" lang="zh_CN">
    <Head>OK</Head>
    <Body>
        <Waybill waybillNo="SF1310510730738" addresseePhone="17782637289" consigneeEmpCode="000212" addresseeContName="顾客退货服务部" addresseeMobile="17782637289" prodName="顺丰标快" addresseeAddr="广东省广州市荔湾区DE##EwA9TkQc0Hli%2F4cCnucFsBAYaUlH1bY5uX2LJto1VSMh18PajsVselD8r%2Bisyu9ehuxmMR94j%2BbYbDF1SWl%2FvwwH%2BkM%3D" consignorContName="唯品会" consignorMobile="15012794312" consignorAddr="DE##EwA9TvFD4FLhu25Lin%2FhO%2FEUKZ5ApIZ5ny2T7y1OLWQbcx8%2BeeJ%2FtaGD6ajwQujzibwcRnp0hN8MaEb0ACAsXQeuihU%3D" consignorPhone="15012794312">
            <Fee name="运费" type="1" value="14.0"/>
        </Waybill>
    </Body>
</Response>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作 * @author HX * */ public class XmlManager { /** * 得到某节点下某个属性的值 * @param element 要获取属性的节点 * @param attributeName 要取值的属性名称 * @return 要获取的属性的值 * @author HX_2010-01-12 */ public static String getAttribute( Element element, String attributeName ) { return element.getAttribute( attributeName ); } /** * 获取指定节点下的文本 * @param element 要获取文本的节点 * @return 指定节点下的文本 * @author HX_2010-01-12 */ public static String getText( Element element ) { return element.getFirstChild().getNodeValue(); } /** * 解析某个xml文件,并在内存中创建DOM树 * @param xmlFile 要解析XML文件 * @return 解析某个配置文件后的Document * @throws Exception xml文件不存在 */ public static Document parse( String xmlFile ) throws Exception { // 绑定XML文件,建造DOM树 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document domTree = db.parse( xmlFile ); return domTree; } /** * 获得某节点下的某个子节点(指定子节点名称,和某个属性的值) * 即获取parentElement下名字叫childName,并且属性attributeName的值为attributeValue的子结点 * @param parentElement 要获取子节点的那个父节点 * @param childName 要获取的子节点名称 * @param attributeName 要指定的属性名称 * @param attributeValue 要指定的属性的值 * @return 符合条件的子节点 * @throws Exception 子结点不存在或有多个符合条件的子节点 * @author HX_2008-12-01 */ public static Element getChildElement( Element parentElement, String childName, String attributeName, String attributeValue ) throws Exception { NodeList list = parentElement.getElementsByTagName( childName ); int count = 0; Element curElement = null; for ( int i = 0 ; i < list.getLength() ; i ++ ) { Element child = ( Element )list.item( i ); String value = child.getAttribute( attributeName ); if ( true == value.equals( attributeValue ) ) { curElement =

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值