XMLEncoder で XML に保存するオブジェクトのプロパティの中で XML に書き出したくないプロパティがある場合には ObjectOutputStream を使った直列化のようにフィールドを transient にするのではなくプロパティに対応する PropertyDescriptor オブジェクトを取得してそれに transient 属性を設定します。
下は任意のプロパティを書き出さないようにするサンプルです。 Test#setTransientProperty メソッドは Class オブジェクトとプロパティ名から対象プロパティを表現する PropertyDescriptor オブジェクトを取得し、その transient 属性を設定します。 プロパティを XML に書き出さないようにするには Boolean.TRUE を指定し、デフォルトの書き出す状態に戻す場合には Boolean.FALSE を指定します。
/******************** Test.java ********************/
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.XMLEncoder;
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException,
IntrospectionException {
Book book = new Book("星新一", "きまぐれロボット", 357);
writeObject(book, "book_1.xml");
setTransientProperty(Book.class, new String[]{"title", "price"},
Boolean.TRUE);
writeObject(book, "book_2.xml");
setTransientProperty(Book.class, new String[]{"title"},
Boolean.FALSE);
writeObject(book, "book_3.xml");
}
private static void writeObject(Object bean, String fileName)
throws FileNotFoundException {
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(fileName)));
encoder.writeObject(bean);
encoder.close();
}
public static void setTransientProperty(Class clazz,
String[] properties,
Boolean isTransient)
throws IntrospectionException {
BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors =
info.getPropertyDescriptors();
next:
for (int i = 0; i < properties.length; i++) {
for (int j = 0; j < propertyDescriptors.length; j++) {
PropertyDescriptor pd = propertyDescriptors[j];
if (properties[i].equals(pd.getName())) {
pd.setValue("transient", isTransient);
continue next;
}
}
}
}
}
/******************** Book.java ********************/
public class Book {
private String author;
transient private String title;
private int price;
public Book() {}
public Book(String author, String title, int price) {
this.author = author;
this.title = title;
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
/******************** book_1.xml ********************/
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_01" class="java.beans.XMLDecoder">
<object class="Book">
<void property="author">
<string>星新一</string>
</void>
<void property="price">
<int>357</int>
</void>
<void property="title">
<string>きまぐれロボット</string>
</void>
</object>
</java>
/******************** book_2.xml ********************/
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_01" class="java.beans.XMLDecoder">
<object class="Book">
<void property="author">
<string>星新一</string>
</void>
</object>
</java>
/******************** book_3.xml ********************/
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_01" class="java.beans.XMLDecoder">
<object class="Book">
<void property="author">
<string>星新一</string>
</void>
<void property="title">
<string>きまぐれロボット</string>
</void>
</object>
</java>