Hi!
I use XMLEncoder to encode some files in XML. I want to ignore transient fields like with the plain binary serialization. I use the following piece of code inspired of a tutorial of Sun Microsystems to do so:
/**
* force the XML encoder/decoder to ignore transient members like the binary serialization
* FIXME: it does not work properly with collections and arrays
* @param myClass
*/
public static final void forceHandlingOfTransientModifiersForXMLSerialization(Class<?> myClass){
BeanInfo beanInfo = null;
try{beanInfo=Introspector.getBeanInfo(myClass);}
catch(IntrospectionException ie)
{ie.printStackTrace();}
if(beanInfo!=null)
{PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();
String fieldName;
//for each non inherited declared field
for(Field field:myClass.getDeclaredFields())
//if this field is transient for binary serialization
if(Modifier.isTransient(field.getModifiers()))
{fieldName=field.getName();
for(PropertyDescriptor propertyDesc:propertyDescriptors)
if(propertyDesc.getName().equals(fieldName))
{//set this field to transient for the XML serialization too
propertyDesc.setValue("transient",Boolean.TRUE);
break;
}
}
}
}
It works fine except on collections and arrays. For example, if myClass has a field containing a list of image with the transient modifier, it attempts to encode this list
When I use the debug feature of Eclipse, I see that its property descriptor is found and modified. Do you have an idea? Thank you very much for your attention.