{"id":312,"date":"2003-03-13T23:02:54","date_gmt":"2003-03-14T03:02:54","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=312"},"modified":"2003-03-13T23:02:54","modified_gmt":"2003-03-14T03:02:54","slug":"jaxb","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/03\/13\/jaxb\/","title":{"rendered":"JAXB"},"content":{"rendered":"<p>Thursday, developer meeting day at <a href=\"http:\/\/www.mindseye.com\/\">MINDSEYE<\/a>.  We all put our feet up on the conference table, sip on a <a href=\"http:\/\/www.mikeshardlemonade.com\/\">Mike&#8217;s<\/a> or a <a href=\"http:\/\/www.guinness.com\/\">Guinness<\/a> and &#8216;interface&#8217;.  Today <a href=\"http:\/\/www.magrathea.com\/~mch\/\">Maia<\/a> did an impromptu presentation on <a href=\"http:\/\/java.sun.com\/xml\/jaxb\/\">JAXB<\/a>, a Java technology from Sun &#8220;<i>&#8230;automates the mapping between XML documents and Java objects.<\/i>&#8221;  Sounds pretty boring doesn&#8217;t it?  Stop reading then.<\/p>\n<p>So I&#8217;ve written a couple apps that use Java and alot of applications that use XML. IMNSHO, the most tedious programming I&#8217;ve ever done is the work I&#8217;ve done parsing, validating, and hacking at XML.  XML is a boon to developers, but who really wants to write &#8216;lElements.item(0).text&#8217; over and over again?  Not many people I know. Anyway, JAXB.  JAXB takes the tedium out of using XML and for that reason alone is a great tool.  In short, you use JAXB to:<\/p>\n<ul>\n<li>unmarshal XML content into a Java representation\n<li>access, update and validate the Java representation against schema constraint\n<li>marshal the Java representation of the XML content into XML content\n<\/ul>\n<p>In non-geek, that means that you can hand an XML document to a Java system that uses JAXB, the developer writes about 3 lines of code to transform that XML document into a Java object, the developer can then pass that Java object around in his or her system, and then at some point in time easily transform that Java object back into XML.  No messing with childNode() or GetElementsByTagName().  Beautiful.  But it gets better.  JAXB creates Java objects for you that represent the XML document you pass in. These Java objects are created by JAXB with accessor methods so that you can modify the contents of the XML document&#8230;without knowing any XML syntax.  So if you had an xml document that looked like this:<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<br \/>\n&lt;application&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;caching bCache=&#8221;false&#8221; objectttl=&#8221;0&#8243;&gt;<br \/>\n&lt;\/application&gt;<\/p>\n<p>you&#8217;d get a Java class called &#8216;caching&#8217; with getters and setters for the bCache and objectttl properties. You could pass in the above XML document and modify the settings in 5 lines of code (pseudo code, not tested or compiled, use at your own risk):<\/p>\n<p>JAXBContext jc = JAXBContext.newInstance( &#8220;primer.po&#8221; );<br \/>\nUnmarshaller u = jc.createUnmarshaller();<br \/>\ncaching c = (caching)u.unmarshal( new FileInputStream( &#8220;config.xml&#8221; ) );<br \/>\nc.setBCache=&#8221;true&#8221;;<br \/>\nc.setObjectttl=&#8221;36000&#8243;;<\/p>\n<p>not so bad is it? For the sake of getting you hooked, I neglected to mention the fact that for every type of XML document that you want to use, you first have to create an <a href=\"http:\/\/java.sun.com\/webservices\/docs\/1.1\/tutorial\/doc\/JAXBworks3.html#wp98580\">XML Schema Document<\/a>, but hey, you&#8217;re lazy right?  That&#8217;s what makes you a good programmer.  <\/p>\n<p>Related links:<\/p>\n<p>Binding XML Schema to Java Classes with JAXB: <a href=\"http:\/\/java.sun.com\/webservices\/docs\/1.1\/tutorial\/doc\/JAXBworks.html#wp100322\">java.sun.com tutorial<\/a><br \/>\nJAXB FAQ: [<a href=\"http:\/\/java.sun.com\/xml\/jaxb\/faq.html\">link<\/a>]<br \/>\nThe JAXB API:  [<a href=\"http:\/\/www.xml.com\/pub\/a\/2003\/01\/08\/jaxb-api.html\">xml.com article<\/a>]<br \/>\nJAXB Mailing List [<a href=\"http:\/\/archives.java.sun.com\/jaxb-interest.html\">java.sun.com<\/a>]<br \/>\nDeveloping with JAXB and Ant: [<a href=\"http:\/\/www.onjava.com\/pub\/a\/onjava\/2002\/03\/06\/jaxant1.html\">onjava.com<\/a>]<br \/>\nGenerate XML Mapping Code with JAXB: [<a href=\"http:\/\/www.devx.com\/Java\/Article\/10904\">devx.com<\/a>]<br \/>\nBrett McLaughlin on JAXB: [<a href=\"http:\/\/newinstance.com\/mt-blogs\/archives\/000012.html\">newinstance.com<\/a>], note: adding Brett to blogroll. Brett wrote <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596001975\/cephasnet-20\/\">Java &amp; XML: Solutions to Real-World Problems<\/a>, <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596003986\/cephasnet-20\/\">Building Java Enterprise Applications Vol. II: Web Applications<\/a>, and <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596002785\/cephasnet-20\/\">Java and XML Data Binding<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thursday, developer meeting day at MINDSEYE. We all put our feet up on the conference table, sip on a Mike&#8217;s or a Guinness and &#8216;interface&#8217;. Today Maia did an impromptu presentation on JAXB, a Java technology from Sun &#8220;&#8230;automates the mapping between XML documents and Java objects.&#8221; Sounds pretty boring doesn&#8217;t it? Stop reading then. &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/03\/13\/jaxb\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JAXB<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/312"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=312"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/312\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}