I’ve tried to blog before, and it never really works for me. I just don’t have enough interesting things to say. I do, however, occasionally run into solutions to problems that I want to save somewhere. Since I find so much help searching Google for others peoples solutions, I figured I should do the same.
So here’s my first try. How do you get XStream to act sanely when it comes to parsing generally unknown xml files? You override the wrapMapper and tell it to not freak out if a parameter isn’t found…
static XStream xstream = new XStream() {
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {
@Override
public boolean shouldSerializeMember(Class definedIn,
String fieldName) {
if (definedIn == Object.class) {
return false;
}
return super.shouldSerializeMember(definedIn, fieldName);
}
};
}
};
static {
xstream.alias("xml-read-devices", UdrXmlReadDevices.class);
xstream.alias("xml-write-device", UdrXmlWriteDevice.class);
xstream.alias("xml-confirm-confcode-device", UdrXmlConfirmConfCodeDevice.class);
xstream.addImplicitCollection(UdrXmlReadDevices.class, "device", UdrDevice.class);
xstream.addImplicitCollection(UdrServices.class, "service", UdrService.class);
xstream.useAttributeFor(UdrService.class, "flags");
xstream.useAttributeFor(UdrService.class, "serviceId");
xstream.useAttributeFor(UdrService.class, "type");
xstream.useAttributeFor(UdrXmlConfirmConfCodeDevice.class, "errorCode");
xstream.useAttributeFor(UdrXmlWriteDevice.class, "errorCode");
}