How to create Java objects from XML tags which are referring each other?

为君一笑 提交于 2020-01-21 05:05:07

问题


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

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. populate static maps in A and B while reading the A and B objects from XMLs. The maps will contain a mapping of A.name to A, and B.name to B respectively.
  2. populate C objects by reading the A.name and B.name from XML tag and then using the maps defined in A and B objects.

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?

Edit:

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

D
- Map<A, 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>

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>

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.


回答1:


  • Specify the correct XML format with an XSD
  • Generate the JAXB classes

(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

Edit:

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.

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.




回答2:


From what you describe this could be done with a common framework such as Spring, either by you changing your XML or generate a XSTL which creates a Spring XML config file from your XML.

Spring Core documentation is probably enough to get you started. An example of the XML would be

<bean id="beanOneId" class="the.bean.Class">
    <property name="someProperty" value="staticValue">
    <property name="someOtherProperty" ref="beanTwoId">
</bean>
<bean id="beanTwoId" class="the.otherbean.Class">
    <property name="someOtherProperty" ref="beanOneId">
    <property name="someOtherProperty" ref="beanThreeId">
</bean>

But what you describe should not be too hard with reflection. Assuming that none of the other objects need a reference in the constructor (but rather as setX) I would start by scanning the xml, create and store all objects with their names and remember a list of "connections" so be made. After all objects are created do all connections in the connection list.




回答3:


Maybe you want to have a look at XStream: http://x-stream.github.io/

It is a good library to marshal and unmarshal objects to and from XML, and does nothing more (unlike Spring). Like example in http://x-stream.github.io/tutorial.html, You can load objects from XML as easy as:

Person newJoe = (Person)xstream.fromXML(xml);

In order for XStream to understand your XML, you need to setup some alias like http://x-stream.github.io/alias-tutorial.html described.

XStream alone will not be able to solve your problem completely, mainly due to the two static maps. However you can let XStream to load a list of As and Bs from the XML, then build the two maps from the list.

For object C referencing A and B, you can read the following tutorial talking about object reference: http://x-stream.github.io/graphs.html. If it does not suit your need, you can always easily build another Class to read necessary information from XML using XStream, like

public class CInfo {
    public String aName;
    public String bName
}

And construct C instances using CInfo. Given you already have name to instance map of A and B, it will be trivial.



来源:https://stackoverflow.com/questions/8358791/how-to-create-java-objects-from-xml-tags-which-are-referring-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!