【问题标题】:xmlElements with same name and different types同名不同类型的xmlElements
【发布时间】:2013-04-29 11:46:58
【问题描述】:

)

我有一个可以来自不同类型的 xmlelement。与类型无关,它具有相同的名称。它可以是一个对象,也可以只是通过 URI 对现有对象的引用。我认为 xmlElements 可能是解决方案。编组工作正常,但通过解组它,它每次都会选择最后一个给定的类类型。

包含元素的类 Flower

@XmlRootElement(name = "Flower")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "id", "name", "refName", "description", "created", "updated", "color",
    "seed")
public class Flower extends CommonElements{

private string color;
@XmlElements({
    @XmlElement(name="seed", type=Seed.class),
    @XmlElement(name="seed", type=Reference.class)  
})
public Object seed;

}

作为元素可以包含的类型之一的类种子

@XmlRootElement(name = "Seed")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "id", "name", "refName", "description", "created", "updated",
    "category", "country"})
public class Seed extends CommonElements{

protected String category = "";
protected String country = "";

}

以及元素可以包含的其他类

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Reference {
@XmlAttribute(name="href")
protected URI href;
}

在 CommonElements 中只是一些通用元素,如 id、refName、description 等。 XML 看起来像

<Flower>
    <id>http://localhost/test/flowers/1</id>
    <refName>redRose</refName>
    <description>classical red rose </description>
    <color>red</color>
    <seed href="http://localhost/test/seeds/1" />
</Flower>

或者喜欢

<Flower>
   <id>http://localhost/test/flowers/1</id>
   <refName>redRose</refName>
   <description>classical red rose </description>
   <color>red</color>
   <seed>
      <id>http://localhost/test/seeds/1</id>
      <refName>wildrose</refName>
      <description>Special Seed for beautiful wild roses</description>
      <category>wildrose</category>
      <country>china</country>
  </seed>
</Flower>

我假设类的不同结构足以让 jaxb 区分对象。 恐怕我必须使用适配器,但我希望有人有另一个好主意。

我知道有一个主题与我的相似。但是主题中的类型看起来很相似,所以 jaxb 无法区分它们。 (JAXB @XmlElements, different types but same name?)

感谢和抱歉我的英语不好。

编辑:有没有办法通过编组添加类型,jaxb 确切地知道它是用于解组的类型?

【问题讨论】:

    标签: xml jaxb choice


    【解决方案1】:

    @XmlElements 中不能有相同的名称。代码不会编译。您可以使用@XmlSeeAlso,但这不是正确的方法。 @XmlSeeAlso 适用于子类,但在这种情况下它会起作用。

    只有在您知道在

    花.对象

    @XmlRootElement(name = "Flower")
    @XmlSeeAlso({Seed.class,Reference.class})
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Flower extends CommonElements {
        public Flower(){}
    
        public Flower(String id, String name){
            super(id,name);
        }
        @XmlAttribute
        private String color;
        @XmlElement
        public Object seed;
    
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
    }
    
    <Flower>
        <id>2</id>
        <refName>redRose</refName>
        <color>RED</color>
        <seed xsi:type="seed" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <id>1</id>
            <refName>wildrose</refName>
            <category></category>
            <country>USA</country>
        </seed>
    </Flower>
    
    <Flower>
        <id>3</id>
        <refName>whiteRose</refName>
        <color>white</color>
        <seed xsi:type="reference" href="www.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    </Flower>
    

    【讨论】:

    • mhm.. 同名编译和编组完美。但是您的解决方案效果很好。就像你说的 xmlseealso 是为了别的东西.. 所以它似乎有点脏.. 但只要它有效,对我来说就很好。 :)
    • 奇怪。对我来说,在 JDK6 的 Eclipse Juno 中,它给出了编译错误。
    • mhm .. 现在我得到 [com.sun.istack.internal.SAXException2: “Seed”实例替换“java.lang.Object”,但“Seed”绑定到匿名类型.]
    • 我得到了错误,因为我在我的 Seed.class 中有 @XmlType(name="").. 现在这个错误是有道理的 ;)
    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    相关资源
    最近更新 更多