使用Docx4j从Docx文件读取文本

时间:2018-08-24 13:32:27

标签: java ms-word docx4j

嗨,我正在尝试使用Docx4j从一个Docx文件读取数据,因此我可以将其插入到另一个文件中。我的代码如下:

public void getTextFromOtherFile() throws Docx4JException, JAXBException {
    File doc = new File("D:\\helloWorld.docx");
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
    MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
    System.out.println(mainDocumentPart.getXML());
}

代码工作正常。但是,只要在上述方法的末尾插入以下行,该方法就会开始生成异常:

List<Object> jAXBNodesViaXPath = mainDocumentPart.getJAXBNodesViaXPath("//w:t", true);

我面临的异常如下:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/osses] threw exception [javax.servlet.ServletException: java.lang.NoSuchMethodError: org.apache.xml.utils.DefaultErrorHandler.<init>(Z)V] with root cause

java.lang.NoSuchMethodError:org.apache.xml.utils.DefaultErrorHandler。(Z)V     在org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl。(TransformerIdentityImpl.java:1459)     在org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl。(TransformerIdentityImpl.java:96)     在org.docx4j.utils.XmlSerializerUtil.serialize(XmlSerializerUtil.java:18)     在org.docx4j.XmlUtils.w3CDomNodeToString(XmlUtils.java:833)     在org.docx4j.XmlUtils.xpath(XmlUtils.java:1206)     在org.docx4j.XmlUtils.xpath(XmlUtils.java:1200)     在org.docx4j.XmlUtils.getJAXBAssociationsForXPath(XmlUtils.java:1191)     在org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:1133)     在org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.getJAXBNodesViaXPath(JaxbXmlPartXPathAware.java:190)     在Business.SelectionBean.getTextFromOtherFile(SelectionBean.java:1087)     在org.apache.jsp.test_jsp._jspService(test_jsp.java:95)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在Business.NoCacheFilter.doFilter(NoCacheFilter.java:36)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)     在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:637)     在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:745)

我的word文件包含两行,如下所示: 您好,我叫abcd。 我喜爱我的工作。 从之前的7天开始,我一直处于这种状态,但无法解决此问题。我还包含了不同的jar文件(Xalan,xml-apis-1.3.04,serializer-2.7.2),但是没有运气。我在工作中使用docx4j-6.0.1.jar。 有人可以指导我解决这个问题吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

为我工作(docx4j 6.0.1,Xalan 2.7.2,序列化器2.7.2)

我想您的类路径中没有您认为包含的jar。

org.apache.xml.utils.DefaultErrorHandler在Xalan 2.7.2中。

此外,堆栈跟踪中的行号:

    java.lang.NoSuchMethodError: org.apache.xml.utils.DefaultErrorHandler.(Z)V 
at org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl.(TransformerIdentityImpl.java:1459) 
at org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl.(TransformerIdentityImpl.java:96)

与docx4j源代码不匹配,所以很奇怪。