我有这样的代码来制作序列化的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文件,我不知道该怎么做!

最佳答案


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);
    }

09-28 05:20