gpt4 book ai didi

java - 嵌套 XML 解码到 POJO 类

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:57 26 4
gpt4 key购买 nike

我是 JAXB 编码和解码的新手,我正在尝试将嵌套 XML 解码到 Java POJO 类,但我在解码对象中得到 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;
}
}

项目.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”引用链接:http://www.mysamplecode.com/2012/06/jaxb-convert-java-object-xml.html

关于java - 嵌套 XML 解码到 POJO 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48298309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com