JaxB - 如何不映射特定的 XML 节点
这是我的问题:
我正在开发一个从 Castor 迁移到 JaxB 的项目。我对 JaxB 很陌生,面临着一个令我难以置信的问题,但解决起来可能很简单。这是一个简单的示例:
class Data
{
private Item item;
}
我有包含一个项目的数据。输出 XML 文件(我无法更改)如下所示:
<Data>
<Items>
<Item>
<Item>
<Items>
<Data>
即使 Data 仅包含一个 Item,它也应该在输出 XML 中作为 Items 中的元素呈现。问题是,我不希望 JaxB 为 Items 生成类或属性,而只为 Data 和 Item 生成。
我的架构应该是什么样子?有没有什么方法可以在模式中指定指定的项目存储在没有类表示的节点中?按照我的逻辑,可能是这样的:
<xs:element name="Data">
<xs:complexType>
<xs:sequence>
<xs:element ref="Items"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Items"> <!-- Do not generate plz -->
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Here is my problem:
I'm working on a project to migrate from Castor to JaxB. I am very new to JaxB and facing a problem which boggles my mind and yet, may be quite simple to solve. Here is a simple example :
class Data
{
private Item item;
}
I have Data containing one Item. Here is how the output XML file (which I cannot change) looks like:
<Data>
<Items>
<Item>
<Item>
<Items>
<Data>
Even though Data contains only one Item, it should be presented in the output XML as an element in Items. The thing is, I do not want JaxB to generate a class or property for Items, only Data and Item.
How should my schema look like? Is there any way to specify in the schema that the specified item is stored in a node which has no class representation? Something which in my logic could look like that:
<xs:element name="Data">
<xs:complexType>
<xs:sequence>
<xs:element ref="Items"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Items"> <!-- Do not generate plz -->
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。
对于以下片段,无法以这种方式生成类,因为标准 JAXB API 不提供基于路径的映射。
但是,从 Java 类开始,您可以使用 MOXy 的
@XmlPath
扩展来映射此用例:了解更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
For the following fragment, there isn't a way to generate the classes in that way, because the standard JAXB APIs don't provide path based mapping.
However, starting from Java classes you could use MOXy's
@XmlPath
extension to map this use case:For More Information
一种选择是编写自己的 jaxb 插件:您可以在这里找到介绍: http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html。在您的插件中,您可以使用 Codemodel API 生成代码;我在这里写了一篇博客: http:// www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/21495。当 XJC 到达您的“Items”元素时,它将把控制权传递给您,在您的插件中,您只需忽略它并为底层“Item”元素创建 setter/getter。我确信还有其他同样有效的变体。
One option is to write your own jaxb plugin: you can find an introduction here: http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html. Within your plugin, you generate code using the Codemodel API; I've blogged about that here: http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/21495. When XJC gets to your "Items" element, it will pass control to you and in your plugin, you'd simply ignore it and create the setter/getter for the underlying "Item" element instead. I'm sure there are other variations that are just as valid.
使用标准 JAXB 注释:
Using standard JAXB annotations: