XStream xstream = new XStream() {
/* (non-Javadoc)
* @see com.thoughtworks.xstream.XStream#wrapMapper(com.thoughtworks.xstream.mapper.MapperWrapper)
*/
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
final AtomicBoolean isListTypeOccurs = new AtomicBoolean(false);
MapperWrapper capitalizedElementNameWrapper = new MapperWrapper(next) {
/* (non-Javadoc)
* @see com.thoughtworks.xstream.mapper.MapperWrapper#serializedClass(java.lang.Class)
*/
@Override
public String serializedClass(Class type) {
return isListTypeOccurs.get() ? "Row" : type.getSimpleName();
}
/* (non-Javadoc)
* @see com.thoughtworks.xstream.mapper.MapperWrapper#serializedMember(java.lang.Class, java.lang.String)
*/
@Override
public String serializedMember(Class type, String memberName) {
if(!isListTypeOccurs.get()) {
Field f = null;
try {
f = type.getDeclaredField(memberName);
} catch (Exception e) {
}
if(f !=null && (Collection.class.isAssignableFrom(f.getType()) || f.getType().isArray()) ) {
isListTypeOccurs.getAndSet(true);
return "Detail";
}
}
return StringUtils.capitalize(memberName);
}
};
return capitalizedElementNameWrapper;
}
};
XStreamXMLReader r = new XStreamXMLReader();
try {
r.intialize();
r.setProperty(SaxWriter.CONFIGURED_XSTREAM_PROPERTY, null);
} catch (Exception e) {
e.printStackTrace();
}
ExecutionContext exc = smooks.createExecutionContext();
Stack<XMLReader> readers = new Stack<XMLReader>();
readers.add(r);
exc.setAttribute(XMLReader.class, readers);
转载于:https://my.oschina.net/frankies/blog/104417