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