最外面的bean | |
package com.sinotrans.ecustoms.pojo.cusdel; | |
import java.util.Date; | |
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.XmlElementWrapper; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import javax.xml.bind.annotation.XmlType; | |
/** | |
* @author wpf | |
* @create 2017-6-22 | |
*/ | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlRootElement (name = "DecMessage") | |
@XmlType(name = "DecMessage", propOrder = {"decHead","decList","decContainer","decLicenseDocu","decFreeTxt","decSign"}) | |
public class DecMessage { | |
@XmlElement(name = "DecHead") | |
private DecHead decHead; | |
@XmlElementWrapper(name = "DecLists") | |
@XmlElement(name = "DecList") | |
private List<DecList> decList; | |
@XmlElementWrapper(name = "DecContainers") | |
@XmlElement(name = "Container") | |
private List<Container> decContainer; | |
@XmlElementWrapper(name = "DecLicenseDocus") | |
@XmlElement(name = "LicenseDocu") | |
private List<LicenseDocu> decLicenseDocu; | |
@XmlElement(name = "DecFreeTxt") | |
private DecFreeTxt decFreeTxt; | |
@XmlElement(name = "DecSign") | |
private DecSign decSign; | |
public DecHead getDecHead() { | |
return decHead; | |
} | |
public void setDecHead(DecHead decHead) { | |
this.decHead = decHead; | |
} | |
public List<DecList> getDecList() { | |
return decList; | |
} | |
public void setDecList(List<DecList> decList) { | |
this.decList = decList; | |
} | |
public List<Container> getDecContainer() { | |
return decContainer; | |
} | |
public void setDecContainer(List<Container> decContainer) { | |
this.decContainer = decContainer; | |
} | |
public List<LicenseDocu> getDecLicenseDocu() { | |
return decLicenseDocu; | |
} | |
public void setDecLicenseDocu(List<LicenseDocu> decLicenseDocu) { | |
this.decLicenseDocu = decLicenseDocu; | |
} | |
public DecFreeTxt getDecFreeTxt() { | |
return decFreeTxt; | |
} | |
public void setDecFreeTxt(DecFreeTxt decFreeTxt) { | |
this.decFreeTxt = decFreeTxt; | |
} | |
public DecSign getDecSign() { | |
return decSign; | |
} | |
public void setDecSign(DecSign decSign) { | |
this.decSign = decSign; | |
} | |
@Override | |
public String toString() { | |
return "DecMessage [decHead=" + decHead + ", decList=" + decList + ", decContainer=" + decContainer | |
+ ", decLicenseDocu=" + decLicenseDocu + ", decFreeTxt=" + decFreeTxt + ", decSign=" + decSign + "]"; | |
} | |
} | |
对象的bean | |
package com.sinotrans.ecustoms.pojo.cusdel; | |
import java.util.Date; | |
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; | |
/** | |
* @author wpf | |
* @create 2017-6-22 | |
*/ | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlType(propOrder = { "bonNo", "cusFie", "decBpNo", "decNo", "relId", | |
"relManNo", "voyNo"}) | |
public class DecFreeTxt { | |
@XmlElement(name = "BonNo",required = true) | |
private String bonNo; | |
@XmlElement(name = "CusFie",required = true) | |
private String cusFie; | |
@XmlElement(name = "DecBpNo",required = true) | |
private String decBpNo; | |
@XmlElement(name = "DecNo",required = true) | |
private String decNo; | |
@XmlElement(name = "RelId",required = true) | |
private String relId; | |
@XmlElement(name = "RelManNo",required = true) | |
private String relManNo; | |
@XmlElement(name = "VoyNo",required = true) | |
private String voyNo; | |
public String getBonNo() { | |
return bonNo; | |
} | |
public void setBonNo(String bonNo) { | |
this.bonNo = bonNo; | |
} | |
public String getCusFie() { | |
return cusFie; | |
} | |
public void setCusFie(String cusFie) { | |
this.cusFie = cusFie; | |
} | |
public String getDecBpNo() { | |
return decBpNo; | |
} | |
public void setDecBpNo(String decBpNo) { | |
this.decBpNo = decBpNo; | |
} | |
public String getDecNo() { | |
return decNo; | |
} | |
public void setDecNo(String decNo) { | |
this.decNo = decNo; | |
} | |
public String getRelId() { | |
return relId; | |
} | |
public void setRelId(String relId) { | |
this.relId = relId; | |
} | |
public String getRelManNo() { | |
return relManNo; | |
} | |
public void setRelManNo(String relManNo) { | |
this.relManNo = relManNo; | |
} | |
public String getVoyNo() { | |
return voyNo; | |
} | |
public void setVoyNo(String voyNo) { | |
this.voyNo = voyNo; | |
} | |
@Override | |
public String toString() { | |
return "DecFreeTxt [bonNo=" + bonNo + ", cusFie=" + cusFie + ", decBpNo=" + decBpNo + ", decNo=" + decNo | |
+ ", relId=" + relId + ", relManNo=" + relManNo + ", voyNo=" + voyNo + "]"; | |
} | |
} | |
listBean 和bean 对象是一样的 只是最外层对象的 @XmlElementWrapper(name = "DecContainers") 是xml文件跟目录第一层@XmlElement(name = "Container") 是第二层 | |
package com.sinotrans.ecustoms.pojo.cusdel; | |
import java.util.Date; | |
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; | |
/** | |
* @author wpf | |
* @create 2017-6-22 | |
*/ | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlType(propOrder = { "containerId", "containerMd", "containerWt"}) | |
public class Container { | |
@XmlElement(name = "ContainerId",required = true) | |
private String containerId; | |
@XmlElement(name = "ContainerMd",required = true) | |
private String containerMd; | |
@XmlElement(name = "ContainerWt",required = true) | |
private String containerWt; | |
public String getContainerId() { | |
return containerId; | |
} | |
public void setContainerId(String containerId) { | |
this.containerId = containerId; | |
} | |
public String getContainerMd() { | |
return containerMd; | |
} | |
public void setContainerMd(String containerMd) { | |
this.containerMd = containerMd; | |
} | |
public String getContainerWt() { | |
return containerWt; | |
} | |
public void setContainerWt(String containerWt) { | |
this.containerWt = containerWt; | |
} | |
@Override | |
public String toString() { | |
return "Container [containerId=" + containerId + ", containerMd=" + containerMd + ", containerWt=" + containerWt | |
+ "]"; | |
} | |
} | |
最重要的是这个jaxb工具类 | |
package com.sinotrans.ecustoms.util.cusdel; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
/** | |
* Jaxb2工具类 | |
* @author zhuc | |
* @create 2013-3-29 下午2:40:14 | |
*/ | |
public class JaxbUtil { | |
/** | |
* JavaBean转换成xml | |
* 默认编码UTF-8 | |
* @param obj | |
* @param writer | |
* @return | |
*/ | |
public static String convertToXml(Object obj) { | |
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; | |
} | |
/** | |
* 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; | |
} | |
public static Object xml2java(String xml, Class clazz) { | |
try { | |
JAXBContext content = JAXBContext.newInstance(clazz); | |
Unmarshaller m = content.createUnmarshaller(); | |
StringReader sr = new StringReader(xml); | |
Object t = m.unmarshal(sr); | |
return t; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
xml字符串转换成对象Jaxb技术
最新推荐文章于 2022-11-17 17:34:09 发布