First i have created a document template file and added a xml to MS Document from XML Mapping Pane of Developer tab.
Xml used for content binding.
<employee>
<name></name>
</employee>
Below mentioned method is used to write xml into doc template. Where this method takes documentForm(result doc file), xmlSchemaFilePath(created xml path), templateForm(templete file) as arguments.
public String fillXMLBasedForm(String documentForm, String xmlSchemaFilePath, String templateForm)
throws Exception {
try {
WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(templateForm));
FileInputStream xmlStream = new FileInputStream(xmlSchemaFilePath);
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML);
Docx4J.save(wordMLPackage, new FileOutputStream(new File(documentForm)), Docx4J.FLAG_NONE);
}
catch(Exception e) {
e.printStackTrace();
}
return documentForm;
}
Once i open the generated document file then got "Word found unreadable content.." popup. once i proceed as trusted document then found no issue in content because for testing purpose written only single string to it.
XML added via code.
<employee>
<name>Vivek Singh</name>
</employee>