如何使用Docx4j Java从Ms word merge字段获取值

时间:2013-12-24 10:43:17

标签: java docx4j

我正在尝试使用java中的Docx4j从合并字段中检索值。我正在使用以下方法检索word文档的所有内容:

WordprocessingMLPackage newWordMLPackage = WordprocessingMLPackage
    .load(new java.io.File("C:/Users/admin/Desktop/test" + i + ".docx"));
MainDocumentPart documentPart = newWordMLPackage.getMainDocumentPart();                 
System.out.println(documentPart.getContent());

这将返回word文档中的内容列表。我目前得到的是

MERGEFIELD lastName \* MERGEFORMAT himura

我想要的是从merge-field'lastName'获取值'himura'。我怎样才能做到这一点?
感谢

1 个答案:

答案 0 :(得分:0)

您可以使用xpath进行操作-请参见方法documentPart.getJAXBNodesViaXPath(xpath, false);

我有类似的问题(想用自己的内容替换MergeField)。经过长时间的研究,我写了一种可以做到这一点的方法:

private void replaceTextWithElement(MainDocumentPart mainDocumentPart, String textToReplace, Collection<Object> newElements) throws JAXBException, Docx4JException {
        final String xpath = "//w:r[w:instrText[contains(text(),'MERGEFIELD') and contains(text(),'" + textToReplace + "')]]";
        final List<Object> foundNodes = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
        if (isEmpty(foundNodes)) {
            throw new RuntimeException("Cannot find textToReplace: \"" + textToReplace + "\" in document, skipping replacement.");
        }

        final R r = (R)foundNodes.get(0);
        final P parent = (P)r.getParent();
        final int index = mainDocumentPart.getContent().indexOf(parent);

        mainDocumentPart.getContent().remove(parent);
        if (newElements != null) {
            mainDocumentPart.getContent().addAll(index, newElements);
        }
    }