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

后端 未结 3 974
臣服心动
臣服心动 2021-01-05 21:14

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<         


        
相关标签:
3条回答
  • 2021-01-05 21:42

    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.

    0 讨论(0)
  • 2021-01-05 21:46
    • 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.

    0 讨论(0)
  • 2021-01-05 21:49

    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.

    0 讨论(0)
提交回复
热议问题