{"id":4987,"date":"2017-05-29T12:43:56","date_gmt":"2017-05-29T12:43:56","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4987"},"modified":"2017-05-29T12:43:56","modified_gmt":"2017-05-29T12:43:56","slug":"creating-java-classes-from-an-xml-schema","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-java-classes-from-an-xml-schema\/","title":{"rendered":"Creating Java classes from an XML schema"},"content":{"rendered":"<p>This is actually part of a set of three posts taking an XSD (XML Schema), generating the Java classes for it, then we&#8217;ll create a server using CXF that will support both JSON and XML responses, finally we&#8217;ll create a client that can switch between the two. <\/p>\n<p><strong>Step 1<\/strong><\/p>\n<p>Step 1 is the easy bit &#8211; so we have an xsd (we&#8217;ll use a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd489283.aspx\" target=\"_blank\">Sample XSD<\/a> recreated below)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;xsd:schema xmlns:xsd=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;   \r\n           xmlns:tns=&quot;http:\/\/tempuri.org\/PurchaseOrderSchema.xsd&quot;   \r\n           targetNamespace=&quot;http:\/\/tempuri.org\/PurchaseOrderSchema.xsd&quot;   \r\n           elementFormDefault=&quot;qualified&quot;&gt;  \r\n &lt;xsd:element name=&quot;PurchaseOrder&quot; type=&quot;tns:PurchaseOrderType&quot;\/&gt;  \r\n &lt;xsd:complexType name=&quot;PurchaseOrderType&quot;&gt;  \r\n  &lt;xsd:sequence&gt;  \r\n   &lt;xsd:element name=&quot;ShipTo&quot; type=&quot;tns:USAddress&quot; maxOccurs=&quot;2&quot;\/&gt;  \r\n   &lt;xsd:element name=&quot;BillTo&quot; type=&quot;tns:USAddress&quot;\/&gt;  \r\n  &lt;\/xsd:sequence&gt;  \r\n  &lt;xsd:attribute name=&quot;OrderDate&quot; type=&quot;xsd:date&quot;\/&gt;  \r\n &lt;\/xsd:complexType&gt;  \r\n  \r\n &lt;xsd:complexType name=&quot;USAddress&quot;&gt;  \r\n  &lt;xsd:sequence&gt;  \r\n   &lt;xsd:element name=&quot;name&quot;   type=&quot;xsd:string&quot;\/&gt;  \r\n   &lt;xsd:element name=&quot;street&quot; type=&quot;xsd:string&quot;\/&gt;  \r\n   &lt;xsd:element name=&quot;city&quot;   type=&quot;xsd:string&quot;\/&gt;  \r\n   &lt;xsd:element name=&quot;state&quot;  type=&quot;xsd:string&quot;\/&gt;  \r\n   &lt;xsd:element name=&quot;zip&quot;    type=&quot;xsd:integer&quot;\/&gt;  \r\n  &lt;\/xsd:sequence&gt;  \r\n  &lt;xsd:attribute name=&quot;country&quot; type=&quot;xsd:NMTOKEN&quot; fixed=&quot;US&quot;\/&gt;  \r\n &lt;\/xsd:complexType&gt;  \r\n&lt;\/xsd:schema&gt;  \r\n<\/pre>\n<p>I&#8217;ve saved this as sample.xsd. <\/p>\n<p>Now from a command prompt run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nxjc -p com.putridparrot.sample sample.xsd\r\n<\/pre>\n<p>XJC will generate Java source code in the package (-p) com.putridparrot.sample, so you should see four files. Two are our complexTypes, one is package-info.java which allows us to apply package level annotations and the final one is ObjectFactory.java which is a utility class\/factory for creating types and (as it says in the comments within the file) <\/p>\n<p><em>&#8220;allows you to programatically construct new instances of the Java representation for XML content&#8221;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is actually part of a set of three posts taking an XSD (XML Schema), generating the Java classes for it, then we&#8217;ll create a server using CXF that will support both JSON and XML responses, finally we&#8217;ll create a client that can switch between the two. Step 1 Step 1 is the easy bit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161],"tags":[],"class_list":["post-4987","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4987"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4987\/revisions"}],"predecessor-version":[{"id":5017,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4987\/revisions\/5017"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}