gpt4 book ai didi

android - 如何使用 ByteArrayOutputStream 作为输出反序列化 XMLSerializer 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:03 25 4
gpt4 key购买 nike

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

最佳答案

You can change your existing serialisation and deserialisation as follows:

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

关于android - 如何使用 ByteArrayOutputStream 作为输出反序列化 XMLSerializer 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com