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.
用来用去,还是觉得Castor是挺好的OXM的工具
OXM的几个实现你有没有比较过他们的性能?
性能没有特别比较,性能成瓶颈不大可能,主要还是扩展性。
基于Annotation最大的不好,除了M能力一般外,要转换成多种
格式的XML就不好办。吼吼~当然这只是偶的需求,
我们是先定死了xml schema,然后写java code,还好这两件事情都是我负责,所以变化在我掌控里,simple的annotation就搞定了
不过好像castor的mapping是更自由一些,以后就用castor了