0
我把建議從這裏比較的docx文件:獲取詳細
然而,這條線:
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>
因此,包含<ins>
標籤Text.getValue()
調用返回一個空字符串。
我試圖以編程方式確定兩個docx文件之間的diff(原件+結果往返一個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文件進行比較是: