feiyu02
2022-11-15 23bd719cebe5feeff4e48fde925b0b39755eea93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.ClassFinder;
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();
                
        ClassFinder finder = new ClassFinder(FldChar.class); // <----- change this to suit
        new TraversalUtil(documentPart.getContent(), finder);
        
        System.out.println("got " + finder.results.size()  );
        
        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));
            }
 
            
            
        }
                                
    }
    
        
}