Page 1 of 1

Those pesky @XmlRootElement annotations

PostPosted: Tue Jan 26, 2010 10:53 am
by joshuajborn
Hello, everyone,

First, thank you for the docx4j library.

I'm using the library to generate Word documents based on data retrieved from an application's database, and the library is streamlining the process quite a bit.

One nuisance I've encountered is in creating tables. It seems a couple of the table-related nodes don't have the @XmlRootElement annotation and so generate an error when trying to save the document (and implicitly marshal the XML).

I saw that someone else in another thread (link below) ran into an issue with the missing @XmlRootElement annotations. In that case, there was a workaround since the marshaling was being done explicitly. However, in my case, the marshaling is being done when the WordprocessingMLPackage.save() call gets down to that particular element.

I realize that these classes org.docx4j.wml.* are generated automatically, and I am downloading the source so that I can add in the annotations and rebuild the library. Is there any quicker way to handle this case? It is a bit tedious to rebuild the library from source every time I encounter a new one of these.

Missing @XmlRootElement annotations; deepCopy (cloning) thread: http://dev.plutext.org/forums/viewtopic.php?f=6&t=206&start=0&hilit=annotation&sid=6a2cd4245f7348a3d340c669d02da4f0

Re: Those pesky @XmlRootElement annotations

PostPosted: Wed Jan 27, 2010 9:48 am
by jason
Hi Joshua

I don't have a general solution for you right now, but I am interested in looking into the particular case. Which table-related element isn't marshalling?

For anybody else who happens across this topic, as Joshua notes, there are workarounds if you are marshalling explicitly. See the Getting Started Guide, under Tips and Tricks > @XmlRootElement

cheers .. Jason

Re: Those pesky @XmlRootElement annotations

PostPosted: Thu Jan 28, 2010 5:03 am
by joshuajborn
So far I am encountering this with the TblPr and the TblGrid elements. See below.

Code: Select all
com.sun.istack.internal.SAXException2: unable to marshal type "org.docx4j.wml.TblPr" as an element because it is missing an @XmlRootElement annotation


Code: Select all
com.sun.istack.internal.SAXException2: unable to marshal type "org.docx4j.wml.TblGrid" as an element because it is missing an @XmlRootElement annotation

Re: Those pesky @XmlRootElement annotations

PostPosted: Thu Jan 28, 2010 6:47 am
by joshuajborn
Also, org.docx4j.wml.TblBorders seems to have the same problem.

Re: Those pesky @XmlRootElement annotations

PostPosted: Fri Jan 29, 2010 2:23 pm
by jason
Had a quick look at this.

TblPr already has the annotation:

Code: Select all
@XmlRootElement(name = "tblPr")
public class TblPr


so unless you are using code which predates that, it is odd that you'd get an error.

TblGrid doesn't have the annotation, but:

Code: Select all
      TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
      tbl.setTblGrid(tblGrid);


is marshalled ok in CreateWordprocessingMLDocument
(see TblFactory).

How are you creating your tblGrid?

cheers .. Jason

Re: Those pesky @XmlRootElement annotations

PostPosted: Sat Jan 30, 2010 1:06 pm
by joshuajborn
I noticed that, too, but didn't investigate further since I was distracted by my efforts to get the build from source to work.

Looking back, I found the issue was that I had a
Code: Select all
tbl.getEGContentRowContent().add(tblPr);
instead of a
Code: Select all
tbl.setTblPr(tblPr);
in my code. I don't have the code in front of me, but I'm guessing the tblGrid had a similar problem.

Oh, well, I guess the annotation wasn't the issue.

Lesson learned: when writing a lot of code for JAXB generated classes, pay attention to when to use the
Code: Select all
.getFoo.add(bar)
syntax and when to use the
Code: Select all
.setFoobar(foobar)
syntax.