且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有类似结构的XML的JAXB

更新时间:2023-11-06 09:47:10

您可以这样编写Trade元素的类:

You can write the class for element Trade like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TradeType", propOrder = {
    "something1",
    "something2"
})
public class TradeType {
    @XmlElement(name = "Something1")
    protected Something1Type something1;
    @XmlElement(name = "Something2")
    protected Something2Type something2;
    // getters, setters
}

顺便说一句,这是编译(xjc)此XML模式时得到的:

BTW, this is what you get when you compile (xjc) this XML schema:

<xs:complexType name="TradeType">
  <xs:choice>
    <xs:element name="Something1" type="Something1Type"/>
    <xs:element name="Something2" type="Something2Type"/>
  </xs:choice>
</xs:complexType>

您只有一个***元素Trade,可以通过将 trade.getSomethingX()与null进行比较来区分(解组后).

You'll just have a single top-level element Trade, and the distinction (after unmarshalling) can be made by comparing trade.getSomethingX() with null.

如果Something1Type和Something2Type类型相同(不是相似"),则可以使用相同的 class (而不是接口).

If types Something1Type and Something2Type are identical (not "similar"), you could use the same class (not the interface).