Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用jaxb覆盖子类中的属性_Java_Jaxb - Fatal编程技术网

Java 用jaxb覆盖子类中的属性

Java 用jaxb覆盖子类中的属性,java,jaxb,Java,Jaxb,我有以下XML代码: <stereotypes> <stereotype1/> <stereotype2/> </stereotypes> 我需要这个,因为我想以同样的方式对待这些刻板印象: public void someMethod() { for(AbstractStereotype stereotype: getStereotypes()) { System.out.println(stereotype.g

我有以下XML代码:

<stereotypes>
  <stereotype1/>
  <stereotype2/>
</stereotypes>
我需要这个,因为我想以同样的方式对待这些刻板印象:

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

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

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

  • 堆栈溢出:
  • 博客帖子:

我仍然不能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;
    }

}
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。是的,在进行初始搜索时,我已经找到了您提到的这两个链接。然而,我不认为这是我需要的。我有一个属性,我希望在不同的类中有不同的值。也许我不太理解链接堆栈溢出主题中所述的内容。我将编辑问题,更清楚地了解我需要的结果。我编辑了我的初始问题,如果我能以某种方式使用你的链接方法,也许你可以为我澄清一下。。。非常感谢。感谢Blaise的帮助,现在我的脑子里已经清楚多了:)
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);

    }
}
<?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>