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

余生长醉 提交于 2020-01-02 15:26:59

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/38076030/how-can-i-detect-org-docx4j-wml-instrtext-when-parsing-docx-with-docx4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!