{"id":47,"date":"2005-12-04T17:37:30","date_gmt":"2005-12-04T22:37:30","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=47"},"modified":"2005-12-04T17:37:30","modified_gmt":"2005-12-04T22:37:30","slug":"xml-object-cereal","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/12\/04\/xml-object-cereal\/","title":{"rendered":"XML Object Cereal"},"content":{"rendered":"<p>You think that Java&#8217;s serialization mechanism has problems?  You want to store\/serialize an object as XML?  Well you can use Java&#8217;s XMLEncoder and XMLDecoder classes to serialize an object to XML and then read it back.  Want to see some code?  Well here it is:<\/p>\n<pre>\n\/\/ Encode\/Serialize bean to XML\nMyBean bean = new MyBean();\nbean.setName(&quot;Bill Gates&quot;);\nbean.setTitle(&quot;Chairman of the Board&quot;);\nXMLEncoder encoder = new XMLEncoder(\n\tnew FileOutputStream(filePathStr)\n);\nencoder.writeObject(bean);\nencoder.close();\n<\/pre>\n<p>In the above code block you instantiated a JavaBean and set the name and title properties.  To save the object instance you write out the object using the writeObject method of the XMLEncoder class.  How simple is that?<\/p>\n<p>To read in an object from an XML file, you use the XMLDecoder. Here is some sample code:<\/p>\n<pre>\n\/\/ Read in an object from an XML file\nXMLDecoder decoder = new XMLDecoder(\n\tnew FileInputStream(filePathStr)\n);\nMyBean bean = (MyBean)decoder.readObject();\ndecoder.close();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You think that Java&#8217;s serialization mechanism has problems? You want to store\/serialize an object as XML? Well you can use Java&#8217;s XMLEncoder and XMLDecoder classes to serialize an object to XML and then read it back. Want to see some code? Well here it is: \/\/ Encode\/Serialize bean to XML MyBean bean = new MyBean(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/47"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}