| 1 | package org.docx4j.samples; |
|---|
| 2 | |
|---|
| 3 | import java.util.List; |
|---|
| 4 | |
|---|
| 5 | import org.docx4j.XmlUtils; |
|---|
| 6 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 7 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 8 | |
|---|
| 9 | public class XPathQuery { |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * @param args |
|---|
| 14 | */ |
|---|
| 15 | public static void main(String[] args) throws Exception { |
|---|
| 16 | |
|---|
| 17 | String inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml"; |
|---|
| 18 | //String inputfilepath = System.getProperty("user.dir") + "//tmp//modelo.docx"; |
|---|
| 19 | // String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/fo-200912.xml"; |
|---|
| 20 | |
|---|
| 21 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 22 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 23 | |
|---|
| 24 | //String xpath = "//w:t"; |
|---|
| 25 | String xpath = "//w:r[w:t[contains(text(),'scaled')]]"; |
|---|
| 26 | |
|---|
| 27 | List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false); |
|---|
| 28 | |
|---|
| 29 | System.out.println("got " + list.size() + " matching " + xpath ); |
|---|
| 30 | |
|---|
| 31 | for (Object o : list) { |
|---|
| 32 | |
|---|
| 33 | System.out.println(o.getClass().getName() ); |
|---|
| 34 | |
|---|
| 35 | Object o2 = XmlUtils.unwrap(o); |
|---|
| 36 | // this is ok, provided the results of the Callback |
|---|
| 37 | // won't be marshalled |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | if (o2 instanceof org.docx4j.wml.Text) { |
|---|
| 41 | |
|---|
| 42 | org.docx4j.wml.Text txt = (org.docx4j.wml.Text)o2; |
|---|
| 43 | |
|---|
| 44 | System.out.println( txt.getValue() ); |
|---|
| 45 | |
|---|
| 46 | Object parent = txt.getParent(); |
|---|
| 47 | |
|---|
| 48 | System.out.println( "parent: " + XmlUtils.unwrap(parent).getClass().getName() ); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | } |
|---|