Page 1 of 1

docx4j - Find and Replace

PostPosted: Mon Jan 26, 2015 7:55 pm
by msercansungur
Hello guys;

I'm trying to a find and replace app for our works. Actually, it may be faster and easier via VisualBasic; however, after a long time without programming, I've turn back to my old friend Java. First I've tried Apache POI but it was not very useful Than I've discovered docx4j and actually love it!

Anyway, I've researched on google and this forum completely and I've written my codes via Maven install. However, my code does not change anything. Could you warn me where is the main problem?

WordprocessingMLPackage wordMLPackage = null;
try {
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(chosenFile.getSelectedFile().getAbsolutePath()));
} catch (Docx4JException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
WordprocessingMLPackage.FilterSettings filterSettings= new WordprocessingMLPackage.FilterSettings();
filterSettings.setRemoveProofErrors(true);
filterSettings.setRemoveContentControls(true);
filterSettings.setRemoveRsids(true);
try {
wordMLPackage.filter(filterSettings);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
HashMap<String, String> mappings = new HashMap<String, String>();

//it shows the replacing; kis1.getK1xOld() is old value (to be replaced text) kis1.getK1xNew() is new value (the replacer)
mappings.put(kis1.getK1xOld(), kis1.getK1xNew());
mappings.put(kis1.getK2aOld(), kis1.getK2aNew());
mappings.put(kis1.getK3bOld(), kis1.getK3bNew());
mappings.put(kis1.getK4sOld(), kis1.getK4sNew());
mappings.put(kis1.getK5mOld(), kis1.getK5mNew());
mappings.put(kis1.getK6vOld(), kis1.getK6vNew());
mappings.put(kis1.getK7fOld(), kis1.getK7fNew());
mappings.put(kis1.getK8zOld(), kis1.getK8zNew());
mappings.put(kis1.getK9lOld(), kis1.getK9lNew());
mappings.put(kis1.getK10kOld(), kis1.getK10kNew());
mappings.put(kis1.getK11yOld(), kis1.getK11yNew());
mappings.put(kis1.getK12tOld(), kis1.getK12tNew());
Object obj = null;
try {
obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
} catch (JAXBException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
documentPart.setJaxbElement((Document)obj);
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
try {
saver.save(chosenFile.getSelectedFile().getAbsolutePath()+"edit");
} catch (Docx4JException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Re: docx4j - Find and Replace

PostPosted: Thu Jan 29, 2015 1:51 am
by msercansungur
is there anyone to help me please?

Re: docx4j - Find and Replace

PostPosted: Thu Jan 29, 2015 11:20 am
by jason
Probably you have split runs, or incorrect syntax for a variable.

If we're to help you, you'll need to unzip you docx, and post the content of your /word/document.xml part (the main document part - I'm assuming your variables are in here)

Also, what do your kis1.getK1xOld() etc variables contain?