import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import com.alipayeco.medicalinscore.common.Global; public class JaxbUtil {//工具类 /**
* java对象转换为xml文件
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return xml文件的String
* @throws JAXBException
*/
public static String beanToXml(Object obj, Class<?> load) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.setProperty(Marshaller.JAXB_ENCODING, Global.ENCODING);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} /**
* xml文件配置转换为对象
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return java对象
* @throws JAXBException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlPath, Class<T> load) throws JAXBException, IOException {
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xmlPath));
} /**
* JavaBean转换成xml
* 默认编码UTF-8
* @param obj
* @param writer
* @return
*/
public static String convertToXml(Object obj) {
// return convertToXml(obj, "UTF-8");
return convertToXml(obj, "UTF-8");
} /**
* JavaBean转换成xml
* @param obj
* @param encoding
* @return
*/
public static String convertToXml(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
} return result;
} /**
* JavaBean转换成xml去除xml声明部分
* @param obj
* @param encoding
* @return
*/
public static String convertToXmlIgnoreXmlHead(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
} return result;
} /**
* xml转换成JavaBean
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
} return t;
} }
下面是调用方法
outXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><document><response><head><version>3.7.0</version><appid>2016012900930739</appid><function>alipay.medical.card.query</function><respTime>20180330122158</respTime><respTimeZone>UTC+8</respTimeZone><reqMsgId>78bc03b3-8c81-4ae7-a1c5-a02cfd0803a2</reqMsgId></head><body><resultInfo><resultStatus>S</resultStatus><resultCode>SUCCESS</resultCode><resultMsg>业务成功</resultMsg></resultInfo><medical_card_id>6049735182</medical_card_id><medical_card_no>B6C989166</medical_card_no><medical_card_validate_date>2038-03-30</medical_card_validate_date><medical_card_status>active</medical_card_status><medical_card_type>finance</medical_card_type><bank_card_no>6214677200012756150</bank_card_no><out_real_name>林海毅</out_real_name><out_user_card_type>01</out_user_card_type><out_user_card_no>450502198805230162</out_user_card_no><social_card_no>450502198805230162</social_card_no><extend_params>{\"card_query\":{\"dnh\":\"640750316\"}}</extend_params></body></response><signature>LuhjShqmZjFUHQ7SVxqELZnyUHB4EU/nWO4Or8RUJBbmSfsS+ChONxpGNCXYjGvd5Jfn8+t3ugqqKYPCGsRy3UDec63a2PllSKJCA1sOYSGfKDz0W38mSIgL/jWNXd4ryiijzQi4BQbTlHIoms6X5AhXeL30QW6dLpdvZlPHYWrjtS97n/991FJZDcb7df/UYYjZaicL+LhjUpG1WkN93PJc3RLzsHXF2u3OMAirTR7bhYx2L9bnY2VxnWiMPBcxBVB26r03bD5I5iqL8iLqmVcFqOQHt7iozUGywe71eBEV8dlgzDCswv3unCiywU8Ndvw/VOKdZXQ6OERbpGDCqA==</signature></document>";
ResponseDocumentBO<ResponseCardQueryBodyBO> outDocumentXml = JaxbUtil.converyToJavaBean(outXml, ResponseDocumentBO.class);
下面是javabean
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "document")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseDocumentBO<T> { @XmlElement(name = "response")
private ResponseBO<T> response; @XmlElement(name = "signature")
private String signature; public ResponseBO<T> getResponse() {
return response;
} public void setResponse(ResponseBO<T> response) {
this.response = response;
} public String getSignature() {
return signature;
} public void setSignature(String signature) {
this.signature = signature;
} }
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; @XmlAccessorType(XmlAccessType.FIELD)
public class ResponseCardQueryBodyBO { @XmlElement(name = "resultInfo")
private ResultInfo resultInfo; @XmlElement(name = "medical_card_id")
private String medicalCardId; @XmlElement(name = "medical_card_no")
private String medicalCardNo; @XmlElement(name = "medical_card_validate_date")
private String medicalCardValidateDate; @XmlElement(name = "medical_card_status")
private String medicalCardStatus; @XmlElement(name = "medical_card_type")
private String medicalCardType; @XmlElement(name = "bank_card_no")
private String bankCardNo; @XmlElement(name = "out_user_account")
private String outUserAccount; @XmlElement(name = "out_user_id")
private String outUserId; @XmlElement(name = "out_real_name")
private String outRealName; @XmlElement(name = "out_user_card_type")
private String outUserCardType; @XmlElement(name = "out_user_card_no")
private String outUserCardNo; @XmlElement(name = "out_user_certify_pic")
private String outUserCertifyPic; @XmlElement(name = "social_card_no")
private String socialCardNo; @XmlElement(name = "extend_params")
private String extendParams; public ResultInfo getResultInfo() {
return resultInfo;
} public void setResultInfo(ResultInfo resultInfo) {
this.resultInfo = resultInfo;
} public String getMedicalCardId() {
return medicalCardId;
} public void setMedicalCardId(String medicalCardId) {
this.medicalCardId = medicalCardId;
} public String getMedicalCardNo() {
return medicalCardNo;
} public void setMedicalCardNo(String medicalCardNo) {
this.medicalCardNo = medicalCardNo;
} public String getMedicalCardValidateDate() {
return medicalCardValidateDate;
} public void setMedicalCardValidateDate(String medicalCardValidateDate) {
this.medicalCardValidateDate = medicalCardValidateDate;
} public String getMedicalCardStatus() {
return medicalCardStatus;
} public void setMedicalCardStatus(String medicalCardStatus) {
this.medicalCardStatus = medicalCardStatus;
} public String getMedicalCardType() {
return medicalCardType;
} public void setMedicalCardType(String medicalCardType) {
this.medicalCardType = medicalCardType;
} public String getBankCardNo() {
return bankCardNo;
} public void setBankCardNo(String bankCardNo) {
this.bankCardNo = bankCardNo;
} public String getOutUserAccount() {
return outUserAccount;
} public void setOutUserAccount(String outUserAccount) {
this.outUserAccount = outUserAccount;
} public String getOutUserId() {
return outUserId;
} public void setOutUserId(String outUserId) {
this.outUserId = outUserId;
} public String getOutRealName() {
return outRealName;
} public void setOutRealName(String outRealName) {
this.outRealName = outRealName;
} public String getOutUserCardType() {
return outUserCardType;
} public void setOutUserCardType(String outUserCardType) {
this.outUserCardType = outUserCardType;
} public String getOutUserCardNo() {
return outUserCardNo;
} public void setOutUserCardNo(String outUserCardNo) {
this.outUserCardNo = outUserCardNo;
} public String getOutUserCertifyPic() {
return outUserCertifyPic;
} public void setOutUserCertifyPic(String outUserCertifyPic) {
this.outUserCertifyPic = outUserCertifyPic;
} public String getSocialCardNo() {
return socialCardNo;
} public void setSocialCardNo(String socialCardNo) {
this.socialCardNo = socialCardNo;
} public String getExtendParams() {
return extendParams;
} public void setExtendParams(String extendParams) {
this.extendParams = extendParams;
} @Override
public String toString() {
return "ResponseCardQueryBodyBO [resultInfo=" + resultInfo + ", medicalCardId=" + medicalCardId + ", medicalCardNo=" + medicalCardNo
+ ", medicalCardValidateDate=" + medicalCardValidateDate + ", medicalCardStatus=" + medicalCardStatus + ", medicalCardType="
+ medicalCardType + ", bankCardNo=" + bankCardNo + ", outUserAccount=" + outUserAccount + ", outUserId=" + outUserId
+ ", outRealName=" + outRealName + ", outUserCardType=" + outUserCardType + ", outUserCardNo=" + outUserCardNo
+ ", outUserCertifyPic=" + outUserCertifyPic + ", socialCardNo=" + socialCardNo + ", extendParams=" + extendParams + "]";
} }
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement; //@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseBO<T> {
@XmlElement(name = "head")
private ResponseHeadBO head; @XmlElement(name = "body")
private ResponseCardQueryBodyBO responseCardQueryBody; public ResponseHeadBO getHead() {
return head;
} public void setHead(ResponseHeadBO head) {
this.head = head;
} public ResponseCardQueryBodyBO getResponseCardQueryBody() {
return responseCardQueryBody;
} public void setResponseCardQueryBody(ResponseCardQueryBodyBO responseCardQueryBody) {
this.responseCardQueryBody = responseCardQueryBody;
} @Override
public String toString() {
return "ResponseBO [head=" + head + ", responseCardQueryBody=" + responseCardQueryBody + "]";
}
}
希望对你们有帮助