在使用Apache POI的XWPFDocument替换Word文档中的书签内容时,出现了DOM Level 3 Not implemented异常。

这个异常是由于Apache POI使用的Xerces XML解析器不支持DOM Level 3的操作导致的。解决办法是使用JDK自带的XML解析器来替代Xerces。

可以通过设置系统属性来指定使用JDK自带的XML解析器,代码如下:

System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

在main方法的开头添加上述代码即可解决该异常。

完整的代码如下:

package word;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Word {
    public static void main(String[] args) {
        try {
            // 设置系统属性,指定使用JDK自带的XML解析器
            System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

            // 读取源文件
            XWPFDocument sourceDoc = new XWPFDocument(new FileInputStream("input01.docx"));
            // 读取目标文件
            XWPFDocument targetDoc = new XWPFDocument(new FileInputStream("input03.docx"));

            // 获取源文件中的所有书签
            for (XWPFParagraph paragraph : sourceDoc.getParagraphs()) {
                for (CTBookmark bookmark : paragraph.getCTP().getBookmarkStartList()) {
                    String bookmarkName = bookmark.getName();
                    // 判断书签名称是否为应变计书签
                    if (bookmarkName.equals("应变计")) {
                        // 获取书签的文本内容
                        String bookmarkText = paragraph.getText();

                        // 在目标文件中查找同名的书签
                        for (XWPFParagraph targetParagraph : targetDoc.getParagraphs()) {
                            for (CTBookmark targetBookmark : targetParagraph.getCTP().getBookmarkStartList()) {
                                String targetBookmarkName = targetBookmark.getName();
                                // 判断目标文件中的书签名称是否为应变计书签
                                if (targetBookmarkName.equals("应变计")) {
                                    // 替换目标文件中的书签内容
                                    targetParagraph.getCTP().getBookmarkStartList().get(0).getDomNode().setTextContent(bookmarkText);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            // 保存目标文件
            FileOutputStream outputStream = new FileOutputStream("input04.docx");
            targetDoc.write(outputStream);
            outputStream.close();

            System.out.println("书签内容复制完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

添加上述代码后,重新运行程序即可正常替换Word文档中的书签内容。

package word;import orgapachepoixwpfusermodelXWPFDocument;import orgapachepoixwpfusermodelXWPFParagraph;import orgopenxmlformatsschemaswordprocessingmlx2006mainCTBookmark;import javaioFileInputStream;

原文地址: https://www.cveoy.top/t/topic/h227 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录