JAXB 相对于 XSLT 有什么好处?
几天来我一直在脑子里思考这个问题并尝试不同的方法,并且我已经搜索了SO(请参阅将 XML 转换为 Java 对象的最佳实践是什么?)以及 Google 在 XSLT 上使用 JAXB 但找不到合适的答案。多年来,我在各种项目中使用过 Castor、JIBX 和 XSLT,因此我对 XML 绑定略知一二。
我的问题是我有一个非常扁平的 XML 结构,我想将其解组为 Java 类并将它们直接保存到关系数据库(必须是 Oracle)。一个小例子...
<Output>
<Channel>
<channelId>1</channelId>
<genreId>1</genreId>
</Channel>
<Channel>
<channelId>2</channelId>
<genreId>2</genreId>
</Channel>
<Genre>
<genreId>1</genreId>
<name>Movies</name>
</Genre>
<Genre>
<genreId>2</genreId>
<name>Sport</name>
</Genre>
<ChannelName>
<channelId>1</channelId>
<name>The Movie Channel</name>
</ChannelName>
<ChannelName>
<channelId>2</channelId>
<name>The Sport Channel</name>
</ChannelName>
</Output>
对于上面的 XML,我真正想要的只是 OneToOne 关系中的两个简单的带注释的类,我可以使用 JPA 保留它们。基本上,我想要反映数据库表的类,如下所示:
class Channel {
Long id;
String name;
Genre genre;
}
class Genre {
Long id;
String name;
}
XML 只会被解组,我真的在寻找这个问题的最简单的解决方案,我意识到这可能是一个主观问题。
我是 XSLT 的粉丝,所以我真的想问,使用 XSLT 将原始 XML 弯曲成更好的结构,更紧密地匹配代码,然后只使用一些非常简单的 JAXB 注释来绑定到我的类,这是否是一个好的设计。或者我应该选择使用 JAXB 进行“转换”,我认为这会涉及更多实际的 Java 代码,例如 XMLAdapter
和更多注释。基本上,有什么好处,或者 JAXB 相对于 XSLT 还给我带来了什么?
I've been going over this in my head and trying out different approaches for a few days now and I've searched SO (see What is best practice in converting XML to Java object?) and also Google for using JAXB over XSLT but cannot find a suitable answer. Over the years I've used Castor, JIBX and XSLT for various projects, so I know a little bit about XML binding.
My problem is that I have a very flat XML structure and I want to unmarshall it to Java classes and persist them directly to a relational database (has to be Oracle). A small example...
<Output>
<Channel>
<channelId>1</channelId>
<genreId>1</genreId>
</Channel>
<Channel>
<channelId>2</channelId>
<genreId>2</genreId>
</Channel>
<Genre>
<genreId>1</genreId>
<name>Movies</name>
</Genre>
<Genre>
<genreId>2</genreId>
<name>Sport</name>
</Genre>
<ChannelName>
<channelId>1</channelId>
<name>The Movie Channel</name>
</ChannelName>
<ChannelName>
<channelId>2</channelId>
<name>The Sport Channel</name>
</ChannelName>
</Output>
What I really want for the XML above is just two simple annotated classes in a OneToOne relationship, that I can persist using JPA. Basically I want classes that reflect the database tables like so:
class Channel {
Long id;
String name;
Genre genre;
}
class Genre {
Long id;
String name;
}
The XML will only ever be unmarshalled and I am really looking for the simplest solution to this problem which I realise can be a subjective question.
I am a fan of XSLT so am really asking if it's good design to use XSLT to bend the original XML into a better structure the more closely matches the code and then just use some very simple JAXB annotations to bind to my classes. Or should I opt for doing the "transformation" using JAXB which I think would involve more actual Java code, for example XMLAdapter
s and more annotations. Basically, what is the benefit, or what else does JAXB give me over XSLT?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JAXB 是 Java 对象到 XML 的绑定 API。
XSL-T 将 XML 转换为 XML。
他们给我的感觉不一样。最终结果可能是 XML,但每种情况下的来源都不同。
我认为答案取决于您对每种技术的舒适程度。如果 XSL-T 和 JAXB 组合对您来说很自然,那么请务必朝这个方向发展 - 除非它是重复执行的运行时操作。在这种情况下,我想说性能可能是一个问题,具体取决于转换的复杂性。
JAXB is the Java object-to-XML binding API.
XSL-T transforms XML to XML.
They don't feel like the same thing to me. The end result might be XML, but the source is different in each case.
I think the answer depends on your comfort level with each technology. If the XSL-T and JAXB combination feels natural to you, by all means go in that direction - unless it's a runtime operation that's performed repeatedly. In that case I'd say that performance might be a concern, depending on the complexity of the transformations.
我建议查看 XStream,特别是它的 转换器 API。
XStream 提供了一种非常简单的方法来将 XML 序列化或反序列化为 Java,而无需提供 XSD(我相信 JAXB 会需要)。
I'd suggest taking a look at XStream, specifically its Converters API.
XStream provides a very simple way to serialize or deserialize XML to Java, without needing to provide a XSD (which I believe JAXB will require).
看一下 ReXSL,它是一个在 JAX-RS 之上集成了 JAXB 和 XSL 的 Web 开发框架。该站点将每个页面生成为带 JAXB 注释的对象,该对象由框架转换为 XML。然后,该 XML 连同附加的 XSL 样式表一起传递到浏览器。
Take a look at ReXSL, a web development framework that integrates JAXB and XSL, on top of JAX-RS. The site generates every page as a JAXB-annotated object, which is converted to XML by the framework. Then, this XML is delivered to the browser with attached XSL stylesheet.