{"id":12,"date":"2007-12-12T14:07:53","date_gmt":"2007-12-12T04:07:53","guid":{"rendered":"http:\/\/dev.plutext.org\/blog\/2007\/12\/12\/docx4j-branch-using-jaxb-to-unmarshall-ooxml-to-java\/"},"modified":"2008-01-08T12:24:24","modified_gmt":"2008-01-08T02:24:24","slug":"docx4j-branch-using-jaxb-to-unmarshall-ooxml-to-java","status":"publish","type":"post","link":"https:\/\/www.docx4java.org\/blog\/2007\/12\/docx4j-branch-using-jaxb-to-unmarshall-ooxml-to-java\/","title":{"rendered":"Docx4j branch: Using JAXB to unmarshall OOXML to Java"},"content":{"rendered":"<p>docx4j contains classes which represent key parts of a WordprocessingML docx.<\/p>\n<p>For example, we have a paragraph class to represent the p element; another to represent a run, etc.<\/p>\n<p>Each class knows how to unmarshall its docx XML representation, and marshall it again.<\/p>\n<p>It will create specialised objects for things it knows how to handle (for example the paragraph content collection contains run objects).  For XML we don&#8217;t have strongly typed objects for, the class will simply store that XML node, so that it can be round tripped.<\/p>\n<p>Instead of coding these classes one by one by hand, we wanted to see whether one of the Java-XML data binding frameworks could make our lives easier.<\/p>\n<p>Given there is a standard for doing this (JSR 222 &#8211; The Java Architecture for XML Binding), we tried the <a href=\"https:\/\/jaxb.dev.java.net\/\">JAXB reference implementation<\/a> (JAXB 2.1.5).<\/p>\n<p>The <a href=\"https:\/\/jaxb.dev.java.net\/\">JAXB web presence<\/a> leaves a lot to be desired.  I&#8217;ll write a post on that shortly.<\/p>\n<p>Having said that, I&#8217;m quite impressed with the spec and the reference implementation.<\/p>\n<p>You feed your schema into xjc, and it generates Java classes.<\/p>\n<p>The @XmlAnyElement annotation allows unknown elements to be round tripped, mimicking our existing code.<\/p>\n<p>Why would there be any unknown elements you ask?<\/p>\n<p>The answer is that we are using a subset of the <a href=\"http:\/\/www.ecma-international.org\/news\/TC45_current_work\/OfficeOpenXML-XMLSchema.zip\">wml.xsd schema from TC45<\/a>.  So there can be a lot of stuff in a docx document which falls outside the subset.<\/p>\n<p>There are a number of reasons we are using a subset:<\/p>\n<ol>\n<li>running the entire schema through XJC produces lots of errors, both in the parsing phase, and once you overcome those, in the compiling stage<\/li>\n<li>more importantly, we&#8217;re unlikely to ever implement the entire WordML spec. So it makes sense to work  with the subset of key features which are on our roadmap.<\/li>\n<li>you have to add annotations to the schema to ensure the resulting Java classes use names which make sense (this is called customizing the binding).<\/li>\n<\/ol>\n<p>Anyway, this approach seems to work well.  That is:<\/p>\n<ul>\n<li>the JAXB version can read a Word document, edit it, and save it again, and Word 2007 can consume the result.  See <a href=\"http:\/\/dev.plutext.org\/trac\/docx4j\/browser\/branches\/jaxb\/src\/main\/java\/org\/docx4j\/openpackaging\/samples\/Sample.java\">sample.java<\/a><\/li>\n<li>the resulting classes can be made quite intuitive (though there is more tweaking to do)<\/li>\n<li>unknown elements can be round-tripped<\/li>\n<\/ul>\n<p>The JAXB version of docx4J is in subversion at the following branch:<\/p>\n<p><a href=\"http:\/\/dev.plutext.org\/trac\/docx4j\/browser\/branches\/jaxb\" target=\"_blank\">http:\/\/dev.plutext.org\/trac<wbr><\/wbr>\/docx4j\/browser\/branches\/jaxb<\/a><\/p>\n<p>You can&#8217;t just check out the branch and use it right now, since<br \/>\nclasses need to be generated.  There are maybe 50  generated, but I have<br \/>\nonly <a href=\"http:\/\/dev.plutext.org\/trac\/docx4j\/browser\/branches\/jaxb\/target\/generated-sources\/org\/docx4j\/jaxb\/document\">committed 3<\/a>  of them.<\/p>\n<p>Where to from here?<\/p>\n<p>If this approach continues to look promising, we are likely to move the JAXB code into the trunk, and upgrade plutext-server and docx4all to use it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>docx4j contains classes which represent key parts of a WordprocessingML docx. For example, we have a paragraph class to represent the p element; another to represent a run, etc. Each class knows how to unmarshall its docx XML representation, and marshall it again. It will create specialised objects for things it knows how to handle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[15,16,13,17,14,12,18],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-docx4j","tag-docx","tag-jaxb","tag-ooxml","tag-schema","tag-wordml","tag-wordprocessingml","tag-xsd"],"_links":{"self":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}