xml(soap报文)和java对象的相互转换

创建工具类

package com.iwangding.business.intermediary.service.impl.any;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * 帮助我们将xml转为java对象或者java对象转为xml,java模板查看template下的案例
 * @version 1.0
 * @date 2024/1/24 10:43
 */
public class Xmlhandler {
    public static <T> String java2xml( T response){
        try {
            JAXBContext ctx = JAXBContext.newInstance(new Class[]{response.getClass()});
            Marshaller marshaller = ctx.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(response, stringWriter);
            return stringWriter.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static <T> T xml2java(String xml, Class<T> responseType){
        try {
            JAXBContext ctx = JAXBContext.newInstance(new Class[]{responseType});
            Unmarshaller um = ctx.createUnmarshaller();
            return (T) um.unmarshal(new StringReader(xml));
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }
}

创建java对象

soap报文:

<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
                 <soapenv:Body>
                 <ns1:QueryBandwidthByUserNameResponse xmlns:ns1=\"http://localhost:8081/fdsace_Mobile/\">
                 <LoginName>13822355598</LoginName>
                 <Bandwidth>2</Bandwidth>
                 <Area>doubalu</Area>
                 <queryInfo>
                 <attrInfo>
                 <attrCode>queryType</attrCode>
                 <attrName>业务类型</attrName>
                 <attrValue>1</attrValue>
                 </attrInfo>
                 <attrInfo>
                 <attrCode>areaNo</attrCode>
                 <attrName>区号</attrName>
                 <attrValue>004</attrValue>
                 </attrInfo>
                 <attrInfo>
                 <attrCode>billingNo</attrCode>
                 <attrName>业务号</attrName>
                 <attrValue>13254868452</attrValue>
                 </attrInfo>
                 </queryInfo>
                 </ns1:QueryBandwidthByUserNameResponse>
                 </soapenv:Body>
                </soapenv:Envelope>

以下是测试用的java对象,对应上方的soap报文,可以以此为模板创建你的对象

package com.iwangding.business.intermediary.service.impl.any.template;

import lombok.Data;

import javax.xml.bind.annotation.*;

/**
 * @version 1.0
 * @date 2024/1/23 18:03
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "body"
})
@XmlRootElement(name = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
public class Envelope {

    @XmlElement(name = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/", required = true)
    protected Body body;

}
package com.iwangding.business.intermediary.service.impl.any.template;

import lombok.Data;

import javax.xml.bind.annotation.*;

/**
 * @version 1.0
 * @date 2024/1/23 17:42
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "queryBandwidthByUserNameResponse"
})
@XmlRootElement(name = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
public class Body {

    @XmlElement(name = "QueryBandwidthByUserNameResponse", namespace = "http://localhost:8080/AAAinterface_SDMobile/", required = true)
    protected QueryBandwidthByUserNameResponse queryBandwidthByUserNameResponse;
}
package com.iwangding.business.intermediary.service.impl.any.template;

import lombok.Data;

import javax.xml.bind.annotation.*;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "loginName",
        "bandwidth",
        "area",
        "queryInfo"
})
@XmlRootElement(name = "QueryBandwidthByUserNameResponse", namespace = "http://localhost:8080/AAAinterface_SDMobile/")
public class QueryBandwidthByUserNameResponse {

    @XmlElement(name = "LoginName", required = true)
    protected String loginName;

    @XmlElement(name = "Bandwidth", required = true)
    protected String bandwidth;

    @XmlElement(name = "Area", required = true)
    protected String area;
    @XmlElement(name = "queryInfo")
    private QueryInfo queryInfo;
}

package com.iwangding.business.intermediary.service.impl.any.template;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import java.util.List;

/**
 * @version 1.0
 * @date 2024/1/24 10:07
 */

@XmlAccessorType(XmlAccessType.FIELD)
class QueryInfo {
    @XmlElement(name = "attrInfo")
    private List<AttrInfo> attrInfoList;
}
package com.iwangding.business.intermediary.service.impl.any.template;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

/**
 * @version 1.0
 * @date 2024/1/23 18:08
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "attrInfo", propOrder = {
        "attrCode",
        "attrName",
        "attrValue"
})
public class AttrInfo {

    @XmlElement(name = "attrCode")
    private String attrCode;

    @XmlElement(name = "attrName")
    private String attrName;

    @XmlElement(name = "attrValue")
    private String attrValue;
}

测试效果

 public static void main(String[] args) throws JAXBException {
        String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                " <soapenv:Body>\n" +
                " <ns1:QueryBandwidthByUserNameResponse xmlns:ns1=\"http://localhost:8081/Asd_Mobile/\">\n" +
                " <LoginName>1381656578</LoginName>\n" +
                " <Bandwidth>1</Bandwidth>\n" +
                " <Area>doaslu</Area>\n" +
                " <queryInfo>\n" +
                " <attrInfo>\n" +
                " <attrCode>queryType</attrCode>\n" +
                " <attrName>业务类型</attrName>\n" +
                " <attrValue>2</attrValue>\n" +
                " </attrInfo>\n" +
                " <attrInfo>\n" +
                " <attrCode>areaNo</attrCode>\n" +
                " <attrName>区号</attrName>\n" +
                " <attrValue>001</attrValue>\n" +
                " </attrInfo>\n" +
                " <attrInfo>\n" +
                " <attrCode>billingNo</attrCode>\n" +
                " <attrName>业务号</attrName>\n" +
                " <attrValue>13809589756</attrValue>\n" +
                " </attrInfo>\n" +
                " </queryInfo>" +
                " </ns1:QueryBandwidthByUserNameResponse>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        Envelope objectOfXml = Xmlhandler.xml2java(soapXml, Envelope.class);
        String s = Xmlhandler.java2xml(objectOfXml);
        System.out.println(s);
    }
  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值