Search code examples
docx4j

How can I detect org.docx4j.wml.instrText when parsing DOCX with docx4j?


I want to pars a docx file with docx4j library.

I need to detect org.docx4j.wml.instrText objects but actually it returns org.docx4j.wml.Text instead of org.docx4j.wml.instrText.

I found a solution that was working with older version of this library here:

http://www.docx4java.org/forums/docx-java-f6/can-i-just-don-t-load-contents-of-w-instrtext-into-text-t193.html

Actually this solution:

 ((javax.xml.bind.JAXBElement)((org.docx4j.wml.Text) o).getParent()).getName().getLocalPart()

But with the latest update it does not work. Could you please tell me what changes I have to make on this code?

Actually it make a type casting error that can not convert org.docx4j.wml.R to JAXBElement.

Thanks in advance.


Solution

  • The parent of the Text object, given by:

     ((org.docx4j.wml.Text) o).getParent()
    

    is, as the error says, a org.docx4j.wml.R, which you can't cast to JAXBElement.

    To identify your object in this case, try:

    ((javax.xml.bind.JAXBElement)o).getName().getLocalPart()