【问题标题】:Obtain Details of docx4j Comparison获取 docx4j 比较的详细信息
【发布时间】:2012-07-06 01:43:29
【问题描述】:

我从这里接受了比较 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>

因此,包含&lt;ins&gt; 标签的Text.getValue() 调用返回一个空字符串。

我正在尝试使用建议的方法和以下代码以编程方式确定两个 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)

【问题讨论】:

    标签: docx4j


    【解决方案1】:

    披露:我在 docx4j 工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 2020-10-08
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多