gpt4 book ai didi

java - 使用 docx4j 进行 docx 比较时出现 OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-30 09:46:05 25 4
gpt4 key购买 nike

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

java.lang.OutOfMemoryError: Java heap space error,

有人可以帮忙吗?

最佳答案

内存不足是因为您没有使用 Docx4jDriver 类,通过首先进行段落级差异,可以使差异问题更容易处理。

像这样使用它:

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

关于java - 使用 docx4j 进行 docx 比较时出现 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com