Suppressing simple-xml's class attribute

Mon 08 February 2010
  • 手艺 tags:
  • java
  • xml published: true comments: true

Simple-xml is an object-xml serialization and de-serialization framework. It's featured by annotation-driven, light-weight and self-contained.

In simple-xml, pojos that will be serialized are annotated with @Root, @Element, @Attribute or @Text. The problem is, when handling with inheritance, if the actual type is different from declared type, simple-xml will add a "class" attribute for de-serialization consideration. That is, the parser will use this attribute to determine the schema of the xml element. However, this is of no useful when we do not parse xml by simple-xml. And there is also risk that the class attribute exposes our program's internal structure.

Take following code as example:

@Root
public class Response{
    @Element
    private Object entry;

// getter and setter ...
}

public class OrderItem{
@Element
private String name;

// getter and setter ...
} When you generate xml from classes above, you will have the entry element of response with an attribute "class", which value is the qualified class name, like "package.OrderItem".

This is done by simple-xml's Strategy interface. By default, the Persister uses TreeStrategy which has an implementation of setElement like:

/**
 * This is used to attach a attribute to the provided element
 * that is used to identify the class. The attribute name is
 * "class" and has the value of the fully qualified class
 * name for the object provided. This will only be invoked
 * if the object class is different from the field class.
 *
 * @param type this is the declared class for the field used
 * @param value this is the instance variable being serialized
 * @param node this is the element used to represent the value
 * @param map this is used to maintain contextual information
 *
 * @return this returns true if serialization is complete
 */
public boolean setElement(Type type, Object value, NodeMap node, Map map){
    Class actual = value.getClass();
    Class expect = type.getType();
    Class real = actual;
    if(actual.isArray()) {
        real = setArray(expect, value, node);
    }
    if(actual != expect) {
        node.put(label, real.getName());
    }
    return false;
}
This is where class attributed. So to suppress the attribute, we simply override this method by inherit TreeStrategy. I take a inline class for convenience here.
Serializer s = new Persister(new TreeStrategy(){
    @Override
    public boolean setElement(Type type, Object value, NodeMap node, Map map){
        return false;
    }
});
Now it works, however, simple-xml won't be able to deserialize xml generated by this modified strategy.