我有以下XML代码:
<stereotypes>
<stereotype1/>
<stereotype2/>
</stereotypes>问题是,对于每个原型,我需要一个通用属性,它对每个原型具有不同的价值。
实际上,我不确定这是否是可实现的,或者我是否能够实现这样的事情。我使用以下模式片段(设置path属性)尝试这样做。我想为每个原型赋予这个属性一个固定的值。目标是在getPath类上生成AbstractStereotype并以通用方式使用它。问题是,我似乎找不到在特定构造型中定义属性值的方法。
<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方法并以通用方式使用它”的建议都是非常感谢的。
编辑:可能更清楚我需要的结果。
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());
}
}正如我以前说过的,使用这种方法甚至不确定这是可能的。
发布于 2011-05-05 15:09:30
您是否希望使用path属性作为继承指示符?如果是这样的话,以下内容将有所帮助:
我仍然不能百分之百地确定我是否理解您的用例,但是以下几点呢?
原型
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
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
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Stereotype1 extends AbstractStereotype {
public String getPath() {
return "Path1";
}
}Stereotype2
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Stereotype2 extends AbstractStereotype {
public String getPath() {
return "Path2";
}
}Demo
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>获取更多信息
https://stackoverflow.com/questions/5898479
复制相似问题