获取docx4j比较的详细信息

时间:2012-07-05 15:46:36

标签: docx4j

我接受了比较此处docx文件的建议:OutOfMemoryError while doing docx comparison using docx4j

然而,这一行:

Body newBody = (Body) org.docx4j.XmlUtils.unmarshalString(contentStr);

会触发许多JAXB警告,例如:

WARN org.docx4j.jaxb.JaxbValidationEventHandler .handleEvent line 80 - [ERROR] : unexpected element (uri:"", local:"ins"). Expected elements are <{[?]}text>
INFO org.docx4j.jaxb.JaxbValidationEventHandler .handleEvent line 106 - continuing (with possible element/attribute loss)

这是可以理解的,因为org.docx4j.wml.Text没有表示处理任何嵌套标签Docx4jDriver.diff()写的字符串包含:

<w:t dfx:insert="true" xml:space="preserve"><ins>This</ins><ins> </ins><ins>first</ins><ins> </ins><ins>line</ins><ins> </ins><ins>has</ins><ins> </ins><ins>a</ins><ins> </ins></w:t>

因此,包含Text.getValue()标记的<ins>调用将返回一个空字符串。

我正在尝试使用建议的方法加上以下代码以编程方式确定两个docx文件之间的差异(原始+转发docx转换过程的结果):

Body newBody = (Body) org.docx4j.XmlUtils.unmarshalString(contentStr);

for ( Object bodyPart : newBody.getContent() ) {
  if ( bodyPart instanceof P ) {
    P bodyPartInCast = (P)bodyPart;
    for ( Object currentPContent : bodyPartInCast.getContent() ) {
      if ( currentPContent instanceof R ) {
        R pContentCast = (R)currentPContent;
        for( Object currentRContent : pContentCast.getContent() ) {
          if ( currentRContent instanceof JAXBElement ) {
            JAXBElement rContentCast = (JAXBElement)currentRContent;
            Object jaxbValue = rContentCast.getValue();
            if ( jaxbValue instanceof Text ) {
              Text textValue = (Text)jaxbValue;
              System.out.println( "Text: --> " + textValue.getValue() );
            } 
          }
        }
      } 
    }
  } 
}

所以,问题是:如果这不是处理两个文件之间差异细节的正确方法,那是什么?

我正在使用docx4j版本2.8.0,并且正在比较的两个docx文件是:

  1. Document 1 (input)
  2. Document 2 (output)

1 个答案:

答案 0 :(得分:0)

披露:我的工作是docx4j

查看CompareDocuments使用Differencer将差异结果转换回有效的WordML内容。