【问题标题】:Overwriting attributes in subclasses with jaxb用 jaxb 覆盖子类中的属性
【发布时间】:2011-08-19 09:37:31
【问题描述】:

我有以下 XML 代码:

<stereotypes>
  <stereotype1/>
  <stereotype2/>
</stereotypes>

问题是我需要一个通用属性为每个原型类型具有不同的值。
实际上,我不确定这是否可行,或者我是否可以实现这样的事情。 我使用以下架构片段(设置路径属性)尝试了此操作。我想要的是为每个原型类型赋予这个属性一个固定值。目标是在 AbstractStereotype 类上生成 getPath 并以通用方式使用它。问题是我似乎找不到在特定 Stereotypes 中定义属性值的方法。

<xs:element name="stereotypes" minOccurs="0" maxOccurs="1">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="stereotype1" type="Stereotype1" />
      <xs:element name="stereotype2" type="Stereotype2"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

<xs:complexType name="AbstractStereotype" abstract="true">
       <xs:attribute name="path" type="amf-base:FQN" use="required"></xs:attribute>
</xs:complexType>

<xs:complexType name="Stereotype1">
     <xs:complexContent>
         <xs:extension base="AbstractStereotype">
            <!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class1"/> -->
         </xs:extension>
     </xs:complexContent>
</xs:complexType>

<xs:complexType name="Stereotype2">
     <xs:complexContent>
         <xs:extension base="AbstractStereotype">
            <!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class2"/> -->
         </xs:extension>
     </xs:complexContent>
</xs:complexType> 

任何其他可以让我“在 AbstractStereotype 类上生成 getPath 方法并以通用方式使用它”的建议将不胜感激。

编辑:也许更清楚我需要的结果。

public abstract class AbstractStereotype {
    public String getPath();
}

public class Stereotype1 extends AbstractStereotype {
    public String getPath() {
        return "Path1";
    }
}

public class Stereotype2 extends AbstractStereotype {
    public String getPath() {
        return "Path2";
    }
}

我需要这个,因为我想以同样的方式对待这些刻板印象:

public void someMethod() {
    for(AbstractStereotype stereotype: getStereotypes()) {
        System.out.println(stereotype.getPath());
    }
}

正如我之前所说,甚至不确定使用这种方法是否可行。

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    您是否希望将路径属性用作继承指示符?如果是这样,以下将有所帮助:


    我仍然不能 100% 确定我了解您的用例,但以下情况如何:

    刻板印象

    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElementRef;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Stereotypes {
    
        private List<AbstractStereotype> sterotypes;
    
        @XmlElementRef
        public List<AbstractStereotype> getSterotypes() {
            return sterotypes;
        }
    
        public void setSterotypes(List<AbstractStereotype> sterotypes) {
            this.sterotypes = sterotypes;
        }
    
    }
    

    抽象刻板印象

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlSeeAlso;
    
    @XmlSeeAlso({Stereotype1.class, Stereotype2.class})
    public abstract class AbstractStereotype {
    
        @XmlAttribute
        public abstract String getPath();
    
    }
    

    刻板印象1

    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Stereotype1 extends AbstractStereotype {
        public String getPath() {
            return "Path1";
        }
    }
    

    刻板印象2

    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Stereotype2 extends AbstractStereotype {
    
        public String getPath() {
            return "Path2";
        }
    
    }
    

    演示

    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Stereotypes.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Stereotypes stereotypes = (Stereotypes) unmarshaller.unmarshal(new File("input.xml"));
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(stereotypes, System.out);
    
        }
    }
    

    input.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <stereotypes>
      <stereotype1/>
      <stereotype2/>
    </stereotypes>
    

    输出

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <stereotypes>
        <stereotype1 path="Path1"/>
        <stereotype2 path="Path2"/>
    </stereotypes>
    

    更多信息

    【讨论】:

    • 不错的解决方案。绝对强调 XML/Schema 不是 OO。
    • 是的,在进行初步搜索时,我已经找到了您提到的这两个链接。但是,我认为这不是我需要的。我有一个属性,我希望在不同的类中有不同的值。也许我不太了解链接的 Stack Overflow 主题中所说的内容。我将编辑问题,更清楚我需要的结果。
    • 我编辑了我最初的问题,如果我能以某种方式使用您的链接方法,也许您可​​以为我澄清......非常感谢。
    • 感谢 Blaise 的帮助,现在我的头脑变得更加清晰了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2015-09-12
    • 1970-01-01
    相关资源
    最近更新 更多