Java 将 XML 解组为动态对象
我正在寻找从 XML 定义创建和加载 JAVA 对象的最佳工具/方法。 我检查过JAXB,看起来不错,但没有'没有找到一种方法可以处理属性是动态的或不时更改的实体,因此希望有类似自动处理实体的方法,而无需将对象转换为预定义的实体对象。存在这样的东西吗?
工作流程就像这样,从 XML 读取,为每个具有动态属性集的实体创建类和/或为这些实体创建 ORM 映射部分,然后所有操作检索/存储到数据库中,或者可能会使用一些 NoSQL 解决方案,如 MongoDB。
I'm looking for best tool/way to create and load JAVA objects from XML definitions.
I had checked out JAXB, seems pretty nice, but didn't find is there a way to work with Entities which properties are dynamic, or changed from time to time, so want to have something like automatic way of working with entities, without converting Object into predefine Entity object. Does something like that exists?
Workflow would be like this read from XML create class for each Entity with dynamic set of attributes and/or create ORM mapping part for those entities and then all manipulation retrieve/store into db or probably will going to use some NoSQL solution like MongoDB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:我是EclipseLink JAXB (MOXy) 领导者,也是 JAXB 2 的成员 (JSR-222) 专家 团体。
查看以下 EclipseLink 示例。它演示了如何将动态属性与 JPA 和 JAXB 实现结合使用:
选项 #1 - 具有动态属性的静态对象
MOXy 有一个
@XmlVirtualAccessMethods
扩展,允许您映射条目到 XML 的映射。这允许您向静态类添加属性。在下面的示例中,Customer 类具有“真实”名称属性,并且可能具有许多“虚拟”属性。虚拟属性是通过 MOXy 的 XML 元数据定义的。在下面的示例中,我们将添加两个属性:middleName 和shippingAddress。
了解更多信息
选项 #2 - 动态对象
MOXy 还提供完整的动态对象模型:
了解更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
Check out the following EclipseLink example. It demonstrates how to use dynamic properties with both the JPA and JAXB implementations:
Option #1 - Static Objects with Dynamic Properties
MOXy has an
@XmlVirtualAccessMethods
extension which allows you to map entries in a map to XML. This allows you to add properties to a static class. In the example below the Customer class has a "real" name property and may have many "virtual" properties.The virtual properties are defined via MOXy's XML metadata. In the example below we will add two properties: middleName and shippingAddress.
For More Information
Option #2 - Dynamic Objects
MOXy also offers full dynamic object models:
For More Information
那么,您基本上是在尝试使用 XML 文件创建 POJO(普通的旧 Java 对象)吗?它们就像数据类,对吧?
我是 XStream 的忠实粉丝,它非常易于使用,并且在不需要时效果也很好验证。当需要针对架构进行验证时,我使用了 Castor 。我只是使用 XStream 将对象保存到 xml 文件,然后我可以从任何地方读回它,即使我更改了与该对象关联的数据值(我认为这就是“动态属性集”的意思,正确的?)。
So, you're basically trying to make POJO's (plain old Java objects) using XML files? They are just like data classes, right?
I'm a big fan of XStream, which is really easy to use and works great if you don't need validation. I've used Castor when validation against a schema was required. I just used XStream to save an object to an xml file and then I can read it back in from anywhere, even if I change the data values associated with the object (which I think is what you mean by "dynamic set of attributes", right?).