Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize a XMLSerializer object with ByteArrayOutputStream as Output

Tags:

android

xml

I have a code like this to make a serialize xml file:

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

What i need to do: convert this byte Array into a XML file again and i don´t know how to do it!

like image 995
Rodrigo Gontijo Avatar asked Nov 08 '22 13:11

Rodrigo Gontijo


1 Answers

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);
    }
like image 138
Rakesh Avatar answered Nov 14 '22 23:11

Rakesh