Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示错误的variableReplace_Java_Docx_Docx4j - Fatal编程技术网

Java 显示错误的variableReplace

Java 显示错误的variableReplace,java,docx,docx4j,Java,Docx,Docx4j,我访问了GitHub存储库中的docx4j文件,并下载了VariableReplace。当我将此文件复制到netbeans中时,第86行出现了一个错误(找不到符号) 代码如下: /* * Copyright 2007-2008, Plutext Pty Ltd. * * This file is part of docx4j. docx4j is licensed under the Apache License, Version 2.0 (the "License")

我访问了GitHub存储库中的docx4j文件,并下载了VariableReplace。当我将此文件复制到netbeans中时,第86行出现了一个错误(找不到符号)

代码如下:

/*
 *  Copyright 2007-2008, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

        http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the 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 are at least 3 approaches for replacing variables in 
 * a docx.
 * 
 * 1. as shows in this example
 * 2. using Merge Fields (see org.docx4j.model.fields.merge.MailMerger)
 * 3. binding content controls to an XML Part (via XPath)
 * 
 * Approach 3 is the recommended one when using docx4j. See the 
 * ContentControl* examples, Getting Started, and the subforum.
 * 
 * Approach 1, as shown in this example, works in simple cases
 * only.  It won't work if your KEY is split across separate
 * runs in your docx (which often happens), or if you want 
 * to insert images, or multiple rows in a table.
 * 
 * You're encouraged to investigate binding content controls
 * to an XML part.  There is org.docx4j.model.datastorage.migration.FromVariableReplacement
 * to automatically convert your templates to this better
 * approach.
 * 
 * OK, enough preaching.  If you want to use VariableReplace,
 * your variables should be appear like so: ${key1}, ${key2} 
 * 
 * And if you are having problems with your runs being split,
 * VariablePrepare can clean them up.
 *
 */
public class VariableReplace {

    public static void main(String[] args) throws Exception {

        // Exclude context init from 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 you haven't yet caused unmarshalling to occur):

            documentPart.variableReplace(mappings);

/*      // Approach 2 (original)

            // unmarshallFromTemplate requires string input
            String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
            // Do it...
            Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
            // Inject result into docx
            documentPart.setJaxbElement((Document) obj);
*/

        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("Time: " + total);

        // Save it
        if (save) {
            SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
            saver.save(outputfilepath);
        } else {
            System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true,
                    true));
        }
    }

}
/*
*版权所有2007-2008,Plutext私人有限公司。
*   
*此文件是docx4j的一部分。
docx4j根据Apache许可证2.0版(以下简称“许可证”)获得许可;
除非遵守许可证,否则不得使用此文件。
您可以通过以下方式获得许可证副本:
http://www.apache.org/licenses/LICENSE-2.0 
除非适用法律要求或书面同意,软件
根据许可证进行的分发是按“原样”进行分发的,
无任何明示或暗示的保证或条件。
请参阅许可证以了解管理权限和权限的特定语言
许可证下的限制。
*/
导入java.util.HashMap;
导入org.docx4j.XmlUtils;
导入org.docx4j.jaxb.Context;
导入org.docx4j.openpackaging.io.SaveToZipFile;
导入org.docx4j.openpackaging.packages.WordprocessingMLPackage;
导入org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
导入org.docx4j.wml.Document;
/**
*至少有3种方法可以替换中的变量
*文件。
* 
* 1. 如本例所示
* 2. 使用合并字段(请参见org.docx4j.model.Fields.Merge.MailMerge)
* 3. 将内容控件绑定到XML部件(通过XPath)
* 
*使用docx4j时,建议采用方法3。见
*ContentControl*示例、入门和子论坛。
* 
*如本例所示,方法1适用于简单情况
*只是。如果您的密钥被拆分为多个单独的
*在docx中运行(这经常发生),或者如果需要
*在表中插入图像或多行。
* 
*我们鼓励您研究绑定内容控件
*到XML部分。有org.docx4j.model.datastorage.migration.FromVariableReplacement
*要自动将模板转换为此格式,请执行以下操作:
*接近。
* 
*好了,说教够了。如果要使用VariableReplace,
*您的变量应该如下所示:${key1},${key2}
* 
*如果你在跑步时遇到问题,
*VariablePrepare可以清理它们。
*
*/
公共类变量替换{
公共静态void main(字符串[]args)引发异常{
//从计时中排除上下文初始化
org.docx4j.wml.ObjectFactory foo=Context.getWmlObjectFactory();
//输入docx中有变量:${color},${icecream}
字符串inputfilepath=System.getProperty(“user.dir”)+“/sample docs/word/unmarshallFromTemplateExample176.docx”;
布尔保存=假;
字符串outputfilepath=System.getProperty(“user.dir”)
+“/OUT\u variableplace.docx”;
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage
.load(新的java.io.File(inputfilepath));
MainDocumentPart documentPart=wordMLPackage.getMainDocumentPart();
HashMappings=新的HashMap();
映射。放置(“颜色”、“绿色”);
把(“冰淇淋”、“巧克力”);
长启动=System.currentTimeMillis();
//方法1(从3.0.0开始;如果尚未导致解组,则速度更快):
documentPart.variableReplace(映射);
/*//方法2(原件)
//unmarshallFromTemplate需要字符串输入
字符串xml=XmlUtils.marshaltoString(documentPart.getJaxbElement(),true);
//做吧。。。
Object obj=XmlUtils.unmarshallFromTemplate(xml,映射);
//将结果注入docx
documentPart.setJaxbElement((文档)obj);
*/
long end=System.currentTimeMillis();
长总计=结束-开始;
系统输出打印项次(“时间:+总计);
//省省吧
如果(保存){
SaveToZipFile saver=新的SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}否则{
System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(),true,
是的);
}
}
}
有人知道我为什么会犯这个错误吗

以下是一些图片:

感谢Hrach

编辑:

我没有立即收到任何错误,但当我运行下面的代码时,会收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
    at VariableReplace.main(VariableReplace.java:61)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 2 more
Java Result: 1
线程“main”java.lang.NoClassDefFoundError中的异常:org/apache/log4j/Logger 位于org.docx4j.openpackaging.Base。(Base.java:42) 在variableplace.main(variableplace.java:61) 原因:java.lang.ClassNotFoundException:org.apache.log4j.Logger 位于java.net.URLClassLoader$1.run(URLClassLoader.java:202) 位于java.security.AccessController.doPrivileged(本机方法) 位于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) ... 还有两个 Java结果:1 我刚刚解决了这个问题:)。我用的是旧样品

以下是新代码:

/*
 *  Copyright 2007-2008, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

        http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the 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 are at least 3 approaches for replacing variables in 
 * a docx.
 * 
 * 1. as shows in this example
 * 2. using Merge Fields (see org.docx4j.model.fields.merge.MailMerger)
 * 3. binding content controls to an XML Part (via XPath)
 * 
 * Approach 3 is the recommended one when using docx4j. See the 
 * ContentControl* examples, Getting Started, and the subforum.
 * 
 * Approach 1, as shown in this example, works in simple cases
 * only.  It won't work if your KEY is split across separate
 * runs in your docx (which often happens), or if you want 
 * to insert images, or multiple rows in a table.
 * 
 * You're encouraged to investigate binding content controls
 * to an 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");

        // 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));
        }
    }

}
/*
*版权所有2007-2008,Plutext私人有限公司。
*   
*此文件是docx4j的一部分。
docx4j根据Apache许可证2.0版(以下简称“许可证”)获得许可;
除非遵守许可证,否则不得使用此文件。
您可以通过以下方式获得许可证副本:
http://www.apache.org/licenses/LICENSE-2.0 
除非适用法律要求或书面同意,软件
根据许可证进行的分发是按“原样”进行分发的,
无任何明示或暗示的保证或条件。
请参阅许可证以了解管理权限和权限的特定语言
许可证下的限制。
*/
包org.docx4j.samples;
导入java.util.HashMap;
导入org.docx4j.XmlUtils;
导入org.docx4j.openpackagi