如何使用 Java 和数据库实现 XML 模式的往返
实现接收 XML 文件的往返并最终使用 Java 将数据保存到数据库中的最佳方法是什么?目前我有:
1. XML 模式和XML 数据文件发送给我
- XSD 相当复杂并且属于外部方,所以我无法更改它
2.创建Java类
- JAXB 生成超过 150 个类,不幸的是架构可能会改变
- 我使用 Maven POM 来自动化该过程 3. 将 XML 数据文件解组为 Java 对象(使用 JAXB 注释)
- 数据将显示给用户进行操作
4. 将 Java 对象保存到 RDBMS (Oracle / MySQL)
- 看来JDO是最合适的解决方案
5.导出数据
- 数据可以再次导出为 XML 或 Excel(例如)
我找不到合适的方法将 JDO 元数据或注释添加到 Java 源代码(在 JAXB 过程中生成)以确保我可以保留 Java 类。我正在使用以下技术:
- Java、Maven、JAXB、JDO 和JDBC
我认为 DataNucleus 最适合,因为我可能必须经常在具有不同导出目的地的环境之间更改数据存储(RDBMS / XML / Excel)。我可能需要考虑的另外两项技术是:
- Spring 和 XDoclet
建议或教程指针将不胜感激
What is the best way of implementing a roundtrip for receiving XML files and eventually persisting the data into a database using Java. Currently I have:
1. An XML Schema & XML data files send to me
- the XSD is fairly complex and belongs to an external party, so I can not change it
2. Created the Java classes
- JAXB generates over 150 classes, unfortunately the schema can change
- I have used Maven POM to automate the process
3. Unmarshall the XML data files into Java objects (with JAXB annotation)
- the data would be displayed to the user to be manipulated
4. Persist the Java objects into a RDBMS (Oracle / MySQL)
- it seems JDO is the most suitable solution
5. Exporting the data
- The data can be exported again as XML or Excel (for example)
I can not find a suitable way to add JDO metadata or annotations to the Java source code (generated during the JAXB process) to ensure I can persist the Java classes. I am working with the following technologies:
- Java, Maven, JAXB, JDO & JDBC
I think DataNucleus would be most suited as I might have to change datastores (RDBMS / XML / Excel) often between environments with different export destinations. The only other two technologies I might need to consider is:
- Spring and XDoclet
Advice or a pointer to a tutorial would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,我将使用 JAXB 和 JAXB 的组合。注意:我领导 EclipseLink JAXB (MOXy),EclipseLink 还提供了一个优秀的 JPA 实现。
选项 #1 - 从 XML 模式开始
选项 #2 - 从 Java 类开始
如果 XML 模式发生更改,您始终可以重新生成模型,或者根据需要注释模型以处理对模型的修改XML 架构。
您可能会发现 HyperJAXB 项目很有用,因为我相信它将直接在 JAXB 模型上生成 JPA 注释。
这只是正常的 JAXB 使用。有关示例,请参阅:
JPA 是另一种替代方案。
这也是获取 XML 输出的正常 JAXB 用法。
For this I would use a combination of JAXB and JAXB. Note: I lead EclipseLink JAXB (MOXy), EclipseLink also provides an excellent JPA implementation.
Option #1 - Start from XML Schema
Option #2 - Start from Java Classes
If the XML schema changes you can always regenerate your model, or annotate your model as necessary to handle the modifications to the XML schema.
You may find the HyperJAXB project useful, as I believe it will generate JPA annotations right onto a JAXB model.
This is just normal JAXB usage. For an example see:
JPA is another alternative.
Again this is normal JAXB usage to get the XML output.
恕我直言,在数据库中存储由 JAXB 从不断发展的 XML 模式生成的 Java 对象实例没有多大意义。关系数据库的目标是以明确定义的关系结构存储数据,以便能够轻松查询。
如果您想要的只是能够按原样取回之前收到的 XML 文档,只需将其作为 clob 存储在某个表或文件系统中即可。
如果确实需要以结构化方式将数据存储在数据库中,那么首先设计数据库模式,然后设计映射此数据库模式所需的 JDO 类,并实现将 JAXB 对象转换为 JDO 对象所需的业务逻辑,以便将它们存储在数据库中。
但在考虑您想要使用的技术之前,我会更多地考虑您想要实现什么功能。
Storing, in database, instances of Java objects generated by JAXB from an evolving XML schema doesn't make much sense, IMHO. The goal of a relational database is to store data, in a well-defined relational structure, in order to be able to query it easily.
If all you want is to be able to get back the XML document you received some time before, as is, just store it as a clob in some table, or in the file system.
If storing the data in database in a structured way is really needed, then design your database schema first, then design the JDO classes needed to map this database schema, and implement the business logic needed to transform your JAXB objects into JDO objects in order to store them in database.
But I would think more about what functionality you want to implement before thinking about the technologies you want to use.