gpt4 book ai didi

xsd - XSD 模式和 JAXB 类中的多态性

转载 作者:行者123 更新时间:2023-12-03 11:01:04 26 4
gpt4 key购买 nike

我有一个这样的xml:

<todo>
<doLaundry cost="1"/>
<washCar cost="10"/>
<tidyBedroom cost="0" experiencePoints="5000"/>
</todo>

它的 XSD 架构是:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="doLaundry" />
<xs:element name="washCar" type="washCar" />
<xs:element name="tidyBedroom" type="tidyBedroom" />
</xs:choice>
</xs:sequence>
</xs:complexType>

<xs:complexType name="doLaundry">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>

<xs:complexType name="washCar">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>

<xs:complexType name="tidyBedroom">
<xs:attribute name="cost" type="xs:int" />
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:complexType>
</xs:schema>

当我通过 JAXB 处理这个模式时,我得到了一个具有如下方法的类:
public class Todo {

public List<Object> getDoLaundryOrWashCarOrTidyBedroom() {
...
}

}

理想情况下,我想要的是一种定义所有其他 XSD 类型扩展的通用基本类型的方法。从 XSD 模式生成的 Jaxb 类应该具有返回通用任务列表的方法。这将使向待办事项列表添加新任务变得非常容易:
public class Todo {

public List<Task> getTasks() {
...
}

}

public abstract class Task {
public int getCost() {
...
}
}

public class TidyBedroom extends Task {
public int getExperiencePoints() {
...
}
}

为了生成上述 Java 类,XSD 模式应该是什么样的?

最佳答案

我在 Blaise Doughan 的文章的帮助下找到了答案:http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-xsitype.html

此架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="doLaundry" />
<xs:element name="washCar" type="washCar" />
<xs:element name="tidyBedroom" type="tidyBedroom" />
</xs:choice>
</xs:sequence>
</xs:complexType>

<xs:complexType abstract="true" name="Task">
<xs:attribute name="cost" type="xs:int" use="required" />
</xs:complexType>

<xs:complexType name="doLaundry">
<xs:complexContent>
<xs:extension base="Task">
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="washCar">
<xs:complexContent>
<xs:extension base="Task">
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="tidyBedroom">
<xs:complexContent>
<xs:extension base="Task">
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:extension>
</xs:complexContent>
</xs:complexType>

</xs:schema>

结合绑定(bind)文件:
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings>
<jxb:bindings schemaLocation="todo.xsd" node="/xs:schema/xs:complexType[@name='todo']/xs:sequence/xs:choice">
<jxb:property name="Tasks"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>

正如我在问题中描述的那样,将给出抽象类和继承类。绑定(bind)文件会将 Jaxb 的默认方法名称从 getDoLaundryOrWashCarOrTidyBedroom() 更改为 getTasks()。

关于xsd - XSD 模式和 JAXB 类中的多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5020313/

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