Using MOXy (or any other java XML framework) is it possible to perform the following marshalling and unmarshalling between this xml and object:
<teacher>
<field name="Age">30</field>
<field name="Name">Bob</field>
<field name="Course">Math</field>
</teacher>
public class Teacher {
Field age;
Field name;
Field course;
}
public class Field {
String name;
String value;
}
There are solutions that work for marshalling (annotating all fields with @XmlElement(name= "field")) and there are some solutions that work for unmarshalling (@XmlPath("field[@name='Age']/text()") .
Is there however a solution that works both ways, or an approach that will unmarshal and marshal XML between these two formats?
Using JAXB's
xjctool to generate JAXB classes from an XML Schema yields:Teacher.java
Field.java
From this XML schema:
teacher.xsd
To address the comment "I wish to not unmarshal my XML into a list of Field but into their own distinct "age", "name" and "course" variables.", here is a revised XML schema to inject properties for "age", "name" and "course":
teacher.xsd (revised)
To generate the revised
Teacherclass, use:Teacher.java (revised)