docx4j检查复选框 [英] docx4j checking checkboxes

查看:698
本文介绍了docx4j检查复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im不幸的是相当新的docx4j,我想弄明白如何检查一个模板中的复选框,我有。
我试图使用Xpaths和获得节点的方式,但我不确定我是否正确,即使我管理获得正确的节点Im不太确定如何正确更改值,替换文本我管理图



检查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>

我试过了不同的预处理Xpaths,例如:
// ffData [@name ='Kontrollkästchen1'] / checkBox



这会找到我想要的节点吗?



谢谢
Magnus



使用XPathQuery示例,您可以使用XPath it:

  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 {


/ **
*如何在document.xml中查找对象的示例
*通过遍历(相对于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){

对象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());

} 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){

//根据需要调整
if(o.getClass()。equals(typeToFind)){
results.add(o);
}
return null;
}
}

}

我做了这些例子,他们都得到你的org.docx4j.wml.FldChar对象。



在这里,您会在getFfData()中找到您的CTFFCheckBox。getNameOrEnabledOrCalcOnExit()



想要的是复选框,那么你可以适应任何一个例子来抓取。这将更简单。


Im unfortunatly rather new to docx4j and I am trying to figure out how to check a checkbox within a template that I have. I tried working with Xpaths and get the Node that way but Im not sure I got it right, also even if I manage getting the right Node Im not quite sure how to change the value properly, replacing text I managed to figure out but I havent figured out changing a attribute value yet.

checking the document.xml i found the name of the Checkbox and the attributes it has

<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>

And I tried diffrent premutations of Xpaths, for example: //ffData[@name='Kontrollkästchen1']/checkBox

Would this find the Node I want? If not, how could I get the node and change the attribute correctly?

Thank you Magnus

解决方案

If you are using XPaths, you need to take the namespace into account.

Using the XPathQuery sample, you could feed it:

String xpath = "//w:fldChar[./w:ffData/w:checkBox]";

(or a variation, depending which of those three nodes you want to select)

The alternative approach is to traverse the document, for which there is TraversalUtils.

Both of these approaches are explained in docx4j's Getting Started document.

As noted there, Sun/Oracle JAXB XPaths can't be relied if you have modified your objects.

For that reason, a manual traverse can often be better.

Here's an example of how to do it that way.

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

}

The way I've done these examples, they both get you the org.docx4j.wml.FldChar object.

From there, you'll find your CTFFCheckBox inside getFfData().getNameOrEnabledOrCalcOnExit()

If all you want is the check box, then you can adapt either example to fetch just that. That would be simpler.

这篇关于docx4j检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆