Java 将 XML 解组为动态对象

发布于 2024-12-02 12:42:58 字数 341 浏览 0 评论 0原文

我正在寻找从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-12-09 12:42:58

注意:我是EclipseLink JAXB (MOXy) 领导者,也是 JAXB 2 的成员 (JSR-222) 专家 团体。


查看以下 EclipseLink 示例。它演示了如何将动态属性与 JPA 和 JAXB 实现结合使用:


选项 #1 - 具有动态属性的静态对象

MOXy 有一个 @XmlVirtualAccessMethods 扩展,允许您映射条目到 XML 的映射。这允许您向静态类添加属性。在下面的示例中,Customer 类具有“真实”名称属性,并且可能具有许多“虚拟”属性。

package blog.metadatasource.refresh;

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;

@XmlRootElement
@XmlType(propOrder={"firstName", "lastName", "address"})
@XmlVirtualAccessMethods
public class Customer {

    private String name;
    private Map<String, Object> extensions = new HashMap<String, Object>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object get(String key) {
        return extensions.get(key);
    }

    public void set(String key, Object value) {
        extensions.put(key, value);
    }

}

虚拟属性是通过 MOXy 的 XML 元数据定义的。在下面的示例中,我们将添加两个属性:middleName 和shippingAddress。

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.metadatasource.refresh">
    <java-types>
        <java-type name="Customer">
            <java-attributes>
                <xml-element
                    java-attribute="middleName"
                    name="middle-name"
                    type="java.lang.String"/>
                <xml-element
                    java-attribute="shippingAddress"
                    name="shipping-address"
                    type="blog.metadatasource.multiple.Address"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

了解更多信息


选项 #2 - 动态对象

MOXy 还提供完整的动态对象模型:

DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);

DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set(street, "123 A Street");
address.set(city, "Any Town");
customer.set("address", address);

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

了解更多信息

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.

package blog.metadatasource.refresh;

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;

@XmlRootElement
@XmlType(propOrder={"firstName", "lastName", "address"})
@XmlVirtualAccessMethods
public class Customer {

    private String name;
    private Map<String, Object> extensions = new HashMap<String, Object>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object get(String key) {
        return extensions.get(key);
    }

    public void set(String key, Object value) {
        extensions.put(key, value);
    }

}

The virtual properties are defined via MOXy's XML metadata. In the example below we will add two properties: middleName and shippingAddress.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.metadatasource.refresh">
    <java-types>
        <java-type name="Customer">
            <java-attributes>
                <xml-element
                    java-attribute="middleName"
                    name="middle-name"
                    type="java.lang.String"/>
                <xml-element
                    java-attribute="shippingAddress"
                    name="shipping-address"
                    type="blog.metadatasource.multiple.Address"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

For More Information


Option #2 - Dynamic Objects

MOXy also offers full dynamic object models:

DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);

DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set(street, "123 A Street");
address.set(city, "Any Town");
customer.set("address", address);

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

For More Information

丑丑阿 2024-12-09 12:42:58

那么,您基本上是在尝试使用 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?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文