首页 > 代码库 > xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例
xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例
JaxbXmlUtil工具类
package com.aiait.ivs.util; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; /** * Jaxb工具类 xml和java类相互转换 * * @author sunjianbo * @date 2017年4月17日 */ public class JaxbXmlUtil { public static final String DEFAULT_ENCODING = "UTF-8"; /** * pojo转换成xml 默认编码UTF-8 * * @param obj 待转化的对象 * @return xml格式字符串 * @throws Exception JAXBException */ public static String convertToXml(Object obj) throws Exception { return convertToXml(obj, DEFAULT_ENCODING); } /** * pojo转换成xml * * @param obj 待转化的对象 * @param encoding 编码 * @return xml格式字符串 * @throws Exception JAXBException */ public static String convertToXml(Object obj, String encoding) throws Exception { String result = null; JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // 指定是否使用换行和缩排对已编组 XML 数据进行格式化的属性名称。 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); return result; } /** * xml转换成JavaBean * * @param xml xml格式字符串 * @param t 待转化的对象 * @return 转化后的对象 * @throws Exception JAXBException */ @SuppressWarnings("unchecked") public static <T> T convertToJavaBean(String xml, Class<T> t) throws Exception { T obj = null; JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); obj = (T) unmarshaller.unmarshal(new StringReader(xml)); return obj; } }
生成xml的实体类
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.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "header", "body" }) @XmlRootElement(name = "soapenv:Envelope") public class RequestEntity { @XmlAttribute(name="xmlns:soapenv") protected String soapenv="http://schemas.xmlsoap.org/soap/envelope/"; @XmlAttribute(name="xmlns:eapp") protected String eapp="http://wx.gmw9.com"; @XmlAttribute(name="xmlns:bus") protected String bus=null; @XmlElement(required = true,name="soapenv:Header") protected RequestHeader header; @XmlElement(required = true,name="soapenv:Body") protected RequestBody body; //get set方法省略 } import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "soapenv:Body") public class RequestBody { @XmlElement(required = true,name="eapp:aaaaaa") public BodyContent aaaaaaa //get set方法省略 } import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "aa1", "aa2", "aa3" }) @XmlRootElement(name = "eapp:aaaaaa") public class BodyContent { @XmlElement(required = true,name="eapp:compNo") protected String aa1; @XmlElement(required = true,name="eapp:agentCode") protected String aa2; @XmlElement(required = true,name="eapp:eAppId") protected String aa3; //get set方法省略 }
生成的xml如下
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eapp="http://wx.gmw9.com">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<eapp:aaaaaa>
<eapp:aa1>041</eapp:aa1>
<eapp:aa2>5000</eapp:aa2>
<eapp:aa3>?</eapp:aa3>
</eapp:aaaaaa>
</soapenv:Body>
</soapenv:Envelope>
xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。