我有两个这样的xml。一个是
print_name
第二个是
print
我的pojo是
<?xml version="1.0" encoding="UTF-8"?>
<Message>
<VersNbr>1</VersNbr>
<SubmrId>ABC</SubmrId>
<MsgSeqNbr>00065</MsgSeqNbr>
<CreateDt>20180528</CreateDt>
<CreateTm>172428</CreateTm>
<TravelBatch>
<BtchSeqNbr>00000001</BtchSeqNbr>
<ProviderNm>Travel</ProviderNm>
<TrvlBtchCurrCd>GBP</TrvlBtchCurrCd>
</TravelBatch>
</Message>
我们可以使用相同的tarvelBatch对象处理TravelBatch和RefBatch标签吗? 我尝试通过提供2个单独的@XMLElement来使用@XmlElements,但其无法正常工作。 预先感谢。
答案 0 :(得分:1)
在xml中看起来唯一不同的是一些批处理详细信息的周围元素。因此您可以创建一个新的类,如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
public class BatchDetails {
@XmlElement(name = "BtchSeqNbr")
private String batchSequenceNumber;
@XmlElement(name = "ProviderNm")
private String providerName;
@XmlElement(name = "TrvlBtchCurrCd")
private String travelBatchCurrencyCode;
}
然后是您的根类,以便处理这两个xml:
@XmlRootElement(name = "Message")
public class Message {
@XmlElement(name = "VersNbr")
protected int versNbr;
@XmlElement(name = "SubmrId")
protected String submrId;
@XmlElement(name = "MktCode")
protected String mktCode;
@XmlElement(name = "FileStatus")
protected String fileStatus;
@XmlElement(name = "MsgSeqNbr")
protected String msgSeqNbr;
@XmlElement(name = "CreateDt")
protected String createDt;
@XmlElement(name = "CreateTm")
protected String createTm;
@XmlElement(name = "ExprDt")
protected String exprDt;
@XmlElement(name = "HubTimeStamp")
protected String hubTimeStamp;
@XmlElements({
@XmlElement(name = "RefBatch", type = BatchDetails.class),
@XmlElement(name = "TravelBatch", type = BatchDetails.class)
})
protected BatchDetails batchDetails;
protected String isValidRec;
}
注意带有@XmlElements
批注的batchDetails字段。