Serializing and De-serializing a Java object to XML

0

A Java object can be serialized as XML using the XMLEncoder

try { XMLEncoder enc= new XMLEncoder(new BufferedOutputStream( new FileOutputStream("<output xml file>"))); enc.writeObject(instance);//instance is the bean object to be serialized enc.close(); } catch (FileNotFoundException e) { }


De-serializing the XML file can be done as follows using the XMLDecoder


try { XMLDecoder dec= new XMLDecoder(new BufferedInputStream( new FileInputStream("<input xml file>"))); MyClass o = (MyClass)dec.readObject(); dec.close(); } catch (FileNotFoundException e) { }

Read more »

0 comments:

Post a Comment

© Zone817. Powered by Blogger.