{"id":703,"date":"2010-05-14T16:08:07","date_gmt":"2010-05-14T10:38:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=703"},"modified":"2010-06-09T16:06:10","modified_gmt":"2010-06-09T10:36:10","slug":"serializing-objects-to-xml","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/serializing-objects-to-xml\/","title":{"rendered":"Serializing objects to XML"},"content":{"rendered":"<p>XStream is a simple Java library to serialize objects to XML and back again.<br \/>\nSuppose we have a Person class as follows:<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n  public class Person {\r\n  private String name\r\n  private int age\r\n  private Address address\r\n}\r\n\r\npublic class Address {\r\n  private String street\r\n  private String city\r\n  private String state\r\n  private String country\r\n}\r\n <\/pre>\n<\/blockquote>\n<p>The following code will convert instance to XML:<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\nXStream xstream = new XStream();   \/\/ instantiate the XStream class\r\nxstream.alias(\"person\", Person.class);  \/\/the alias will be the root node.\r\n\r\nPerson personInstance = new Person()\r\n\/\/ populate the person object\r\n\r\nString xml = xstream.toXML(personInstance) <\/pre>\n<\/blockquote>\n<p>XML will look like :<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n<person>\r\n  <name><\/name>\r\n    <age><\/age>\r\n    <address>\r\n    \t<street><\/street>\r\n    \t<city><\/city>\r\n    \t<state><\/state>\r\n    \t<country><\/country>\r\n     <\/address>\r\n <\/person>\r\n <\/pre>\n<\/blockquote>\n<p>To reconstruct an object, from the XML:<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\nPerson person = (Person)xstream.fromXML(xml) <\/pre>\n<\/blockquote>\n<p>Hope this helpled!<\/p>\n<p>Cheers!<\/p>\n<p>Anshul Sharma<\/p>\n","protected":false},"excerpt":{"rendered":"<p>XStream is a simple Java library to serialize objects to XML and back again. Suppose we have a Person class as follows: public class Person { private String name private int age private Address address } public class Address { private String street private String city private String state private String country } The following [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/703"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=703"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/703\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}