source: trunk/docx4j/src/scratchpad/XPathNavigation.java @ 1154

Revision 1154, 4.2 KB checked in by jharrop, 23 months ago (diff)
Line 
1package org.docx4j.samples;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Map;
9
10import javax.xml.XMLConstants;
11import javax.xml.bind.Binder;
12import javax.xml.bind.JAXBContext;
13import javax.xml.bind.JAXBException;
14import javax.xml.namespace.NamespaceContext;
15import javax.xml.parsers.DocumentBuilder;
16import javax.xml.parsers.DocumentBuilderFactory;
17import javax.xml.parsers.ParserConfigurationException;
18import javax.xml.xpath.XPath;
19import javax.xml.xpath.XPathConstants;
20import javax.xml.xpath.XPathExpressionException;
21import javax.xml.xpath.XPathFactory;
22
23import org.apache.commons.lang.text.StrTokenizer;
24import org.docx4j.XmlUtils;
25import org.docx4j.openpackaging.exceptions.Docx4JException;
26import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
27import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
28import org.docx4j.openpackaging.parts.XmlPart.XmlNamespaceContext;
29import org.docx4j.openpackaging.parts.relationships.Namespaces;
30import org.w3c.dom.Node;
31import org.w3c.dom.NodeList;
32
33public class XPathNavigation {
34
35        public static JAXBContext context = org.docx4j.jaxb.Context.jc; 
36
37//      private static XPathFactory xPathFactory;
38//      private static XPath xPath;
39//             
40//      static {
41//             
42//              // Crimson doesn't support setTextContent; this.writeDocument also fails.
43//              // We've already worked around the problem with setTextContent,
44//              // but rather than do the same for writeDocument,
45//              // let's just stop using it.
46//              System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
47//                      "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
48//             
49//              xPathFactory = XPathFactory.newInstance();
50//              xPath = xPathFactory.newXPath();                               
51//      }
52       
53       
54        /**
55         * @param args
56         */
57        public static void main(String[] args) throws Exception {
58
59                //String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Images.docx";
60                String inputfilepath = System.getProperty("user.dir") + "//tmp//modelo.docx";
61//      String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/fo-200912.xml";
62                               
63                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));         
64                MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
65
66                // Setup the binder
67                //Binder<Node> binder = documentPart.getBinder();
68
69//              javax.xml.parsers.DocumentBuilderFactory dbf
70//              = DocumentBuilderFactory.newInstance();
71//      dbf.setNamespaceAware(true);
72//      org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
73//      binder.marshal(documentPart.getJaxbElement(), doc);
74//      System.out.println(XmlUtils.w3CDomNodeToString(doc) );
75       
76                System.out.println( "Binder established" );
77               
78                /* updateXML is not enough to establish the initial association.
79                 *
80                 * There are 2 ways to establish it.
81                 *
82                 * Something like the following should work, but didn't quite
83                 *
84                        javax.xml.parsers.DocumentBuilderFactory dbf
85                                = DocumentBuilderFactory.newInstance();
86                        dbf.setNamespaceAware(true);
87                        org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
88                        binder.marshal(documentPart.getJaxbElement(), doc);
89                 *
90                 * this does populate doc, but doesn't seem to establish the association??
91                 *
92                       
93                 * The way which does work is the other way around ie
94                 * unmarshall from the XML document, so that's what we do.
95                 *
96                 * You can do:
97                 *
98                                Node node = binder.updateXML(documentPart.getJaxbElement());
99                                node = binder.getXMLNode(documentPart.getJaxbElement());
100
101                 * OR
102                 *
103                                Node node = binder.getXMLNode(documentPart.getJaxbElement());
104
105                 * BUT NOT
106
107                                Node node = binder.updateXML(documentPart.getJaxbElement());
108
109                 *
110                 */
111                               
112                List<Object> list = documentPart.getJAXBNodesViaXPath("//w:p", true);
113               
114                System.out.println("got " + list.size() );
115                               
116                /*
117                 * Note https://jaxb.dev.java.net/issues/show_bug.cgi?id=459
118                 * (Binder.updateXML() fails if called twice)
119                 *
120                 * So we can't use XPath after modifying the document
121                 * via JAXB, unless we get the marshall stuff working.
122                 *
123                 */
124               
125        }
126       
127               
128}
Note: See TracBrowser for help on using the repository browser.