{"id":1158,"date":"2016-10-25T19:12:27","date_gmt":"2016-10-25T19:12:27","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=1158"},"modified":"2016-10-25T19:12:27","modified_gmt":"2016-10-25T19:12:27","slug":"fun-with-xml-jaxb-and-arrays","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/10\/25\/fun-with-xml-jaxb-and-arrays\/","title":{"rendered":"fun with XML &#8211; JAXB and arrays"},"content":{"rendered":"<p>In <a href=\"http:\/\/blog.paranoidprofessor.com\/index.php\/2016\/10\/21\/fun-with-xml-a-jaxb-example\/\">part I of my JAXB example<\/a> I briefly described some of the annotations required to turn normal Java objects into XML files with JAXB.<\/p>\n<p>To expand on the xml examples from my previous post,\u00a0I will be taking a number of small java objects and load them into an array. What makes this interesting is that the number of XML objects is not known nor all that important when loading or saving.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;\r\n&lt;Library&gt;\r\n    &lt;Publication&gt;\r\n        &lt;Author&gt;Robert Heinlein&lt;\/Author&gt;\r\n        &lt;Title&gt;Have spacesuit will travel&lt;\/Title&gt;\r\n        &lt;Price&gt;15.99&lt;\/Price&gt;\r\n        &lt;Isbn&gt;095524347&lt;\/Isbn&gt;\r\n    &lt;\/Publication&gt;\r\n    \r\n     ...\r\n\r\n    &lt;Publication&gt;\r\n        &lt;Author&gt;Robert Heinlein&lt;\/Author&gt;\r\n        &lt;Title&gt;Stranger in a strange land&lt;\/Title&gt;\r\n        &lt;Price&gt;15.99&lt;\/Price&gt;\r\n        &lt;Isbn&gt;0123123123&lt;\/Isbn&gt;\r\n    &lt;\/Publication&gt;\r\n&lt;\/Library&gt;\r\n<\/pre>\n<p>Actually, there isn&#8217;t really all that much more that needs to be explained. \u00a0The marshal and unmarshal\u00a0methods converts between xml and plain old java objects. \u00a0There is one slight difference.<\/p>\n<p>The\u00a0top level class is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. \u00a0The actual subobjects or subelements cannot have this tag.<\/p>\n<p>The bookcollection is the top class which is just an array of books which in this case is essentially the primitive.<\/p>\n<p>In my example, the bookcollection is perhaps not a clean separation between the actual xml object and the methods that use it. \u00a0All of the methods that are part of this class are not necessary. \u00a0The only important part of the class is the following lines.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage de.companyname.complex;\r\n\r\nimport javax.xml.bind.annotation.*;\r\nimport org.apache.log4j.Logger;\r\n\r\n@XmlRootElement(name=&quot;Library&quot;)\r\n@XmlAccessorType(XmlAccessType.FIELD)\r\n\r\npublic class bookcollection {\r\n\r\n\t@XmlElement(name=&quot;Publication&quot;)\r\n\tprivate book[] cardcatalog;\r\n\r\n\t...\r\n<\/pre>\n<p>The example is pretty straight forward to understand. \u00a0It loads the xml data and dumps it to the console.<\/p>\n<div id=\"code-link-1158\" class=\"sh-link code-link sh-hide\"><a href=\"#\" onclick=\"showhide_toggle('code', 1158, 'Show full code example(514 More Words)', 'Hide code'); return false;\" aria-expanded=\"false\"><span id=\"code-toggle-1158\">Show full code example(514 More Words)<\/span><\/a><\/div><div id=\"code-content-1158\" class=\"sh-content code-content sh-hide\" style=\"display: none;\"><\/p>\n<h2>bookcollection.java<\/h2>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage de.companyname.complex;\r\n\r\nimport javax.xml.bind.annotation.*;\r\nimport org.apache.log4j.Logger;\r\n\r\n@XmlRootElement(name=&quot;Library&quot;)\r\n@XmlAccessorType(XmlAccessType.FIELD)\r\n\r\npublic class bookcollection {\r\n\r\n\t@XmlElement(name=&quot;Publication&quot;)\r\n\tprivate book[] cardcatalog;\r\n\r\n\tpublic book[] getCatalog()\r\n\t{\r\n\t\treturn cardcatalog;\r\n\t}\r\n\tpublic void setCatalog(book cardCatalog[])\r\n\t{\r\n\t\tcardcatalog = cardCatalog;\r\n\t}\r\n\r\n\tpublic int itemCount()\r\n\t{\r\n\t\tint len = 0;\r\n\r\n\t\tif (cardcatalog == null)\r\n\t\t\tlen = 0;\r\n\t\telse\r\n\t\t\tlen = cardcatalog.length;\r\n\r\n\t\treturn len;\r\n\t}\r\n\r\n\tpublic book findBook(String title)\r\n\t{\r\n\t\tbook retval = null;\r\n\t\tLogger logfile = Logger.getLogger(bookcollection.class);\r\n\r\n\t\tlogfile.debug(&quot;looking for book&quot; );\r\n\t\tfor (int idx = 0; idx &amp;amp;amp;lt; cardcatalog.length; idx++)\r\n\t\t{\r\n\t\t\tlogfile.debug(cardcatalog[idx].getTitle() + &quot; &quot;);\r\n\t\t\tlogfile.debug(cardcatalog[idx].getAuthor() + &quot; &quot;);\r\n\t\t\tlogfile.debug(cardcatalog[idx].getPrice() + &quot; &quot;);\r\n\t\t\tlogfile.debug(cardcatalog[idx].getISBN() );\r\n\t\t\tlogfile.debug(&quot;&quot;);\r\n\r\n\t\t\tif (cardcatalog[idx].getTitle().equalsIgnoreCase(title) )\r\n\t\t\t{\r\n\t\t\t\treturn cardcatalog[idx];\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tlogfile.error(&quot;unable to locate publication '&quot; + title + &quot;' &quot;);\r\n\r\n\t\treturn retval;\r\n\t}\r\n}\r\n<\/pre>\n<h2>book.java<\/h2>\n<p>The book class has a main method which is completely unnecessary for the &#8220;libraryapplication&#8221;, it was added when I was testing the book object.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">package de.companyname.complex;\r\n\r\nimport java.io.File;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.OutputStream;\r\n\r\nimport javax.xml.bind.JAXBContext;\r\nimport javax.xml.bind.JAXBException;\r\nimport javax.xml.bind.Marshaller;\r\nimport javax.xml.bind.Unmarshaller;\r\nimport javax.xml.bind.annotation.*;\r\n\r\n@XmlAccessorType(XmlAccessType.FIELD)\r\npublic class book {\r\n\r\n\t\/\/ Microsoft Exchange values \r\n\t@XmlElement(name=&quot;Author&quot;)\r\n\tprivate  String author; \r\n\r\n\t@XmlElement(name=&quot;Title&quot;)\r\n\tprivate   String title; \r\n\r\n\t@XmlElement(name=&quot;Price&quot;)\r\n\tprivate   double price;\r\n\r\n\t@XmlElement(name=&quot;Isbn&quot;)\r\n\tprivate   String isbn;\r\n\r\n\tpublic book()\r\n\t{\r\n\t}\r\n\tpublic book(String author, String title, double price, String isbn)\r\n\t{\r\n\t\tthis.author = author;\r\n\t\tthis.title = title;\r\n\t\tthis.price = price;\r\n\t\tthis.isbn = isbn;\r\n\t}\r\n\r\n\tpublic String getAuthor()\r\n\t{\r\n\t\treturn author;\r\n\t}\r\n\tpublic void setAuthor(String value)\r\n\t{\r\n\t\tthis.author = value;\r\n\t}\r\n\r\n\tpublic String getTitle()\r\n\t{\r\n\t\treturn title;\r\n\t}\r\n\tpublic void setTitle(String value)\r\n\t{\r\n\t\tthis.title = value;\r\n\t}\r\n\r\n\tpublic double getPrice()\r\n\t{\r\n\t\treturn price;\r\n\t}\r\n\tpublic void setPrice(double value)\r\n\t{\r\n\t\tthis.price = value;\r\n\t}\r\n\r\n\tpublic String getISBN()\r\n\t{\r\n\t\treturn isbn;\r\n\t}\r\n\tpublic void setISBN(String value)\r\n\t{\r\n\t\tthis.isbn = value;\r\n\t}\r\n\r\n\tpublic void dump()\r\n\t{    \t\r\n\t\tJAXBContext jc = null;\r\n\t\ttry {\r\n\t\t\tjc = JAXBContext.newInstance(book.class);\r\n\t\t}\r\n\t\tcatch (JAXBException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\r\n\t\tMarshaller marshaller;\r\n\t\ttry {\r\n\t\t\tmarshaller = jc.createMarshaller();\r\n\t\t\tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);\r\n\t\t\tmarshaller.marshal(this, System.out);\r\n\t\t} \r\n\t\tcatch (JAXBException ex) \r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic book load(String filename)\r\n\t{\r\n\t\tbook loaded = new book();\r\n\r\n\t\tJAXBContext jc = null;\r\n\t\ttry {\r\n\t\t\tjc = JAXBContext.newInstance(book.class);\r\n\t\t\tUnmarshaller unmarshaller = jc.createUnmarshaller();\r\n\t\t\tFile xml = new File(filename);\r\n\t\t\tloaded = (book) unmarshaller.unmarshal(xml);\r\n\t\t}\r\n\t\tcatch (JAXBException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\r\n\t\treturn loaded;\r\n\t}\r\n\r\n\tpublic void save(String filename)\r\n\t{\r\n\t\tJAXBContext jc = null;\r\n\t\ttry {\r\n\t\t\tjc = JAXBContext.newInstance(book.class);\r\n\t\t}\r\n\t\tcatch (JAXBException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\r\n\t\tMarshaller marshaller;\r\n\t\ttry {\r\n\t\t\tmarshaller = jc.createMarshaller();\r\n\t\t\tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);\r\n\r\n\t\t\tOutputStream os = new FileOutputStream( filename );\r\n\t\t\tmarshaller.marshal( this, os );\r\n\t\t\tos.close();\r\n\t\t} \r\n\t\tcatch (JAXBException ex) \r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\t\tcatch (FileNotFoundException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t} catch (IOException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) throws Exception \r\n\t{\r\n\t\tbook single = new book ();\r\n\r\n\t\tsingle.setTitle(&quot;bible&quot;);\r\n\t\tsingle.setISBN(&quot;0000&quot;);\r\n\t\tsingle.setPrice(9.99);\r\n\t\tsingle.setAuthor(&quot;Jesus&quot;);\r\n\t\tsingle.dump();\r\n\r\n\t\tsingle.save(&quot;first.xml&quot;);\r\n\r\n\t\tbook second = single.load(&quot;first.xml&quot;);\r\n\t\tsecond.dump();\r\n\t}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>libraryapplication.java<\/h2>\n<p>The loadCatalog and saveCatalog methods do the necessary marshalling between the xml and the plain old java objects.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage de.companyname.complex;\r\n\r\nimport java.io.File;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.OutputStream;\r\n\r\nimport javax.xml.bind.JAXBContext;\r\nimport javax.xml.bind.JAXBException;\r\nimport javax.xml.bind.Marshaller;\r\nimport javax.xml.bind.Unmarshaller;\r\n\r\npublic class libraryapplication\r\n{\r\n\tprivate bookcollection cardcatalog = null; \r\n\r\n\tpublic void dump()\r\n\t{       \t\r\n\t\tJAXBContext jc = null;\r\n\t\ttry {\r\n\t\t\tjc = JAXBContext.newInstance(bookcollection.class);\r\n\t\t}\r\n\t\tcatch (JAXBException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\r\n\t\tMarshaller marshaller;\r\n\t\ttry {\r\n\t\t\tmarshaller = jc.createMarshaller();\r\n\t\t\tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);\r\n\t\t\tmarshaller.marshal(cardcatalog, System.out);\r\n\t\t} \r\n\t\tcatch (JAXBException ex) \r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void loadCatalog(String filename)\r\n\t{\r\n\t\tFile file = new File(&quot;.\/&quot;,filename);\r\n\r\n\t\tif (file.exists() == true)\r\n\t\t{\r\n\t\t\tJAXBContext jaxbContext = null;\r\n\t\t\tUnmarshaller jaxbUnmarshaller = null;\r\n\t\t\ttry {\r\n\t\t\t\tjaxbContext = JAXBContext.newInstance(bookcollection.class);\r\n\t\t\t\tjaxbUnmarshaller = jaxbContext.createUnmarshaller();\r\n\t\t\t\tcardcatalog = (bookcollection) jaxbUnmarshaller.unmarshal(file);\r\n\t\t\t} \r\n\t\t\tcatch (JAXBException e) \r\n\t\t\t{\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t\tcardcatalog = new bookcollection();\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tcardcatalog = new bookcollection();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void saveCatalog(String filename)\r\n\t{\r\n\t\tFile file = new File(&quot;.\/&quot;,filename);\r\n\t\tJAXBContext jc = null;\r\n\t\ttry {\r\n\t\t\tjc = JAXBContext.newInstance(bookcollection.class);\r\n\t\t}\r\n\t\tcatch (JAXBException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\r\n\t\tMarshaller marshaller;\r\n\t\ttry {\r\n\t\t\tmarshaller = jc.createMarshaller();\r\n\t\t\tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);\r\n\r\n\t\t\tOutputStream os = new FileOutputStream( file );\r\n\t\t\tmarshaller.marshal( cardcatalog, os );\r\n\t\t\tos.close();\r\n\t\t} \r\n\t\tcatch (JAXBException ex) \r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t}\r\n\t\tcatch (FileNotFoundException ex)\r\n\t\t{\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\tex.printStackTrace();\r\n\t\t} catch (IOException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void createCatalog(String filename)\r\n\t{\r\n\t\tcardcatalog = new bookcollection();\r\n\r\n\t\tbook list[] = new book[1];\r\n\t\tlist[0] = new book(&quot;Robert Heinlein&quot;,&quot;Stranger in a strange land&quot;,15.99, &quot;0123123123&quot;);\r\n\r\n\t\tcardcatalog.setCatalog(list);\r\n\t\tsaveCatalog(filename);\r\n\t}\r\n\r\n\t\/**\r\n\t * @param args\r\n\t *\/\r\n\tpublic static void main(String[] args) \r\n\t{\r\n\t\tString libraryName = &quot;US-LibraryOfCongres.xml&quot;;\r\n\t\tlibraryapplication libraryofcongress = new libraryapplication();\r\n\r\n\t\t\/\/ create a list\r\n\t\tlibraryofcongress.createCatalog(&quot;deutsche-bibliothek.xml&quot;);\r\n\t\tlibraryofcongress.dump();\r\n\r\n\t\t\/\/ create a list\r\n\t\tlibraryofcongress.createCatalog(&quot;britsh-library.xml&quot;);\r\n\t\tlibraryofcongress.dump();\r\n\r\n\t\t\/\/ load a list\r\n\t\tlibraryofcongress.loadCatalog(libraryName);\r\n\t\tlibraryofcongress.dump();\r\n\t}\r\n}\r\n<\/pre>\n<p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In part I of my JAXB example I briefly described some of the annotations required to turn normal Java objects into XML files with JAXB. To expand on the xml examples from my previous post,\u00a0I will be taking a number &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/10\/25\/fun-with-xml-jaxb-and-arrays\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[73,12],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1158"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":16,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"predecessor-version":[{"id":1631,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1158\/revisions\/1631"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}