我是 JAXB 的新手,可能有一个相当简单的解决方案,但我不知道该怎么做。我可能会从我无法控制的设备接收到以下 xml。
样品 1
<LoyaltyID entryMethod="swipe">
<Track1>636497123456678</Track1>
</LoyaltyID>
样品 2
<LoyaltyID entryMethod="manual">636497123456678</LoyaltyID>
我使用 xjc 创建了一个具有以下定义的 JAXB 类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"track1",
"track2",
"track3"
})
@XmlRootElement(name = "LoyaltyID")
public class LoyaltyID {
@XmlElement(name = "Track1")
protected String track1;
@XmlElement(name = "Track2")
protected String track2;
@XmlElement(name = "Track3")
protected String track3;
@XmlAttribute(name = "entryMethod")
protected String entryMethod;
/**
* Gets the value of the track1 property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTrack1() {
return track1;
}
/**
* Sets the value of the track1 property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTrack1(String value) {
this.track1 = value;
}
/**
* Gets the value of the track2 property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTrack2() {
return track2;
}
/**
* Sets the value of the track2 property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTrack2(String value) {
this.track2 = value;
}
/**
* Gets the value of the track3 property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTrack3() {
return track3;
}
/**
* Sets the value of the track3 property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTrack3(String value) {
this.track3 = value;
}
/**
* Gets the value of the entryMethod property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getEntryMethod() {
if (entryMethod == null) {
return "swipe";
} else {
return entryMethod;
}
}
/**
* Sets the value of the entryMethod property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setEntryMethod(String value) {
this.entryMethod = value;
}
当我解组样本 1 一切正常时,我可以通过调用 getTrack1() 方法访问 LoyaltyID (636497123456678)。但是,如果我收到示例 2 中的 XML,我将无法访问该数据。应该指出的是,当 entryMethod 属性设置为“手动”时,我只收到示例 2 XML。我不仅需要能够解组此 XML,而且还需要将其编组回最初发送它的设备。有谁知道我怎么能做到这一点?
非常感谢解决此问题的任何帮助。
谢谢!- 蒂姆