Page 1 of 1

Select Table

PostPosted: Mon Apr 11, 2011 8:15 am
by novato

Maybe my question is very easy ........

How I can select a table in docx? For example, the second table


Re: Select Table

PostPosted: Mon Apr 11, 2011 9:47 am
by jason
Easiest is selecting via XPath.

Alternatively you can use TraversalUtil.

Both are described in ... arted.html

Here is the text on XPath:

Selecting your insertion/editing point;
accessing JAXB nodes via XPath

Sometimes, XPath is a succinct way to select the things you need to change.

Happily, from docx4j 2.5.0, you can do use XPath to select JAXB nodes:

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

String xpath = "//w:p";

List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);

These JAXB nodes are live, in the sense that if you change them, your document changes.

There is a limitation however: the xpath expressions are evaluated against the XML document as it was when first opened in docx4j. You can update the associated XML document once only, by passing true into getJAXBNodesViaXPath. Updating it again (with current JAXB 2.1.x or 2.2.x) will cause an error.