在 Castor 中将集合映射到 XML
我正在尝试使用 Castor 将 POJO 映射到 XML。
假设我有一个包含 Items 集合的 Order...有没有办法实现如下所示的 xml:
<order>
...order attributes
<items>
<item> ..item attributes </item>
<item> ..other item </item>
</items>
</order>
我可以制作类似的东西,但没有
节点。在其他情况下这不会成为问题,但我的 XML 必须遵守严格的 XSD 架构,因此我需要这样做。
谢谢!
我想到了一种“解决方法”,其中涉及创建一个新的 java 对象(即节点),该对象仅包含项目列表......有人能想到更好的方法吗?从现在开始有 100 名代表的赏金开放!
I'm trying to map a POJO to XML using Castor.
Let's say I have a Order that has a collection of Items... is there any way of achieving an xml like the following:
<order>
...order attributes
<items>
<item> ..item attributes </item>
<item> ..other item </item>
</items>
</order>
I could make something similar but without the <items>
node. This wouldn't be a problem in other case but my XML must adhere to a strict XSD schema so I need to do it like that.
Thanks!
I though of a kind of "workaround" that would involve creating a new java object (that would be the node) that would only contain the list of items... can anyone think of a better approach? there's a 100 rep bounty open since now!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用bind-xml元素的位置属性
http ://castor.codehaus.org/1.2/xml-mapping.html#6.-Location-attribute
文档中的示例:
生成以下 XML:
You can use the location attribute of the bind-xml lement
http://castor.codehaus.org/1.2/xml-mapping.html#6.-Location-attribute
Example from the docs:
Produces the following XML:
另一个答案不使用集合属性,我认为这可能是您最终需要的。
当包含在 Order 对象的映射中时,类似这样的东西可能会起作用:
The other answer doesn't use the collection attribute which I think is probably what you're ultimately needing.
Something like this might work when included in your mapping for the Order object: