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:
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.
(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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With