This blog is the second part of previous blog, and we will cover more detailed problem here.
Convert Elements to Map
A very handy and useful feature in JAXB is the adapter. You can take some xml types and change them into your specific Java class.
Here, we can change a xml element like following to a map:
<A>
<B>1</B>
<C>2</C>
...
</A>
public class MyAdapter extends XmlAdapter<MyAdapter.AdaptedMap, Map<String, Integer> {
@XmlType(name = "A")
public static class AdaptedMap {
// represent the content in <A>...</A>
@XmlAnyElement
List<Element> elements = new ArrayList<>();
}
@Override
public Map<String,Integer> unmarshal(AdaptedMap adaptedMap) throws Exception {
HashMap<String,Integer> map = new HashMap<>();
for (Element e : adaptedMap.elements) {
map.put(e.getTagName(),
Integer.parseInt(e.getTextContent()));
}
return map;
}
...
}
Map XML Enum to Java
Examples from doc of javax.xml.bind.annotation.XmlEnumValue
:
@XmlEnum(String.class)
public enum Card { CLUBS, DIAMONDS, HEARTS, SPADES }
<!-- Example: XML Schema fragment -->
<xs:simpleType name="Card">
<xs:restriction base="xs:string">
<xs:enumeration value="CLUBS">
<xs:enumeration value="DIAMONDS">
<xs:enumeration value="HEARTS">
<xs:enumeration value="SPADES">
</xs:simpleType>
Convert enum in your xml into the enum in Java class is relative easy, you can just mark your java class with @XmlEnum
. If the name of you class and the type in xml, you may have to annotate your class with @XmlType
to clarify it.
@XmlEnum
public enum A {
a1, a2;
}
<xs:simpleType name="A" final="restriction">
<xs:restriction base="xs:string">
<xs:enumeration value="a1"/>
<xs:enumeration value="a2"/>
</xs:restriction>
</xs:simpleType>
NoSuchMethodError
java.lang.NoSuchMethodError: A.setXXX(I)V
In my case, JAXB will complain that my setter is not the default prototype. I sometimes use the builder setter template, which is convenient, but JAXB sometimes can recognize, sometimes can’t. It seems its bug, but I change builder pattern setter to default one fix the problem.
Adapter Is Not Applicable to xxx (primitive type)
I have an attribute which is similar to maxOccurs
, which can be number or some strings represent some special cases, so I define it like following (If you don’t understand how this works, you may refer to my xsd tutorial):
<xs:element name="max" default="all">
<xs:simpleType>
<xs:union memberTypes="xs:int">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="all"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
And following is my adapter:
public class SelectNumAdapter extends XmlAdapter<String, Integer> {}
@XmlAttribute
@XmlJavaTypeAdapter(value = SelectNumAdapter.class)
private int max;
However, the type we returned and the type of max
are not match, so the annotation checker will complain the mismatch.
Specifying the type makes code works:
@XmlAttribute
@XmlJavaTypeAdapter(type = int.class, value = SelectNumAdapter.class)
private int max;
Setter Not Called or Value Not Set
If you find your default value is not set, check the following possible problems:
- name of element/attribute is not match with class instance variable name
- lack annotation
@XmlElement/XmlAttribute
annotation or mark element with attribute or mark attribute with element - wrong setter format: had better use default setter prototype, not to use builder type like following
// had better not use this type
public A setX() {
}
// instead, use default one
public void setX() {
}
use="prohibited"
- if it is a collection, JAXB will call your setter only if your getter return null, i.e. you can’t init your collection with an empty one if you want your setter invoked.
Ref
Written with StackEdit.
评论
发表评论