如何使用ByteArrayOutputStream将XMLSerializer对象反序列化为输出

时间:2017-11-30 17:53:00

标签: android xml

我有一个这样的代码来制作序列化xml文件:

private byte[] bytes;
...
OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XmlSerializer newSerializer = Xml.newSerializer();
newSerializer.setOutput(byteArrayOutputStream, "utf-8");
newSerializer.startDocument("utf-8", null);
newSerializer.startTag(null, "playlist");
newSerializer.attribute(null, "version", "1.0");
...
put all my XML tags
...

newSerializer.endTag(null, "playlist");
newSerializer.endDocument();
this.bytes= byteArrayOutputStream.toByteArray();

我需要做什么:将此字节数组再次转换为XML文件,我不知道该怎么做!

1 个答案:

答案 0 :(得分:1)

  

您可以按如下方式更改现有的序列化和反序列化:

try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(newSerializer);
        ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        return (XmlSerializer) oin.readObject();
    } catch (Exception e) {
        throw new Exception("Exception occurred:" + e.getMessage(), e);
    }