XStream只注册一个转换器

时间:2015-12-09 19:40:05

标签: java xml converter xstream

在我的序列化XML文件中,只保存了我的对象的一个​​属性,但应保存四个。我认为这是因为y XStream Object只注册了一个转换器,尽管他应该注册四个。

我的转换器都是单独运行的。我一个接一个地测试它们。

我的XML文件:

<object-stream>
  <model.Product>13</model.Product>
</object-stream>

应保存的我的产品类:

public class Product implements Externalizable, Serializable {

    private static final long serialVersionUID = -8437751114305532162L;


    @XStreamConverter(converter.NameConverter.class)
    private  SimpleStringProperty name;

    @XStreamConverter(converter.PriceConverter.class)
    private  SimpleDoubleProperty price;

    @XStreamConverter(converter.CountConverter.class)
    private  SimpleIntegerProperty quantity;

    @XStreamConverter(converter.IDConverter.class)
    private long id;

    public Product(String name, int quantity, double price, long id)
    {
        this.name=new SimpleStringProperty(name);
        this.quantity=new SimpleIntegerProperty(quantity);
        this.price=new SimpleDoubleProperty(price);
        this.id=id;

//Getter and Setter and implentation of Externalizable

我的XStream类

XStream xstream;
ObjectInputStream ois;
ObjectOutputStream oos;

@Override
    public void close() throws IOException {
        if (oos != null) {
            oos.close();
        }
        if (ois != null) {
            ois.close();
        }
    }


    @Override
    public void writeObject(Product obj) throws IOException {
        try {
            oos.writeObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void open(InputStream input, OutputStream output) throws IOException {
        xstream = createXStream(model.Product.class);
        converter.ConverterManager con=new ConverterManager();
        con.registerAllConverters(xstream);

        if (input != null) {
            if (input.available() > 0) {
                ois = xstream.createObjectInputStream(input);
            }
        }
        if (output != null) {
            oos = xstream.createObjectOutputStream(output);
        }
    }

}

我的ConverterManager:

import com.thoughtworks.xstream.XStream;

public class ConverterManager {
    public void registerAllConverters(XStream xstream)
    {
        xstream.aliasAttribute("Product Price", "price");
        xstream.registerConverter(new PriceConverter());

        xstream.aliasAttribute("Product ID", "id");
        xstream.registerConverter(new IDConverter());

        xstream.aliasAttribute("Product Name", "name");
        xstream.registerConverter(new NameConverter());

        xstream.aliasAttribute("Product quantity", "quantity");
        xstream.registerConverter(new CountConverter());


    }

}

我的writeObject,open和close方法是从这个方法从另一个类调用的:

private void saveModel() {

    XStreamStrategy s=new XStreamStrategy(); 
    try {
        s.open(getFilePath());
    } catch (IOException e) {

        e.printStackTrace();
    }
    for(fpt.com.Product p: model)
    {
        try {
            s.writeObject(p);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    try {

        s.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

0 个答案:

没有答案