{"id":945,"date":"2018-09-17T18:42:40","date_gmt":"2018-09-17T09:42:40","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=945"},"modified":"2018-12-23T01:52:55","modified_gmt":"2018-12-22T16:52:55","slug":"xmlencoder-writeobject","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/xmlencoder-writeobject","title":{"rendered":"Save objects with Java XMLEncoder"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java <strong>XMLEncoder<\/strong>to save the object,<strong>XMLDecoder<\/strong>Restore with<\/a><\/h2>\n<p>\n<strong>Java<\/strong>object with the<strong>XML format<\/strong>Here is a sample program that saves in<br \/>\nFor XML output, use the<strong>XMLEncoder<\/strong>to be used.<br \/>\nand<strong>XMLDecoder<\/strong>A sample program to restore a saved object is also presented at the same time using<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"1\">sample program<\/a><\/h2>\n<ul>\n<li>Fields declared private are preserved when Getter and Setter methods are defined.<\/li>\n<li>Only if there is a change in a variable, it is saved in an XML file.<\/li>\n<li>HashMap, java.util.ArrayList, etc. can also be specified as objects to be saved. ArrayList, etc. This is useful for storing the state of objects in a simple manner.<\/li>\n<\/ul>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * java.beans.XMLEncoder\u3092\u4f7f\u7528\u3057\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8[object]\u3092\u3001<br \/>\n * \u6307\u5b9a\u3057\u305f\u30d1\u30b9[path]\u306b\u3001XML\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u4fdd\u5b58\u3057\u307e\u3059\u3002<br \/>\n * \u203b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8[object]\u304cJavaBeans\u306e\u6163\u4f8b\u306b\u9069\u5408\u3057\u3066\u3044\u308b\u5834\u5408\u3001<br \/>\n * \u3000\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u30c7\u30fc\u30bf\u3082\u4fdd\u5b58\u3067\u304d\u307e\u3059\u3002<br \/>\n * \u203b\u4fdd\u5b58\u3057\u305fXML\u30d5\u30a1\u30a4\u30eb\u306fjava.beans.XMLDecoder\u3067\u5fa9\u5143\u3059\u308b\u4e8b\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n * @param path \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u30d1\u30b9\u3002<br \/>\n *              \u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u53ef\u80fd\u3067\u3042\u308c\u3070\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n * @param object \u4fdd\u5b58\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3002<br \/>\n * @throws FileNotFoundException<br \/>\n *                 \u6307\u5b9a\u3055\u308c\u305f\u30d1\u30b9\u540d\u3067\u793a\u3055\u308c\u308b\u30d5\u30a1\u30a4\u30eb\u304c\u958b\u3051\u306a\u304b\u3063\u305f\u5834\u5408<br \/>\n *\/<br \/>\npublic static synchronized void writeXML(String path, Object object)<br \/>\n        throws IOException {<br \/>\n    XMLEncoder enc = null;<br \/>\n    ByteArrayOutputStream out = new ByteArrayOutputStream();<br \/>\n    FileOutputStream fileStream = null;<br \/>\n    try {<br \/>\n        \/\/ \u5148\u306b\u30e1\u30e2\u30ea\u4e2d\u306bXML\u30c7\u30fc\u30bf\u3092\u51fa\u529b\u3059\u308b<br \/>\n        enc = new XMLEncoder(out);<br \/>\n        enc.writeObject(object);<br \/>\n        enc.close(); \/\/ close()\u306e\u4e2d\u3067flush()\u304c\u547c\u3070\u308c\u308b\u3002<br \/>\n        byte[] xmlbuff = out.toByteArray();<\/p>\n<p>        \/\/ Output byte data to file<br \/>\n        fileStream = new FileOutputStream(path);<br \/>\n        fileStream.write(xmlbuff);<br \/>\n        fileStream.flush();<br \/>\n    } finally {<br \/>\n        if (enc != null) {<br \/>\n            enc.close();<br \/>\n        }<br \/>\n        if (out != null) {<br \/>\n            out.close();<br \/>\n        }<br \/>\n        if (fileStream != null) {<br \/>\n            fileStream.close();<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n\/**<br \/>\n * Restores the object from the XML file at the specified path [path].<br \/>\n * *XML files must be saved using java.beans.XMLEncoder.<br \/>\n * @param path The path where the object is stored.<br \/>\n * @throws FileNotFoundException<br \/>\n * If the file indicated by the specified path name does not exist<br \/>\n *\/<br \/>\npublic static Object readXML(String path) throws FileNotFoundException {<br \/>\n    XMLDecoder d = null;<br \/>\n    try {<br \/>\n        d = new XMLDecoder(new BufferedInputStream(<br \/>\n                new FileInputStream(path)));<br \/>\n        return d.readObject();<br \/>\n    } finally {<br \/>\n        if (d != null) {<br \/>\n            d.close();<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n<\/textarea><\/p>\n<p>*The following class is used as a sample of a class to be saved.<br \/>\n<textarea readonly=\"readonly\" style=\"font-size: 13px;height: 250px\" class=\"src\" onclick=\"this.focus();this.select()\">public class FreeSoft {<br \/>\n    private String name;<br \/>\n    private String url;<br \/>\n    public String getName() {<br \/>\n        return name;<br \/>\n    }<br \/>\n    public void setName(String name) {<br \/>\n        this.name = name;<br \/>\n    }<br \/>\n    public String getUrl() {<br \/>\n        return url;<br \/>\n    }<br \/>\n    public void setUrl(String url) {<br \/>\n        this.url = url;<br \/>\n    }<br \/>\n}<br \/>\n<\/textarea><br \/>\n<\/p>\n<h2 class=\"common_title\"><a name=\"2\">Execution Result<\/a><\/h2>\n<p>\u25c6Example of Execution<br \/>\n<textarea readonly=\"readonly\" style=\"font-size: 13px;height: 250px\" class=\"src\" onclick=\"this.focus();this.select()\">public static void main(String[] args) {<br \/>\n    try {<br \/>\n        \/\/ FreeSoft\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057XML\u306b\u51fa\u529b<br \/>\n        FreeSoft freeSoft = new FreeSoft();<br \/>\n        freeSoft.setName(&#8220;Chat\uff06Messenger \u30c1\u30e3\u30c3\u30c8\u3082\u30e1\u30c3\u30bb\u30f3\u30b8\u30e3\u30fc\u3082\uff01\uff01&#8221;);<br \/>\n        freeSoft.setUrl(&#8220;https:\/\/chat-messenger.com\/&#8221;);<br \/>\n        writeXML(&#8220;C:\\\\FreeSoft.xml&#8221;,freeSoft);  <\/p>\n<p>        \/\/ Restore instance of FreeSoft class from XML<br \/>\n        FreeSoft freeSoft2 = (FreeSoft)readXML(\u201cC:\\\\FreeSoft.xml\u201d);<br \/>\n        System.out.println(\u201cSoft name:\u201d+freeSoft2.getName());<br \/>\n        System.out.println(\u201cURL:\u201d+freeSoft2.getUrl());<br \/>\n    } catch (Exception e) {<br \/>\n        e.printStackTrace();<br \/>\n    }<br \/>\n}<br \/>\n<\/textarea><br \/>\n\u25c6Output result<\/p>\n<pre class=\"console\">Software name: Chat &amp; Messenger Chat and Messenger!\r\nURL: https:\/\/chat-messenger.com\/\r\n<\/pre>\n<p>\u25c6C:\\FreeSoft.xml contents<\/p>\n<pre class=\"sample_src\"><object>\r\n  \r\n   Chat &amp; Messenger Chat and Messenger! \r\n  \r\n  \r\n   https:\/\/chat-messenger.com\/\r\n  \r\n <\/object> \r\n<\/pre>\n<h2 class=\"common_title\"><a name=\"3\">Points to keep in mind when using this service<\/a><\/h2>\n<p>XMLEncoder and java.beans.XMLDecoder classes, but in practical use with Chat&amp;Messenger, we have confirmed that in extremely rare cases, when outputting to XML with java.beans. XMLEncoder.<br \/>\nParseException is logged to \"Standard Error Output\" when a 0-byte file is read.<br \/>\n<br \/>\nThe reason for this is that the file becomes empty when the FileOutputStream instance is created.<br \/>\nI believe it occurs when the Java process is shut down after instantiating the FileOutputStream but before outputting the XML data.<br \/>\n<br \/>\nChat&amp;Messenger has created backup files to work around this problem.<\/p>","protected":false},"excerpt":{"rendered":"<p>Save objects with Java XMLEncoder and restore with XMLDecoder Save objects with Java XML [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/945"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=945"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"predecessor-version":[{"id":1880,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/945\/revisions\/1880"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}