I have an attribute:
The associated getter method in the generated JAXB object is like this:
public String getUnits(){
if(units == null) return "metric";
else return units;
}
getUnits() is not being called by JAXB Marshaller when marshalling and the value is not being set. Why would this not be called?
schema.xsd
Below is a simplified version of the XML schema that you used to generate your Java classes:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified">
<element name="root">
<complexType>
<attribute name="units" fixed="metric"/>
</complexType>
</element>
</schema>
Root
This will result in a class like the following to be generated. Since @XmlAccessorType(XmlAccessType.FIELD)
is specified your JAXB (JSR-222) implementation will get the value form the field instead of accessing the getUnits()
method.
package org.example.schema;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {
@XmlAttribute(name = "units")
@XmlSchemaType(name = "anySimpleType")
protected String units;
public String getUnits() {
if (units == null) {
return "metric";
} else {
return units;
}
}
public void setUnits(String value) {
this.units = value;
}
}
For More Information