如何从相互引用的 XML 标记创建 Java 对象?

2022-01-19 00:00:00 xml spring java jaxb jibx

我有一个 XML,它的标签对应于三种类型的 Java 对象,这些对象将从 XML 创建.对象的形式为:

I have an XML which has tags corresponding to three types of Java objects which would be created from the XML. The objects are of the form:

A
- static Map<String, A>
- String name
- String aInfo1
- String aInfo2

B
- static Map<String, B>
- String name
- String bInfo1
- String bInfo2

C
- A aObject
- B bObject

现在,在我的 XML 中,我为 A 对象和 B 对象定义了一个标签列表,然后我为 C 对象定义了标签,这些标签使用那里的名称字段引用 A 和 B 对象.我有两个要求:

Now, in my XML, I define a list of tags for A objects and B objects and then I define tags for C objects which refer to A and B objects using there name field. I have two requirements:

  1. 在 A 和 B 中填充静态映射,同时从 XML 中读取 A 和 B 对象.这些映射将分别包含 A.name 到 A 和 B.name 到 B 的映射.
  2. 通过从 XML 标记中读取 A.name 和 B.name 来填充 C 对象,然后使用 A 和 B 对象中定义的映射.

我已经阅读了一些 Java 框架,例如 JAXB,但是我无法想出一种方法来从我的 XML 创建这种类型的对象.是否有 Java 框架可以开箱即用或以最少的逻辑执行此操作?

I have read about some Java frameworks like JAXB but I am unable to come up with a way to create such type of objects from my XML. Is there a framework in Java which can do this out-of-the box or with minimum logic?

还有一个要求:我需要定义形式的 D 和 E 对象

There is another requirement: I need to define D and E objects of the form

D
- Map<A, E>

我将定义 E 对象,类似于在 web.xml 中定义 servlet 的方式,即首先定义 E 类的名称和类,然后在其他地方使用 E 的名称.此外,传递参数以实例化 E 对象.标签看起来像:

I would define E objects similar to how servlets are defined in web.xml i.e. first define the name and class for the E class and then use the name for E at some other place. Additionally, pass parameters to instantiate E objects. The tag would look like:

<E>
    <name>queryProcessor</name>
    <class>com.mydomain.QueryProcessor</class>
</E>

现在这将在 D 中定义 Map 的内容时使用

Now this would be used while defining content of Map in D

<D>
    <map>
        <A>name_of_some_A_object</A>
        <E name="queryProcessor">
            <param1>name_of_some_B_object</param1>
            <param2>name_of_some_B_object</param2>
        </E>
        <A>name_of_some_A_object</A>
        <E name="queryProcessor">
            <param1>name_of_some_B_object</param1>
            <param2>name_of_some_B_object</param2>
        </E>
     </map>
 </D>

本质上,D 中的映射将通过实例化一个基类型 E 的类来填充,该类使用传递给它的参数和一个由其名称引用的 A 的对象.

Essentially the map in D will be populated by instantiating a class of base type E with the parameters passed to it and an object of A, referred by its name.

推荐答案

  • 使用 XSD 指定正确的 XML 格式
  • 生成 JAXB 类
  • (如果您熟悉 JAXB 注释并希望使用 Java 而不是 XSD 来控制接口,您也可以反过来做).

    (You could do it also the other way around, if you are familiar with JAXB annotations and want to control the interface with Java rather than with an XSD).

    注意:静态地图很可能不是您想要使用的.如果您详细说明您要解决的问题,我们可能会为您指出一些替代方法

    Note: static Maps is most likely not what you want to use. If you explain more about what problem you want to solve we might be able to point you out some alternative ways

    您是在谈论 XML 的格式吗?或者为什么我需要 XML?我需要 XML 才能使我的应用程序在 Java 之外可配置.

    Are you talking about the format of the XML? Or why I need XML at all? I need XML for the ability to make my applications configurable outside of Java.

    看来您是在重新发明轮子.看看 Spring,看看它是否符合您的需求.如果不是,请解释原因.

    It looks like you're re-inventing the wheel. Have a look at Spring and see if it fits your needs. If it doesn't, explain why.

相关文章