首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用jaxb覆盖子类中的属性

用jaxb覆盖子类中的属性
EN

Stack Overflow用户
提问于 2011-05-05 13:28:18
回答 1查看 816关注 0票数 3

我有以下XML代码:

代码语言:javascript
复制
<stereotypes>
  <stereotype1/>
  <stereotype2/>
</stereotypes>

问题是,对于每个原型,我需要一个通用属性,它对每个原型具有不同的价值。

实际上,我不确定这是否是可实现的,或者我是否能够实现这样的事情。我使用以下模式片段(设置path属性)尝试这样做。我想为每个原型赋予这个属性一个固定的值。目标是在getPath类上生成AbstractStereotype并以通用方式使用它。问题是,我似乎找不到在特定构造型中定义属性值的方法。

代码语言:javascript
复制
<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> 

任何其他允许我“在getPath类上生成一个AbstractStereotype方法并以通用方式使用它”的建议都是非常感谢的。

编辑:可能更清楚我需要的结果。

代码语言:javascript
复制
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";
    }
}

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

代码语言:javascript
复制
public void someMethod() {
    for(AbstractStereotype stereotype: getStereotypes()) {
        System.out.println(stereotype.getPath());
    }
}

正如我以前说过的,使用这种方法甚至不确定这是可能的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-05 15:09:30

您是否希望使用path属性作为继承指示符?如果是这样的话,以下内容将有所帮助:

我仍然不能百分之百地确定我是否理解您的用例,但是以下几点呢?

原型

代码语言:javascript
复制
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;
    }

}

AbstractStereotype

代码语言:javascript
复制
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();

}

Stereotype1

代码语言:javascript
复制
import javax.xml.bind.annotation.XmlRootElement;

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

Stereotype2

代码语言:javascript
复制
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Stereotype2 extends AbstractStereotype {

    public String getPath() {
        return "Path2";
    }

}

Demo

代码语言:javascript
复制
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

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<stereotypes>
  <stereotype1/>
  <stereotype2/>
</stereotypes>

输出

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<stereotypes>
    <stereotype1 path="Path1"/>
    <stereotype2 path="Path2"/>
</stereotypes>

获取更多信息

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5898479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档