I would like to build some kind of object generation engine for my domain objects.
For example, lets assume, I’m working with graphs. The models are represented by xml and I should be able to load them and build a java representation at runtime.
Lets say, graph has vertices and edges
So it will look like this:
<graph>
<vertex id="n1" color="red", thickness="2">
<vertex id="n2">
<edge end1="${n1}", end2="${n2}"/>
</graph>
For this I would like to get the objects that can be described by the following java classes:
class Graph {
List<Vertex> vertexList
List<Edge> edgeList
}
class Vertex {
String id
... various properties ...
}
class Edge {
Vertex end1
Vertex end2
}
Another requirement is to be able to define vertices in loop like this:
<graph>
...
<for var = i, min = 1, max = 10, step = 1>
<vertex id=$i.../>
</for>
...
</graph>
I thought about using Apache Jelly but it seems to be a ‘dead’ project, JaxB doesn’t allow such a level of dynamic behavior AFAIK…
My question is – what framework can you recommend for implementing such a task?
If there is something that works like Apache Jelly but still maintained, it could be great also 🙂
Thanks a lot in advance
JAXB (JSR-222) implementations can easily handle references within a document using a combination of
@XmlIDand@XmlIDREF. I will demonstrate below with an example.JAVA MODEL
Graph
Vertex
In the
Vertexclass you need to use the@XmlIDannotation to indicate that theidfield is the id.Edge
In the
Edgeclass the@XmlIDREFannotation is used to indicate that the XML value contains a foreign key that references the real value.DEMO CODE
INPUT (input.xml)/OUTPUT
Below is the input to and output from running the demo code.
For More Information