嵌套 XML 解组为 POJO class
Nested XML unmarshalling to POJO class
我是 JAXB 编组和解组的新手,我正在尝试将嵌套 XML 解组为 Java POJO class,但我在解组对象中得到 null。另外我想确认我是否为相应的 XML.
正确创建了 POJO 类
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ebo:EBO xmlns:ebo="http://service.gap.com/schemas/Tracking" xmlns:esb="http://service.gap.com/schemas/ESBHeader" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.service.gap.com/DeleteItemEBO DeleteItemEBO.xsd">
<esb:ESBHeader>
<esb:EnvironmentName>Development</esb:EnvironmentName>
<esb:VersionNbr>1.0</esb:VersionNbr>
<esb:BusinessEvent>
<esb:TransactionTypeCode>Delta</esb:TransactionTypeCode>
<esb:Description></esb:Description>
<esb:Timestamp>2017-10-26T16:30:13.154-07:00</esb:Timestamp>
<esb:EventID>01</esb:EventID>
<esb:TotalRecordCount>1</esb:TotalRecordCount>
</esb:BusinessEvent>
</esb:ESBHeader>
<ebo:EBOPayload>
<ebo:Item ebo:ActionTypeCode="DELETE">
<ebo:BrandId>2</ebo:BrandId>
<ebo:ItemID>12345</ebo:ItemID>
<ebo:PurgeLevelCd>k</ebo:PurgeLevelCd>
<ebo:PurgeDate>2017-10-26</ebo:PurgeDate>
</ebo:Item>
</ebo:EBOPayload>
</ebo:EBO>
JAXBExample.java 文件
public class JAXBExample {
public static void main(String [] args){
try{
File file = new File("C:\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
EBO ebo = (EBO) jaxbUnmarshaller.unmarshal(file);
System.out.println(EBO.getPayload());
}
catch(Exception ex){
System.out.println("JAXB Exception" +ex);
}
POJO 类:
EBO.java
@XmlRootElement(name = "ebo")
public class EBO {
private ESBHeader esbHeader;
private EBOPayload eboPayload;
public ESBHeader getEsbHeader() {
return esbHeader;
}
@XmlElement(name = "ESBHeader")
public void setEsbHeader(ESBHeader esbHeader) {
this.esbHeader = esbHeader;
}
public EBOPayload getEboPayload() {
return eboPayload;
}
@XmlElement(name = "EBOPayload")
public void setEboPayload(EBOPayload eboPayload) {
this.eboPayload = eboPayload;
}
}
ESBHeader.java
public class ESBHeader {
private String environmentName;
private String versionNbr;
private List<BusinessEvent> businessEvent;
public String getEnvironmentName() {
return environmentName;
}
@XmlElement(name = "EnvironmentName")
public void setEnvironmentName(String environmentName) {
this.environmentName = environmentName;
}
public String getVersionNbr() {
return versionNbr;
}
@XmlElement(name = "VersionNbr")
public void setVersionNbr(String versionNbr) {
this.versionNbr = versionNbr;
}
public List<BusinessEvent> getBusinessEvent() {
return businessEvent;
}
@XmlElement(name = "BusinessEvent")
public void setBusinessEvent(List<BusinessEvent> businessEvent) {
this.businessEvent = businessEvent;
}
}
BusinessEvent.java
public class BusinessEvent {
private String transactionTypeCode;
private String timestamp;
private String eventID;
private String totalRecordCount;
private String description;
public String getTransactionTypeCode() {
return transactionTypeCode;
}
@XmlElement(name = "TransactionTypeCode")
public void setTransactionTypeCode(String transactionTypeCode) {
this.transactionTypeCode = transactionTypeCode;
}
public String getDescription() {
return description;
}
@XmlElement(name = "Description")
public void setDescription(String description) {
this.description = description;
}
public String getTimestamp() {
return timestamp;
}
@XmlElement(name = "Timestamp")
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getEventID() {
return eventID;
}
@XmlElement(name = "EventID")
public void setEventID(String eventID) {
this.eventID = eventID;
}
public String getTotalRecordCount() {
return totalRecordCount;
}
@XmlElement(name = "TotalRecordCount")
public void setTotalRecordCount(String totalRecordCount) {
this.totalRecordCount = totalRecordCount;
}
}
EBOPayload.java
public class EBOPayload {
private List<Item> item;
public List<Item> getItem() {
return item;
}
@XmlElement(name = "Item")
public void setItem(List<Item> item) {
this.item = item;
}
}
Item.java
public class Item {
private String brandId;
private String itemID;
private String purgeLevelCd;
private String purgeDate;
public String getBrandId() {
return brandId;
}
@XmlElement(name = "BrandId")
public void setBrandId(String brandId) {
this.brandId = brandId;
}
public String getItemID() {
return itemID;
}
@XmlElement(name = "ItemID")
public void setItemID(String itemID) {
this.itemID = itemID;
}
public String getPurgeLevelCd() {
return purgeLevelCd;
}
@XmlElement(name = "PurgeLevelCd")
public void setPurgeLevelCd(String purgeLevelCd) {
this.purgeLevelCd = purgeLevelCd;
}
public String getPurgeDate() {
return purgeDate;
}
@XmlElement(name = "PurgeDate")
public void setPurgeDate(String purgeDate) {
this.purgeDate = purgeDate;
}
}
可以在标记@XMLRootElement 和@XMLElement 中再添加一个属性'namespace'
参考link:http://www.mysamplecode.com/2012/06/jaxb-convert-java-object-xml.html
我是 JAXB 编组和解组的新手,我正在尝试将嵌套 XML 解组为 Java POJO class,但我在解组对象中得到 null。另外我想确认我是否为相应的 XML.
正确创建了 POJO 类XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ebo:EBO xmlns:ebo="http://service.gap.com/schemas/Tracking" xmlns:esb="http://service.gap.com/schemas/ESBHeader" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.service.gap.com/DeleteItemEBO DeleteItemEBO.xsd">
<esb:ESBHeader>
<esb:EnvironmentName>Development</esb:EnvironmentName>
<esb:VersionNbr>1.0</esb:VersionNbr>
<esb:BusinessEvent>
<esb:TransactionTypeCode>Delta</esb:TransactionTypeCode>
<esb:Description></esb:Description>
<esb:Timestamp>2017-10-26T16:30:13.154-07:00</esb:Timestamp>
<esb:EventID>01</esb:EventID>
<esb:TotalRecordCount>1</esb:TotalRecordCount>
</esb:BusinessEvent>
</esb:ESBHeader>
<ebo:EBOPayload>
<ebo:Item ebo:ActionTypeCode="DELETE">
<ebo:BrandId>2</ebo:BrandId>
<ebo:ItemID>12345</ebo:ItemID>
<ebo:PurgeLevelCd>k</ebo:PurgeLevelCd>
<ebo:PurgeDate>2017-10-26</ebo:PurgeDate>
</ebo:Item>
</ebo:EBOPayload>
</ebo:EBO>
JAXBExample.java 文件
public class JAXBExample {
public static void main(String [] args){
try{
File file = new File("C:\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
EBO ebo = (EBO) jaxbUnmarshaller.unmarshal(file);
System.out.println(EBO.getPayload());
}
catch(Exception ex){
System.out.println("JAXB Exception" +ex);
}
POJO 类:
EBO.java
@XmlRootElement(name = "ebo")
public class EBO {
private ESBHeader esbHeader;
private EBOPayload eboPayload;
public ESBHeader getEsbHeader() {
return esbHeader;
}
@XmlElement(name = "ESBHeader")
public void setEsbHeader(ESBHeader esbHeader) {
this.esbHeader = esbHeader;
}
public EBOPayload getEboPayload() {
return eboPayload;
}
@XmlElement(name = "EBOPayload")
public void setEboPayload(EBOPayload eboPayload) {
this.eboPayload = eboPayload;
}
}
ESBHeader.java
public class ESBHeader {
private String environmentName;
private String versionNbr;
private List<BusinessEvent> businessEvent;
public String getEnvironmentName() {
return environmentName;
}
@XmlElement(name = "EnvironmentName")
public void setEnvironmentName(String environmentName) {
this.environmentName = environmentName;
}
public String getVersionNbr() {
return versionNbr;
}
@XmlElement(name = "VersionNbr")
public void setVersionNbr(String versionNbr) {
this.versionNbr = versionNbr;
}
public List<BusinessEvent> getBusinessEvent() {
return businessEvent;
}
@XmlElement(name = "BusinessEvent")
public void setBusinessEvent(List<BusinessEvent> businessEvent) {
this.businessEvent = businessEvent;
}
}
BusinessEvent.java
public class BusinessEvent {
private String transactionTypeCode;
private String timestamp;
private String eventID;
private String totalRecordCount;
private String description;
public String getTransactionTypeCode() {
return transactionTypeCode;
}
@XmlElement(name = "TransactionTypeCode")
public void setTransactionTypeCode(String transactionTypeCode) {
this.transactionTypeCode = transactionTypeCode;
}
public String getDescription() {
return description;
}
@XmlElement(name = "Description")
public void setDescription(String description) {
this.description = description;
}
public String getTimestamp() {
return timestamp;
}
@XmlElement(name = "Timestamp")
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getEventID() {
return eventID;
}
@XmlElement(name = "EventID")
public void setEventID(String eventID) {
this.eventID = eventID;
}
public String getTotalRecordCount() {
return totalRecordCount;
}
@XmlElement(name = "TotalRecordCount")
public void setTotalRecordCount(String totalRecordCount) {
this.totalRecordCount = totalRecordCount;
}
}
EBOPayload.java
public class EBOPayload {
private List<Item> item;
public List<Item> getItem() {
return item;
}
@XmlElement(name = "Item")
public void setItem(List<Item> item) {
this.item = item;
}
}
Item.java
public class Item {
private String brandId;
private String itemID;
private String purgeLevelCd;
private String purgeDate;
public String getBrandId() {
return brandId;
}
@XmlElement(name = "BrandId")
public void setBrandId(String brandId) {
this.brandId = brandId;
}
public String getItemID() {
return itemID;
}
@XmlElement(name = "ItemID")
public void setItemID(String itemID) {
this.itemID = itemID;
}
public String getPurgeLevelCd() {
return purgeLevelCd;
}
@XmlElement(name = "PurgeLevelCd")
public void setPurgeLevelCd(String purgeLevelCd) {
this.purgeLevelCd = purgeLevelCd;
}
public String getPurgeDate() {
return purgeDate;
}
@XmlElement(name = "PurgeDate")
public void setPurgeDate(String purgeDate) {
this.purgeDate = purgeDate;
}
}
可以在标记@XMLRootElement 和@XMLElement 中再添加一个属性'namespace' 参考link:http://www.mysamplecode.com/2012/06/jaxb-convert-java-object-xml.html