将 XML 文件编组到 Java pojo/domain 对象
我有一个旧的 Access 数据库,它基本上是一个平面文件。我想将数据迁移到我新的、面向对象的、基于休眠的 Wonder-app。
数据以 XML 文件形式提供,我想映射到三个单独的 java pojo。我计划使用 JAXB 来执行此操作,特别是使用 @xmlelement
注释。然而,XML 文件的结构并不是最佳的,在我的 pojo 中,我将数据分成了三个不同的对象。
JAXB 会对此提供帮助吗?我是否需要使用 jaxb.xml 根据现有 schema/xml 文件(不是 oo)简单地创建 java pojo?然后创建 apdater 类/层将数据放入我的三个 pojo 中?或者我可以直接从 xml 文件映射到具有正确配置/注释的 3 个 pojo 吗?
I have an old Access database that is basically one flat file. I want to migrate the data to my new whizzy, object oriented, hibernate based wonder-app.
The data is available as an XML file, and I want to map to three separate java pojos. I planned on using JAXB to do this, specifically using @xmlelement
annotation. However the structure of the XML file is not optimal, in my pojos I have split up the data into three different objects.
Will JAXB help with this ? Do I need to simply create java pojo based on existing schema/xml file (that is not oo) using jaxb. Then create apdater classes/layer to put the data into my three pojos ? Or can I map straight from the xml file to 3 pojos with correct config/annotation ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。
假设您的 XML 文档如下所示:
您可以利用 MOXy 的 @XmlPath 注释并执行类似的操作。 EclipseLink 还包括 JPA 实现:
行
您将需要创建一个 Root 对象来保存所有内容:
A
因为
A
、B
和C
对象的内容是都在同一级别,您可以使用 MOXy 的@XmlPath
注释并指定"."
XPath。这告诉 MOXy 该对象及其引用的对象出现在同一级别:B
我们再次使用
@XmlPath(".")
来映射 < code>B 和C
:C
演示
以下演示代码可用于运行此示例:
jaxb.属性
将 MOXy 指定为您的JAXB 提供程序,您需要将
jaxb.properties
文件包含在与您的域类相同的包中,并包含以下条目:更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Assuming your XML document looks something like the following:
You could leverage MOXy's @XmlPath annotation and do something like. EclipseLink also includes a JPA implementation:
Rows
You will need to create a Root object to hold everything:
A
Since the contents for the
A
,B
, andC
objects are all at the same level you can use MOXy's@XmlPath
annotation and specify the"."
XPath. This tells MOXy that the object, and the object it is referencing occur at the same level:B
Again we use
@XmlPath(".")
to map the relationship betweenB
andC
:C
Demo
The following demo code can be used to run this example:
jaxb.properties
To specify MOXy as your JAXB provider you need to include a
jaxb.properties
file in the same package as your domain classes with the following entry:For More Information
在不知道问题的具体情况的情况下......应该是可能的,但听起来这可能是您想要编写架构的情况,请使用 xjc 生成绑定对象,解组到生成的绑定对象,然后转换为您的域java 中的对象(或者如果适用的话直接使用生成的对象)。
Without knowing the specifics of your issues...it should be possible, but it sounds like this may be a case where you would want to write a schema, use xjc to generate binding objects, unmarshal to the generated binding objects, and then translate to your domain objects in java (or use the generated objects directly if applicable).
通常 JAXB 会有所帮助。即使您手动编写类和注释,我认为它比使用 DOM API 解析花费的时间更少。
此外,您可以使用 JAXB 自动生成值对象。我认为这是你应该尝试的方法。首先,您应该从 XML 生成 XSD 文件(除非您已经有一个)。然后您应该根据 XSD 生成值对象。然后只需解析该文件即可。 2 行代码就完成了。
Typically JAXB helps. Even if you are writing classes and annotations manually I think it takes less time than parsing using DOM API.
Moreover you can generate value objects automatically using JAXB. I think this is the approach you should try. First you should generate XSD file from your XML (unless you already have one). Then you should generated value objects based on the XSD. Then just parse the file. 2 code lines and you are done.