基于具体实例的解组
我是 JaxB World 的新人,我面临着将存储的 xml 内容解组到 java 类对象的问题。问题描述如下。让我知道这是否可以解决 我的 xsd 文件包含以下内容(这只是一个示例) 学生信息
<xs:complexType name="specialization" abstract="true">
</xs:complexType>
<xs:complexType name="Engineering">
<xs:complexContent>
<xs:extension base="specialization">
<xs:sequence>
<xs:element name="percentage" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Medical">
<xs:complexContent>
<xs:extension base="specialization">
<xs:sequence>
<xs:element name="grade" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
现在所有相应的java类都是通过编译xsd生成的。现在假设在我的应用程序中,我将通过构造 Engineering 类实例来设置学生信息的专业化属性。所以当我保存时完成所有操作 保存的 xml 文件将具有如下所示的条目
<Student>
<Name>Name1</Name>
<Specialization>
<percentage>78<percentage>
</Specialization>
</Student>
现在,当上述内容进行解组时,解组失败,并显示意外元素。我猜这是 b'cos 专业化元素是专业化类型,它对自身调用解组,而不是存储的派生对象。
我希望我的解释很清楚。有什么方法可以根据派生类实例类型进行解组。 xsd 和 bindings.xjb 文件完全在我的控制范围内,因此我可以添加或修改任何条目/信息,这些条目/信息传达解组规则以在派生类上解组。
感谢您的建议,但它仍然不适合我。
这是我尝试的
选项 #1 - xsi:type
我的 xsd 看起来与示例中解释的相同,但 Xsi:type 仍然没有出现在结果 xml 中。编译时需要添加其他设置吗?我应该使用哪个 JaxB 版本?
选项#2 - 替换组
当我在 xsd 中添加替换条目部分时,XSD 编译失败,显示重复名称“工程”和“医学”。我猜元素名称和类型名称是相同的编译婴儿床(所有工程、医学、专业化在类型定义和元素名称方面都相同)
我无法修改生成的类,因为我们正在使用模型驱动架构。手里唯一的东西就是xsd。允许对 xsd 进行任何修改。理想情况下,第一个选项应该有效。但无法弄清楚为什么它不起作用。如果您有任何缩小问题范围的建议,请告诉我。
I am a new comer to JaxB World and I am facing one problem w.r.t. unmarshalling of the stored xml content into java class object. Problem description is as follows. Let me know if this is solvable
I have my xsd file which contains following content(this is just a example)
Student info
<xs:complexType name="specialization" abstract="true">
</xs:complexType>
<xs:complexType name="Engineering">
<xs:complexContent>
<xs:extension base="specialization">
<xs:sequence>
<xs:element name="percentage" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Medical">
<xs:complexContent>
<xs:extension base="specialization">
<xs:sequence>
<xs:element name="grade" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Now all the corresponding java classes are generated by compiling the xsd. Now lets assume in my application i will set the specialization attribute of Student info by constructing Engineering class instance. So after all the operation when i save
the xml file that get saved will have the entry like below
<Student>
<Name>Name1</Name>
<Specialization>
<percentage>78<percentage>
</Specialization>
</Student>
Now when the above content goes for unmarshalling, unmarshalling fails saying unexpected element . I guess this is b'cos Specialization element is of type specialization it calls unmarshalling on itself rather than derived object which is stored.
I hope my explanation is clear. Is there any way that we can unmarshall based on derived class instanse type. The xsd and bindings.xjb file is completely in my control so i can add or modify any entries/info which conveys to unmarshalling rules to unmarshall on derived class.
Thanks for your Suggestion but the it still not working for me.
Here is what I tried
Option #1 - xsi:type
My xsd looks same as what is explained in the example but still the Xsi:type doesn't come in the resulted xml. Do i need to add any other setting while compiling? Which JaxB version should i use for this?
Option#2 - Substitution Groups
When i added the substitution entry part in my xsd, XSD compilation failed saying duplicate names "Engineering" and "Medical". I guess element name and type Name being same compilation cribs(All engineering, Medical,specialization being same both in type definition and element Name)
I can't modify the generated classes as we are using Model driven Architecture. Only thing that is in hand is xsd. Any modification to the xsd is allowed. Ideally First option should have worked. But can't figure out why it is not working. Let me know if you have some suggestion to narrow down the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JAXB 时,可以采用不同的方式在 XML 中表示 Java 继承:
选项 #1 - xsi:type
在此表示中,使用属性来指示用于填充此元素的子类型。
有关详细示例,请参阅:
选项#2 - 替换组
这里是元素名称用于指示子类型。这对应于替换组的架构概念,并利用 JAXB 的
@XmlElementRef
注释:有关详细示例,请参阅:
There are different ways of representing Java inheritance in XML when using JAXB:
Option #1 - xsi:type
In this representation an attribute is used to indicate the subtype being used to populate this element.
For a detailed example see:
Option #2 - Substitution Groups
Here an element name is used to indicate the subtype. This corresponds to the schema concept of substitution groups and leverages JAXB's
@XmlElementRef
annotation:For a detailed example see: