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

拈花ヽ惹草 提交于 2019-11-30 21:15:10
  • 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.

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.

wyz

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.

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