gpt4 book ai didi

java - 使用 jaxb 覆盖子类中的属性

转载 作者:行者123 更新时间:2023-11-30 09:49:16 25 4
gpt4 key购买 nike

我有以下 XML 代码:

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

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

<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());
}
}

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

最佳答案

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


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

}
}

输入.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>

了解更多信息

关于java - 使用 jaxb 覆盖子类中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5898479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com