Page 1 of 1

Missing an @XmlRootElement annotation error

PostPosted: Mon Apr 30, 2012 8:39 pm
by cpnehete
Hi All,

I am working on manipulating word document using docx4j.

I have a word template having empty table with 4 columns in it. I want to access each cell in the table and add value to it from database.
While saving document to output path i am getting following error,

6033 [main] ERROR org.docx4j.openpackaging.io.SaveToZipFile - javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:197)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:175)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:245)
... 11 more
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:216)
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:103)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:664)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:146)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:116)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:304)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:61)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:103)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:561)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:290)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:103)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:664)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:146)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:116)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:304)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:61)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:103)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:664)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:306)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:561)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:290)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:462)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314)
... 16 more


Please find below my code snippet, (For time being i am accessing only one cell and trying to set string value in it)

Code: Select all
public class TableInDocument {

    public static void main(String[] args) throws Exception {
        String inputfilepath = "C:\\Users\\administrator\\My Documents\\Challenges\\Input\\TableDocument.docx";

        String outputfilepath = "C:\\Users\\administrator\\My Documents\\Challenges\\Output\\TableDocument_Output.docx";

        boolean save = true;

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));

        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        String xPath = "//w:tc[contains(w:p/w:r/w:t, ' ')]";

        List<Object> list = documentPart.getJAXBNodesViaXPath(xPath, false);

        for (Object o : list) {
            o = XmlUtils.unwrap(o);

            if (o instanceof Tc) {
                Tc tableCell = (Tc) o;
                tableCell.getEGBlockLevelElts().add("My String"); // Problem is here....
            }
        }

        if (save) {
            SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
            saver.save(outputfilepath);
            System.out.println("Saved updated template to:" + outputfilepath);
        }
}
}



I tried some things looking at similar topics in forums, also from getting started document, but still its not working for me.

Can anyone explain me this problem and help me to get rid of it? I am still at the beginning of docx4j

Thanks in advance.

c.

Re: Missing an @XmlRootElement annotation error

PostPosted: Mon Apr 30, 2012 11:05 pm
by sureshbabubv
Hi cpnehete,

I took a sample document as attached which is having a table of 3rows and 2 columns with some text data.

And made some modification in your code and tested the application is working fine. can you please check, that will helpful or not.


import java.util.List;

Code: Select all
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.P;
import org.docx4j.wml.Tc;

public class TableInDocument {

    public static void main(String[] args) throws Exception {
        String inputfilepath = "C:\\Users\\administrator\\My Documents\\Challenges\\Input\\TableDocument.docx";

        String outputfilepath = "C:\\Users\\administrator\\My Documents\\Challenges\\Output\\TableDocument_Output.docx";

        boolean save = true;

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));

        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        String xPath = "//w:tc[contains(w:p/w:r/w:t, ' ')]";

        List<Object> list = documentPart.getJAXBNodesViaXPath(xPath, false);
       
       
        for (Object o : list) {
            o = XmlUtils.unwrap(o);

            if (o instanceof Tc) {
               Tc tableCell = (Tc) o;
               
                List tableContent = tableCell.getContent();
               
                P paragraph = (P) tableCell.getContent().get((tableContent.size()-1));
               
                paragraph.getContent().add("MyString");
                 
              //   tableCell.getContent().get((tableContent-1)).add("MyString"); // Problem is here....
               
                System.out.println("tableContent size after adding: "+tableContent.size());
            }
        }

        if (save) {
            SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
            saver.save(outputfilepath);
            System.out.println("Saved updated template to:" + outputfilepath);
        }
}
}



Thanks & Regards,
B.V.Suresh Babu

Re: Missing an @XmlRootElement annotation error

PostPosted: Mon May 07, 2012 9:53 pm
by cpnehete
Hi sureshbabubv,

Thanks for your reply. I am sorry, i was away from work for some time.

I got the solution to my posted problem. Earliar, i was setting up "String" value directly into tableCell.getEGBlockLevelElts().add("My String");.

Now i am setting it as "org.docx4j.wml.Text", where String value is set to Text. And Its working :D .

Let me know your thoughts.


Thanks
c.