Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
Xml docx4j复选框_Xml_Xpath_Checkbox_Ms Word_Docx4j - Fatal编程技术网

Xml docx4j复选框

Xml docx4j复选框,xml,xpath,checkbox,ms-word,docx4j,Xml,Xpath,Checkbox,Ms Word,Docx4j,不幸的是,我对docx4j还比较陌生,我正试图弄清楚如何在我拥有的模板中选中复选框。 我尝试使用XPath并以这种方式获得节点,但我不确定我是否获得了正确的节点,即使我成功获得了正确的节点,我也不确定如何正确更改值,替换我设法找到的文本,但我还没有找到更改属性值的方法 通过检查document.xml,我找到了复选框的名称及其属性 <w:fldChar w:fldCharType="begin"> <w:ffData><w:name w:val="Kontrollk

不幸的是,我对docx4j还比较陌生,我正试图弄清楚如何在我拥有的模板中选中复选框。 我尝试使用XPath并以这种方式获得节点,但我不确定我是否获得了正确的节点,即使我成功获得了正确的节点,我也不确定如何正确更改值,替换我设法找到的文本,但我还没有找到更改属性值的方法

通过检查document.xml,我找到了复选框的名称及其属性

<w:fldChar w:fldCharType="begin">
<w:ffData><w:name w:val="Kontrollkästchen1"/>
<w:enabled/>
<w:calcOnExit w:val="0"/>
<w:checkBox>
<w:sizeAuto/>
<w:default w:val="0"/>
</w:checkBox>

我尝试了不同的XPath前置,例如: //ffData[@name='Kontrollkästchen1']/checkBox

这会找到我想要的节点吗?如果没有,如何获取节点并正确更改属性

多谢各位
Magnus

如果使用XPath,则需要考虑名称空间

使用XPathQuery示例,您可以为其提供:

String xpath = "//w:fldChar[./w:ffData/w:checkBox]";
(或变体,取决于要选择这三个节点中的哪一个)

另一种方法是遍历文档,其中有TraversalUtils

docx4j的入门文档中解释了这两种方法

如前所述,如果您修改了对象,就不能依赖Sun/Oracle JAXB XPath

因此,手动导线测量通常会更好

这里有一个这样做的例子

package org.docx4j.samples;

import java.util.ArrayList;
import java.util.List;

import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.FldChar;

public class TraverseFind {


    /**
     * Example of how to find an object in document.xml
     * via traversal (as opposed to XPath)
     *  
     */
    public static void main(String[] args) throws Exception {

        String inputfilepath = System.getProperty("user.dir") + "/checkbox.docx";

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));      
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        Finder finder = new Finder(FldChar.class);
        new TraversalUtil(documentPart.getContent(), finder);

        System.out.println("got " + finder.results.size() + " of type " +  finder.typeToFind.getName() );

        for (Object o : finder.results) {

            Object o2 = XmlUtils.unwrap(o);
            // this is ok, provided the results of the Callback
            // won't be marshalled          

            if (o2 instanceof org.docx4j.wml.Text) {

                org.docx4j.wml.Text txt = (org.docx4j.wml.Text)o2;

                System.out.println( txt.getValue() );

            } else {
                System.out.println( XmlUtils.marshaltoString(o, true, true));
            }



        }

    }

      public static class Finder extends CallbackImpl {

          protected Class<?> typeToFind;

          protected Finder(Class<?> typeToFind) {
              this.typeToFind = typeToFind;
          }

            public List<Object> results = new ArrayList<Object>(); 

            @Override
            public List<Object> apply(Object o) {

                // Adapt as required
                if (o.getClass().equals(typeToFind)) {
                    results.add(o);
                }
                return null;
            }
      }

}
package org.docx4j.samples;
导入java.util.ArrayList;
导入java.util.List;
导入org.docx4j.TraversalUtil;
导入org.docx4j.XmlUtils;
导入org.docx4j.TraversalUtil.CallbackImpl;
导入org.docx4j.openpackaging.packages.WordprocessingMLPackage;
导入org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
导入org.docx4j.wml.FldChar;
公共类遍历find{
/**
*如何在document.xml中查找对象的示例
*通过遍历(与XPath相反)
*  
*/
公共静态void main(字符串[]args)引发异常{
字符串inputfilepath=System.getProperty(“user.dir”)+“/checkbox.docx”;
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage.load(新java.io.File(inputfilepath));
MainDocumentPart documentPart=wordMLPackage.getMainDocumentPart();
查找器=新查找器(FldChar.class);
新的TraversalUtil(documentPart.getContent(),finder);
System.out.println(“get”+finder.results.size()+”类型为“+finder.typeToFind.getName());
for(对象o:finder.results){
对象o2=XmlUtils.unwrap(o);
//只要有回调的结果,就可以了
//不会被编组
if(o2 instanceof org.docx4j.wml.Text){
org.docx4j.wml.Text txt=(org.docx4j.wml.Text)o2;
System.out.println(txt.getValue());
}否则{
System.out.println(XmlUtils.marshaltoString(o,true,true));
}
}
}
公共静态类查找器扩展了CallbackImpl{
受保护类类型查找;
受保护的查找器(类typeToFind){
this.typeToFind=typeToFind;
}
公共列表结果=新建ArrayList();
@凌驾
公共列表应用(对象o){
//根据需要进行调整
if(o.getClass().equals(typeToFind)){
结果:添加(o);
}
返回null;
}
}
}
按照我完成这些示例的方式,它们都为您提供了org.docx4j.wml.FldChar对象

从那里,您可以在getFfData()中找到CTFFCheckBox


如果您只需要选中该复选框,那么您可以调整任一示例以获取该复选框。这会更简单。

JasonPlutext,是否有任何解决方案可以使用docx4j设置复选框中的值?您应该发布一个新问题,列出您遇到的问题。Word文档中有几种类型的复选框,因此一些XML将帮助您明确所问的内容。非常感谢。