使用docx4j进行docx比较时出现OutOfMemoryError [英] OutOfMemoryError while doing docx comparison using docx4j

查看:368
本文介绍了使用docx4j进行docx比较时出现OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中我比较两个docx文件并创建一个html比较文件,当我尝试使用150或170行以下的文件然后没有问题,而我尝试比较像200行或更多的大文件然后那个时候显示

in my application i am comparing two docx files and creating one html comparison file, when i tried with below 150 or 170 lines of file then there is no issue, while i try to compare the big files like 200 lines or more than that then that time it showing the

java.lang.OutOfMemoryError: Java heap space error,

任何人都可以帮忙吗?

推荐答案

由于您没有使用Docx4jDriver类,因此内存不足,这使得diff问题更容易通过首先执行段落级差异来处理。

You are running out of memory because you aren't using the Docx4jDriver class, which makes the diff problem more tractable by doing a paragraph level diff first.

使用它像这样:

        Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody();
        Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody();

        // 2. Do the differencing
        java.io.StringWriter sw = new java.io.StringWriter();
        Docx4jDriver.diff( XmlUtils.marshaltoW3CDomDocument(newerBody).getDocumentElement(),
                        XmlUtils.marshaltoW3CDomDocument(olderBody).getDocumentElement(),
                           sw);

        // 3. Get the result
        String contentStr = sw.toString();
        System.out.println("Result: \n\n " + contentStr);
        Body newBody = (Body) org.docx4j.XmlUtils
                        .unmarshalString(contentStr);

这篇关于使用docx4j进行docx比较时出现OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆