嵌套XML解组到POJO类

时间:2018-01-17 10:02:16

标签: java xml jaxb jaxb2-basics

我是JAXB编组和解组的新手,我正在尝试将嵌套的XML解组为Java POJO类,但我在unmarshalled对象中得到null。另外,我想确认是否已为相应的XML正确创建了POJO类。

XML文件:

txtWeight.addTextChangedListener(textWatcher);

JAXBExample.java文件

<?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>

POJO课程:

EBO.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);
}

ESBHeader.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;
}
}

BusinessEvent.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;
}
}

EBOPayload.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;
}

}

Item.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;
}
}

1 个答案:

答案 0 :(得分:0)

另一个属性&#39;命名空间&#39;可以在标记@XMLRootElement和@XMLElement中添加 参考链接:http://www.mysamplecode.com/2012/06/jaxb-convert-java-object-xml.html