This is the code...
- Code: Select all
import java.util.HashMap;
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 VariableReplace {
public static void main(String[] args) throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/resource/template.docx";
boolean save = true;
String outputfilepath = System.getProperty("user.dir")
+ "/resource/OUT_VariableReplace.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
// unmarshallFromTemplate requires string input
String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("colour", "green");
mappings.put("icecream", "chocolate");
// Do it...
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
// Inject result into docx
documentPart.setJaxbElement((Document) obj);
// Save it
if (save) {
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
} else {
System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true,
true));
}
}
}
and this is the result...
- Code: Select all
dic 23, 2014 11:43:36 AM org.docx4j.jaxb.Context <clinit>
INFORMACIÓN: java.vendor=Oracle Corporation
dic 23, 2014 11:43:36 AM org.docx4j.jaxb.Context <clinit>
INFORMACIÓN: java.version=1.7.0_72
dic 23, 2014 11:43:36 AM org.docx4j.jaxb.Context <clinit>
INFORMACIÓN: No MOXy JAXB config found; assume not intended..
dic 23, 2014 11:43:36 AM org.docx4j.jaxb.NamespacePrefixMapperUtils getPrefixMapper
INFORMACIÓN: Using NamespacePrefixMapperSunInternal, which is suitable for Java 6
dic 23, 2014 11:43:36 AM org.docx4j.jaxb.Context <clinit>
INFORMACIÓN: Using Java 6/7 JAXB implementation
dic 23, 2014 11:43:43 AM org.docx4j.jaxb.Context <clinit>
INFORMACIÓN: Not using MOXy; using com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl
dic 23, 2014 11:43:43 AM org.docx4j.utils.ResourceUtils getResource
ADVERTENCIA: Couldn't get resource: docx4j.properties
dic 23, 2014 11:43:43 AM org.docx4j.Docx4jProperties init
ADVERTENCIA: Couldn't find/read docx4j.properties; docx4j.properties not found via classloader.
dic 23, 2014 11:43:43 AM org.docx4j.XmlUtils <clinit>
INFORMACIÓN: Using com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
dic 23, 2014 11:43:43 AM org.docx4j.XmlUtils <clinit>
INFORMACIÓN: Using com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.contenttype.ContentTypeManager createPackage
INFORMACIÓN: Detected WordProcessingML package
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io3.Load3 get
INFORMACIÓN: Instantiated package of type org.docx4j.openpackaging.packages.WordprocessingMLPackage
dic 23, 2014 11:43:44 AM org.docx4j.utils.XPathFactoryUtil getXPathFactory
INFORMACIÓN: xpath implementation: org.apache.xpath.jaxp.XPathFactoryImpl
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io3.Load3 get
INFORMACIÓN: package read; elapsed time: 7758 ms
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/document.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware unmarshal
INFORMACIÓN: For org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart, unmarshall via binder
dic 23, 2014 11:43:44 AM org.docx4j.XmlUtils replace
ADVERTENCIA: Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="001E208F"><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:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value
dic 23, 2014 11:43:44 AM org.docx4j.XmlUtils replace
ADVERTENCIA: Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>i</w:t></w:r><w:bookmarkStart w:name="_GoBack" w:id="0"/><w:bookmarkEnd w:id="0"/><w:r w:rsidR="001E208F"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>cecream</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile save
INFORMACIÓN: SAVING to /home/fav/Fran/workspace/CambiarVariableDocx/resource/OUT_VariableReplace.docx
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.contenttype.ContentTypeManager marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.contenttype.ContentTypeManager ...
dic 23, 2014 11:43:44 AM org.docx4j.jaxb.NamespacePrefixMapperUtils getPrefixMapperRelationshipsPart
INFORMACIÓN: Using NamespacePrefixMapperSunInternal, which is suitable for Java 6
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.relationships.RelationshipsPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: _rels/.rels
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.DocPropsExtendedPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /docProps/app.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.DocPropsExtendedPart unmarshal
INFORMACIÓN: unmarshalling org.docx4j.openpackaging.parts.DocPropsExtendedPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: docProps/app.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.DocPropsCorePart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /docProps/core.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.DocPropsCorePart unmarshal
INFORMACIÓN: unmarshalling org.docx4j.openpackaging.parts.DocPropsCorePart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: docProps/core.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/document.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.relationships.RelationshipsPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/_rels/document.xml.rels
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.WordprocessingML.WebSettingsPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/webSettings.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/webSettings.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/settings.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware unmarshal
INFORMACIÓN: For org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart, unmarshall via binder
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart setMceIgnorable
ADVERTENCIA: w15
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/settings.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/styles.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware unmarshal
INFORMACIÓN: For org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart, unmarshall via binder
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/styles.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.ThemePart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/theme/theme1.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware unmarshal
INFORMACIÓN: For org.docx4j.openpackaging.parts.ThemePart, unmarshall via binder
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/theme/theme1.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart marshal
INFORMACIÓN: marshalling org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.parts.JaxbXmlPart getContents
INFORMACIÓN: Lazily unmarshalling /word/fontTable.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile saveRawXmlPart
INFORMACIÓN: success writing part: word/fontTable.xml
dic 23, 2014 11:43:44 AM org.docx4j.openpackaging.io.SaveToZipFile save
INFORMACIÓN: ...Done!
the file is created, but instead of replacing ${colour} for "green" just write "colour" without "${ }".
Any ideas?
P/D: Sorry for my bad English.