如何从相互引用的XML标记创建Java对象? [英] How to create Java objects from XML tags which are referring each other?

查看:82
本文介绍了如何从相互引用的XML标记创建Java对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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对象定义了标签。使用name字段引用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. 在从XML读取A和B对象时填充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>

我会定义类似于web.xml中定义servlet的E对象,即首先定义名称和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中的地图内容

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.

推荐答案


  • 指定正确的XML格式一个XSD

  • 生成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.

    这篇关于如何从相互引用的XML标记创建Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆