I have got XML as following :
<Temp NAME="bobo">
<elem1 />
<elem2>
<Attri1 ID="123" />
</elem2>
<elem3>
<Attri2 MOM="9" ID="7" SSS="2" />
</elem3>
<elem3>
<Attri3 MOM="44" ID="4" DSC="First Test"/>
</elem3>
</Temp>
I'm using annotations to map to java classes :
@XmlRootElement(name = "Temp")
public class Temp {
@XmlElements({
@XmlElement(name="elem1",type=elem.class),
@XmlElement(name="elem2",type=elem.class)})
public ArrayList<elem> elms;
}
public class elem {
...
}
public class elem1 extends elem{
...
}
public class elem2 extends elem{
...
}
public class elem3 extends elem{
...
}
In each element there can be lots of attributes.
Can someone please help me how to annotate the java classes correctly ?
Let me sharpen my question. I need to build from this XML a java objects that will have 3 parameters : name of the first element(p1) , name of child element(p2), map values (p3).
The XML above will generate 4 java elem objects as following:
all java objects type is elem!
obj1 :
p1 = elem1
p2 = null
p3 = null
obj2 :
p1 = elem2
p2 = Attri1
p3 = map: key value
(ID, 123)
obj3 :
p1 = elem3
p2 = Attri2
p3 = map: key value
(MOM, 9)
(ID, 7)
(SSS, 2)
obj4 :
p1 = elem3
p2 = Attri3
p3 = map: key value
(MOM, 44)
(ID, 4)
(DSC, First Test)
Thanks in advance! Boris.
Well, you need to have a schema definition for your xml which covers all your cases. If indeed you need dynamic structure you need to build it in a different way with key & value pairs. For ex:
< Attribute >
<name >ID</name >
<value > 123</value >
OR
< Attribute name="ID" value="123" >
This will give you a collection of attributes for each element. This can be represented in your schema. Once you have the schema, you can generate or create your objects
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.