1.) I have an XSD file (I have no control over) that I converted to object model using JAXB
2.) I have a database extract in XML format. The XML element tag names are strictly the field names of the table
3.) I mapped the xml elements to the Java class using annotations.
Question: Is there a way to maintain the element names in the XSD file, and JUST extract the value of the xml elements.
JAXB annotated class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Item", propOrder = {
"code",
"name",
"price"
})
@XmlRootElement(name="inventory")
public class Item {
@XmlElement(name="catalog_num", required = true)
protected String code;
@XmlElement(name="catalog_descrip", required = true)
protected String name;
@XmlElement(name="prod_price")
protected double price;
public String getCode() {
return code;
}
//etc
An excerpt of the database xml file:
<?xml version="1.0"?>
<inventory>
<catalog_num>I001</catalog_num>
<catalog_descrip>Descriptive Name of Product</catalog_descrip>
<prod_price>11200</prod_price>
</inventory>
The result I need to get after marshaling the above xml file is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Item>
<code>I001</code>
<name>Descriptive Name of Product</name>
<price>11200.0</price>
</Item>
In the above code, I have tried annotating the methods instead of the fields, but I yield the same result. I just want the value extracted from the xml elements, but not change the element names.
I hope I am making sense.
Note: I’m the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
MOXy offers an extension where you can apply a second XML binding via an XML document. This binding document can either be used to add metadata, or when
xml-mapping-metadata-complete="true"completely replace the metadata supplied by the JAXB annotations on the Java model;The bindings file is passed as a parameter when creating the JAXBContext:
To solve your issue you could create one JAXBContext to handle the database document (using the annotated classes), and create a second JAXBContext to handle the result format using the MOXy binding file. Below is how this would look:
For a More Detailed Example See: