How to create Java objects from XML tags that reference each other?

I have XML that has tags corresponding to the three types of Java objects that will be created from XML. Objects have 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 objects A and B, and then I define tags for objects C that refer to objects A and B using the name field. I have two requirements:

  • populates static maps in A and B when reading objects A and B from XML. Maps will contain a mapping of A.name to A and B.name to B, respectively.
  • populate C objects by reading A.name and B.name from the XML tag, and then using the maps defined in objects A and B.

Java, JAXB, XML. Java , ?

Edit:

: D E

D
- Map<A, E>

E, , web.xml. E, E - . , E. :

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

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, .

+5
3
  • XML XSD
  • JAXB

( , JAXB Java, XSD).

: , . , , .

Edit:

XML? XML ? XML Java.

, . Spring , . , , .

+3

, , , Spring, XML, XSTL, XML Spring XML.

Spring , , , . XML

<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>

, , . , ( setX), xml, "". .

+1

, XStream: http://x-stream.imtqy.com/

XML , ( Spring). , http://x-stream.imtqy.com/tutorial.html, XML , :

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

XStream XML, , http://x-stream.imtqy.com/alias-tutorial.html.

XStream , - . XStream As B XML, .

For an object C referencing A and B, you can read the following tutorial on object reference: http://x-stream.imtqy.com/graphs.html . If this does not suit you, you can always easily create another class to read the necessary information from XML using XStream, for example

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

And create instances of C using CInfo. Since you already have a name for the instance card A and B, this will be trivial.

+1
source

All Articles