用于简单xml的自定义转换器无法正常工作

时间:2013-07-09 22:14:58

标签: java simple-framework

我正在尝试在Simple XML中为byte []编写自定义转换器。它工作的一半意味着调用write方法而不是read。有人能指出为什么吗?

这是我的简单xml带注释的对象

@Root
public class Device implements Serializable
{

    private final static long serialVersionUID = 1L;
    @Element
    @Convert(ByteArrayConverter.class)
    protected byte[] imageRef;

    public byte[] getImageRef() {
    return imageRef;
    }

    public void setImageRef(byte[] imageRef) {
    this.imageRef = imageRef;
    }

这是我的自定义转换器

public class ByteArrayConverter implements Converter<byte[]>
{


    @Override
    public byte[] read(InputNode node) throws Exception 
    {            
         //put a break point here BUT Code not getting here
           ("I am here in read")
           String s = node.getValue();
           return Base64.decode(s);
    }

    @Override
    public void write(OutputNode node, byte[] byteArray) throws Exception 
    {       
        ("I am here in write")  
            node.setValue(Base64.encode(byteArray)) 
    }

这是我的序列化/反序列化代码

Registry registry = new Registry();
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);
try
{
    registry.bind(byte[].class, ByteArrayConverter.class);
} catch (Exception e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Device device = new Device();
device.setImageRef(new byte[]{1,2,3});

File file = new File("myDevice.xml");       
serializer.write(device, file); 

Device readDevice = serializer.read(Device.class, file);

当我在调试器中执行此操作时,我确实看到调试器在write方法中停止,但是它不会在 read()方法中停止,因此我没有得到预期的结果。当我们构造对象时,这个代码没有停止读取的任何原因???感谢

0 个答案:

没有答案
相关问题