JAXB Java 和 XSD 映射
在我的一个项目中,我使用 JAXB2 编组器,通过契约优先的 Web 服务,我从 XML 模式生成对象。
一切都很好。但是,我有一个“代码可用性”问题。让我举个例子。
架构:
<xs:complexType name="personContractAlertListType">
<xs:sequence>
<xs:element ref="PersonContractAlert" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="PersonContractAlertsResponse">
<xs:complexType>
<xs:sequence>
<xs:element ref="PersonContractAlertList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
因此,为了访问 PersonContractAlerts
列表,我必须调用:
PersonContractAlertsResponse.getPersonContractAlertListType().getPersonContractAlert()
这有点长。
我的问题是:如何摆脱 getPersonContractAlertListType()
并直接转到:PersonContractAlertsResponse.getPersonContractAlert()
因为该包装元素实际上仅适用于 XSD,所以我不知道我的 Java 对象中不需要它。
换句话说,
<Element1>
<Wrapper>
<Element2/>
</Wrapper>
</Element1>
我希望它在 Java 中映射到: Element1.getElement2()
也许使用 JAXB 适配器。请记住,我不想触摸生成的对象。这必须在编组器设置(适配器、拦截器等)或 XSD 中完成(可能需要操作一些设置)。
多谢!
更新:
我找到了有关一些绑定操作的教程:
https: //jaxb.dev.java.net/guide/Using_ Different_datatypes.html
我将研究尝试使用 JAXB 绑定。
In one of my projects I use JAXB2 marshaller, having a contract-first web service I generate the objects from a XML Schema.
Everything works just fine. But, I have a "code usability" issue. Let me give you an example.
Schema:
<xs:complexType name="personContractAlertListType">
<xs:sequence>
<xs:element ref="PersonContractAlert" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="PersonContractAlertsResponse">
<xs:complexType>
<xs:sequence>
<xs:element ref="PersonContractAlertList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
So in order to access the list of PersonContractAlerts
I have to call:
PersonContractAlertsResponse.getPersonContractAlertListType().getPersonContractAlert()
Which is kinda long.
My question is: How can I get rid of the getPersonContractAlertListType()
and go directly to: PersonContractAlertsResponse.getPersonContractAlert()
Because that wrapper element is really only for XSD, I don't need it in my Java object.
In other words have:
<Element1>
<Wrapper>
<Element2/>
</Wrapper>
</Element1>
And I want it in Java to map to: Element1.getElement2()
Maybe using JAXB adaptors. And remember I don't want to touch generated objects. This has to be done either in the marshaller settings (adaptar, interceptor, etc) or in the XSD (maybe there are some settings to manipulate).
Thanks a lot!
UPDATE:
I found a tutorial on some binding operations:
https://jaxb.dev.java.net/guide/Using_different_datatypes.html
I will research try using JAXB bindings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获得所需的对象模型,您可以在 EclipseLink JAXB (MOXy) 中使用基于 XPath 的映射扩展。
To get the object model you want, you could use the XPath based mapping extension in EclipseLink JAXB (MOXy).
我不确定您的具体情况,但一般来说,额外输入 30 个字符的成本可能不足以证明寻找解决方案的合理性。特别是因为 Eclipse 将自动完成该方法调用。
I'm not sure about your particular situation, but in general, typing an extra 30 characters is probably not a big enough cost to justify even searching for a solution to this. Especially since Eclipse will auto-complete that method call.