java - variableReplace showing a error -


i have went onto github repository docx4j files , downloaded variablereplace. when copied file netbeans, got error on line 86 (cannont find symbol).

here code:

/*  *  copyright 2007-2008, plutext pty ltd.  *     *  file part of docx4j.      docx4j licensed under apache license, version 2.0 (the "license");      may not use file except in compliance license.       may obtain copy of license @           http://www.apache.org/licenses/license-2.0       unless required applicable law or agreed in writing, software      distributed under license distributed on "as is" basis,      without warranties or conditions of kind, either express or implied.      see license specific language governing permissions ,      limitations under license.   */   import java.util.hashmap;  import org.docx4j.xmlutils; import org.docx4j.jaxb.context; import org.docx4j.openpackaging.io.savetozipfile; import org.docx4j.openpackaging.packages.wordprocessingmlpackage; import org.docx4j.openpackaging.parts.wordprocessingml.maindocumentpart; import org.docx4j.wml.document;   /**  * there @ least 3 approaches replacing variables in   * docx.  *   * 1. shows in example  * 2. using merge fields (see org.docx4j.model.fields.merge.mailmerger)  * 3. binding content controls xml part (via xpath)  *   * approach 3 recommended 1 when using docx4j. see   * contentcontrol* examples, getting started, , subforum.  *   * approach 1, shown in example, works in simple cases  * only.  won't work if key split across separate  * runs in docx (which happens), or if want   * insert images, or multiple rows in table.  *   * you're encouraged investigate binding content controls  * xml part.  there org.docx4j.model.datastorage.migration.fromvariablereplacement  * automatically convert templates better  * approach.  *   * ok, enough preaching.  if want use variablereplace,  * variables should appear so: ${key1}, ${key2}   *   * , if having problems runs being split,  * variableprepare can clean them up.  *  */ public class variablereplace {      public static void main(string[] args) throws exception {          // exclude context init timing         org.docx4j.wml.objectfactory foo = context.getwmlobjectfactory();          // input docx has variables in it: ${colour}, ${icecream}         string inputfilepath = system.getproperty("user.dir") + "/sample-docs/word/unmarshallfromtemplateexample176.docx";          boolean save = false;         string outputfilepath = system.getproperty("user.dir")                 + "/out_variablereplace.docx";          wordprocessingmlpackage wordmlpackage = wordprocessingmlpackage                 .load(new java.io.file(inputfilepath));         maindocumentpart documentpart = wordmlpackage.getmaindocumentpart();          hashmap<string, string> mappings = new hashmap<string, string>();         mappings.put("colour", "green");         mappings.put("icecream", "chocolate");          long start = system.currenttimemillis();          // approach 1 (from 3.0.0; faster if haven't yet caused unmarshalling occur):              documentpart.variablereplace(mappings);  /*      // approach 2 (original)              // unmarshallfromtemplate requires string input             string xml = xmlutils.marshaltostring(documentpart.getjaxbelement(), true);             // it...             object obj = xmlutils.unmarshallfromtemplate(xml, mappings);             // inject result docx             documentpart.setjaxbelement((document) obj); */          long end = system.currenttimemillis();         long total = end - start;         system.out.println("time: " + total);          // save         if (save) {             savetozipfile saver = new savetozipfile(wordmlpackage);             saver.save(outputfilepath);         } else {             system.out.println(xmlutils.marshaltostring(documentpart.getjaxbelement(), true,                     true));         }     }  } 

does know why getting error?

here few pictures:

error 1 overview

error 1 details

thanks hrach

edit:

i not immediate errors, when run code below, these errors:

exception in thread "main" java.lang.noclassdeffounderror: org/apache/log4j/logger     @ org.docx4j.openpackaging.base.<clinit>(base.java:42)     @ variablereplace.main(variablereplace.java:61) caused by: java.lang.classnotfoundexception: org.apache.log4j.logger     @ java.net.urlclassloader$1.run(urlclassloader.java:202)     @ java.security.accesscontroller.doprivileged(native method)     @ java.net.urlclassloader.findclass(urlclassloader.java:190)     @ java.lang.classloader.loadclass(classloader.java:306)     @ sun.misc.launcher$appclassloader.loadclass(launcher.java:301)     @ java.lang.classloader.loadclass(classloader.java:247)     ... 2 more java result: 1 

i figured out problem :). using old samples.

here new code:

/*  *  copyright 2007-2008, plutext pty ltd.  *     *  file part of docx4j.      docx4j licensed under apache license, version 2.0 (the "license");      may not use file except in compliance license.       may obtain copy of license @           http://www.apache.org/licenses/license-2.0       unless required applicable law or agreed in writing, software      distributed under license distributed on "as is" basis,      without warranties or conditions of kind, either express or implied.      see license specific language governing permissions ,      limitations under license.   */  package org.docx4j.samples;   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;   /**  * there @ least 3 approaches replacing variables in   * docx.  *   * 1. shows in example  * 2. using merge fields (see org.docx4j.model.fields.merge.mailmerger)  * 3. binding content controls xml part (via xpath)  *   * approach 3 recommended 1 when using docx4j. see   * contentcontrol* examples, getting started, , subforum.  *   * approach 1, shown in example, works in simple cases  * only.  won't work if key split across separate  * runs in docx (which happens), or if want   * insert images, or multiple rows in table.  *   * you're encouraged investigate binding content controls  * xml part.  *  */ public class variablereplace {      public static void main(string[] args) throws exception {          string inputfilepath = system.getproperty("user.dir") + "/sample-docs/word/unmarshallfromtemplateexample.docx";          boolean save = false;         string outputfilepath = system.getproperty("user.dir")                 + "/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");          // it...         object obj = xmlutils.unmarshallfromtemplate(xml, mappings);          // inject result docx         documentpart.setjaxbelement((document) obj);          // save         if (save) {             savetozipfile saver = new savetozipfile(wordmlpackage);             saver.save(outputfilepath);         } else {             system.out.println(xmlutils.marshaltostring(documentpart.getjaxbelement(), true,                     true));         }     }  } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -