Thanks for help,
i post here what I have written.
- Code: Select all
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBContext;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Document;
public class UnmarshallFromTemplate {
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String inputfilepath = "C:\\Users\\Michael Pavlovski\\Desktop\\unmarshallFromTemplateExample.docx";
boolean save = true;
String outputfilepath = System.getProperty("user.dir") + "\\test-out.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
.getJaxbElement();
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("colour", "green");
mappings.put("icecream", "chocolate");
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
documentPart.setJaxbElement((Document) obj);
if (save) {
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
System.out.println( "Saved output to:" + outputfilepath );
} else {
}
}
}
I've used the same template which is used in the example UnmarshallFromTemplate.java.
In the log4j output I find this:
3690 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>colour</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value
3690 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>icecream</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value
I don't understand why both keys can't be mapped.
Sincerly
MPav