跳至主要内容

JAXB Problem List (2)

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.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (