Java:XML 到数据库中,最简单的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4248378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: XML into a Database, whats the simplest way?
提问by jeff porter
I have a load of XML files, and an XSD for them.
我有大量的 XML 文件,以及它们的 XSD。
I'd like to simply convert then into POJO's and insert them into a database. The DB schema is under my control, so it can be what ever I like.
我想简单地将其转换为 POJO 并将它们插入到数据库中。DB 模式在我的控制之下,所以它可以是我喜欢的任何东西。
I've looked around at a load of apis, but wanted another opinion what works best.
我环顾了一堆 api,但想要另一种意见,什么最有效。
- JAXB?
- XMlBeans?
- XPATH to DBUnit?
- JAXB?
- XMLBeans?
- XPATH 到 DBUnit?
Does hibernate have some api to create POJO's from an XSD, then read the XML into those POJOs, and then insert the data into the database?
hibernate 是否有一些 api 从 XSD 创建 POJO,然后将 XML 读入这些 POJO,然后将数据插入数据库?
Or does spring have any features to help with this?
或者 spring 是否有任何功能可以帮助解决这个问题?
I guess I'm just after your views, just incase there is an API I've missed that will do help do what I want.
我想我只是在关注你的观点,以防万一我错过了一个 API,它可以帮助我做我想做的事。
Thanks Jeff Porter
感谢杰夫·波特
回答by duffymo
Or you could bypass the step of translating into POJOs and store the XML directly as a CLOB. It'll allow "duck typing" later on, which you might find advantageous.
或者,您可以绕过转换为 POJO 的步骤,直接将 XML 存储为 CLOB。稍后它将允许“鸭子打字”,您可能会发现这很有利。
Mapping to Java POJOs make sense if you need to query for those objects individually later on. If you need the entire stream, all the time, without ever having to query for values in the XML (e.g., XPath), then I'd say that storing XML as a CLOB makes more sense.
如果您稍后需要单独查询这些对象,那么映射到 Java POJO 是有意义的。如果您始终需要整个流,而不必查询 XML(例如 XPath)中的值,那么我会说将 XML 存储为 CLOB 更有意义。
回答by Gary Rowe
Quick answer: JAXB, JPA and Spring
快速回答:JAXB、JPA 和 Spring
When inserting XML into a database you need to consider what operations you'd like to perform on the data that the XML is representing.
将 XML 插入数据库时,您需要考虑要对 XML 表示的数据执行哪些操作。
You could, for example, consider the XML to be the input data and then create a schema that holds the data in an easily queryable manner. If that's what you'd like to do then use JAXB as the unmarshaller because you can easily generate suitably annotated pojos/entities from the XSD via the xjc tool. A bit of additional JPA annotating and you'll have a quick solution that maps the XML to a complete schema that allows a variety of mix and match queries and alternative views. Of course, JAXB annotations can be used to generate a wide variety of output formats (XML, JSON, YAML etc) so you're not limited to XML when you want to output this data.
例如,您可以将 XML 视为输入数据,然后创建一个以易于查询的方式保存数据的模式。如果这是您想要做的,那么使用 JAXB 作为解组器,因为您可以通过 xjc 工具轻松地从 XSD 生成适当注释的 pojos/实体。一些额外的 JPA 注释,您将有一个快速的解决方案,将 XML 映射到一个完整的模式,允许各种混合和匹配查询和替代视图。当然,JAXB 注释可用于生成多种输出格式(XML、JSON、YAML 等),因此当您想要输出这些数据时,您不仅限于 XML。
Next, you could consider the XML to be the complete entity that you wish to store. In that case you want to store it either as a CLOB or as XML (in Oracle). Oracle certainly supportsXPath based searches so you'd get a good opportunity for querying the resulting dataset.
接下来,您可以将 XML 视为您希望存储的完整实体。在这种情况下,您希望将其存储为 CLOB 或 XML(在 Oracle 中)。Oracle 当然支持基于 XPath 的搜索,因此您将获得查询结果数据集的好机会。
Finally, if you're thinking that XML is too bloated, and you're in control of any resulting changes to the pojos you could serialize the unmarshalled pojos directly into the database as BLOBs. You'll have a fairly compact schema and database, but you'll suffer when it comes to querying since it's all gonna be binary. And you'll have binary version compatibility issues later on if you have to deserialize a very old dataset based on old pojos.
最后,如果您认为 XML 过于臃肿,并且您可以控制对 pojo 的任何结果更改,您可以将未编组的 pojo 作为 BLOB 直接序列化到数据库中。您将拥有一个相当紧凑的架构和数据库,但在查询时您会受到影响,因为它都是二进制的。如果您必须反序列化基于旧 pojo 的非常旧的数据集,那么稍后您将遇到二进制版本兼容性问题。
So, to summarise, JAXB is a very good way to handle the unmarshalling and later marshalling processes. It's quick and simple and (nod to @Blaise Doughanhere) very well supported on SO for one thing. JPA is the technology of choice to perform your database operations. Hibernate is one implementer of JPA (with good extensions), and Spring supports it beautifully through the HibernateTemplate. Equally, you can use Spring's JpaTemplate which has, perhaps, a slightly shallower learning curve.
因此,总而言之,JAXB 是处理解组和后期编组过程的一种非常好的方法。它快速而简单,并且(在此向@Blaise Doughan点头)在 SO 上得到了很好的支持,因为一件事。JPA 是执行数据库操作的首选技术。Hibernate 是 JPA 的一个实现者(有很好的扩展),Spring 通过 HibernateTemplate 很好地支持它。同样,您可以使用 Spring 的 JpaTemplate,它的学习曲线可能稍微浅一些。
回答by bdoughan
A common approach would be to use JPA (i.e. EclipseLinkor Hibernate) to store the objects into the database, and JAXB (i.e. Metro, EclipseLink MOXy(I'm the tech lead), or JaxMe) to convert the objects to XML.
一种常见的方法是使用 JPA(即EclipseLink或 Hibernate)将对象存储到数据库中,并使用 JAXB(即 Metro、EclipseLink MOXy(我是技术负责人)或 JaxMe)将对象转换为 XML。
EclipseLink JAXB (MOXy) has a number of extensions for mapping JPA entities to XML:
EclipseLink JAXB (MOXy) 有许多用于将 JPA 实体映射到 XML 的扩展: