Page 1 of 1

Beginner Help: Persistent JAXBException

PostPosted: Tue Aug 07, 2018 2:04 am
by ClearlyaPseudonym
Hi All,

I am having an issue importing word documents, and haven't found any documentation of others with this issue.

The Issue: Calling WordprocessingMLPackage.load(File anyFile) throws the following exception when the file to be loaded was originally created in Word:
Exception in thread "main" java.lang.RuntimeException: javax.xml.bind.JAXBException: class org.docx4j.openpackaging.packages.WordprocessingMLPackage nor any of its super class is known to this context.

Full error trace:
Code: Select all
Exception in thread "main" java.lang.RuntimeException: javax.xml.bind.JAXBException: class org.docx4j.openpackaging.packages.WordprocessingMLPackage nor any of its super class is known to this context.
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:844)
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:691)
   at warBuilder.WARFactory.readFile(WARFactory.java:73)
   at warBuilder.WARFactory.main(WARFactory.java:118)
Caused by: javax.xml.bind.JAXBException: class org.docx4j.openpackaging.packages.WordprocessingMLPackage nor any of its super class is known to this context.
   at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:593)
   at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)
   at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
   at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:256)
   at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116)
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:831)
   ... 3 more


Attempted Debugging: In an attempt to rule out error sources, I successfully created a test word document, saved it as a file, and opened it with the same call. This shows that, at least in theory, I properly configured my environment and have the appropriate dependencies. Code follows for this:
Code: Select all
private void createTestDoc() throws Docx4JException {
      WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
      mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
      mainDocumentPart.addParagraphOfText("Welcome To a document");
      File exportFile = new File(LOCAL_TESTING_SAVE_PATH+"\\welcome.docx");
      wordPackage.save(exportFile);
   }
   
   private void readTestDoc() throws JAXBException, Docx4JException {
      File doc = new File(LOCAL_TESTING_SAVE_PATH+"\\welcome.docx");
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
      MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
      System.out.println(mainDocumentPart.getXML());
      String textNodesXPath = "//w:t";
      List<Object> textNodes= mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
      for (Object obj : textNodes) {
          Text text = (Text) ((JAXBElement) obj).getValue();
          String textValue = text.getValue();
          System.out.println(textValue);
      }
   }


Tl;dr For some reason, I can open documents created with docx4java, but when I try to open up any document originally created in Word, things go pear-shaped. Any assitance in figuring out why this is happening would be greatly appreciated.

Attachments: In case it helps, I am attaching a basic word document that I tried and failed to load. Perhaps my version of Office is adding something docx4java can't parse?

Re: Beginner Help: Persistent JAXBException

PostPosted: Wed Aug 08, 2018 9:37 am
by jason
What does warBuilder.WARFactory.readFile(WARFactory.java:73) do?

It looks like it is passing the WordprocessingMLPackage object to marshaltoString.

That's not going to work. The WordprocessingMLPackage object holds a number of "parts" (each corresponding to a file in a docx when you unzip it), some of which are JaxbXmlParts (ie basically the XML parts).

Each of these JaxbXmlParts contains a content object which is unmarshalled from the XML, and capable of being marshalled.