{"id":79,"date":"2012-04-03T15:56:06","date_gmt":"2012-04-03T14:56:06","guid":{"rendered":"http:\/\/www.flamingpenguin.co.uk\/blog\/?p=79"},"modified":"2012-04-03T15:56:06","modified_gmt":"2012-04-03T14:56:06","slug":"implement-tostring-method-on-domsource-when-marshalling-with-jaxb","status":"publish","type":"post","link":"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/04\/03\/implement-tostring-method-on-domsource-when-marshalling-with-jaxb\/","title":{"rendered":"Implement toString method on DOMSource when marshalling with JAXB"},"content":{"rendered":"<p>When serialising with JAXB to a DOMSource the object that results does not have a useful implementation of toString and alot of boiler plate is required to set up the marshalling. Here is some code that returns a DOMSource from an arbitrary JAXB annotated object with the toString method of the object return the XML representation:<\/p>\n<pre lang=\"java\">\r\npublic class SerialisationHelper\r\n{\r\n   private final String marshalledBean;\r\n   private final Document document;\r\n\r\n   private SerialisationHelper(final Object bean) throws JAXBException, ParserConfigurationException\r\n   {\r\n      document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();\r\n\r\n      final Marshaller marshaller = JAXBContext.newInstance(bean.getClass()).createMarshaller();\r\n      marshaller.marshal(bean, document);\r\n      final StringWriter writer = new StringWriter();\r\n      marshaller.marshal(bean, writer);\r\n      marshalledBean = writer.toString();\r\n   }\r\n\r\n   @Override\r\n   public String toString()\r\n   {\r\n      return marshalledBean;\r\n   }\r\n\r\n   private Document toDocument()\r\n   {\r\n      return document;\r\n   }\r\n\r\n   public static DOMSource marshalled(final Object entity)\r\n   {\r\n      try\r\n      {\r\n         final SerialisationHelper serialisationHelper = new SerialisationHelper(entity);\r\n         return new DOMSource(serialisationHelper.toDocument()) {\r\n            @Override\r\n            public String toString()\r\n            {\r\n               return serialisationHelper.toString();\r\n            }};\r\n      }\r\n      catch (final JAXBException e)\r\n      {\r\n         throw new RuntimeException(\"unable to marshal \" + entity, e);\r\n      }\r\n      catch (final ParserConfigurationException e)\r\n      {\r\n         throw new RuntimeException(\"unable to marshal \" + entity, e);\r\n      }\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When serialising with JAXB to a DOMSource the object that results does not have a useful implementation of toString and alot of boiler plate is required to set up the marshalling. Here is some code that returns a DOMSource from &hellip; <a href=\"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/04\/03\/implement-tostring-method-on-domsource-when-marshalling-with-jaxb\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,26],"tags":[],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-java","category-jaxb"],"_links":{"self":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}