As tinne suggests, you need to understand a bit about JAXB.
Then you'll be able to traverse the main document part, and looking at the org.docx4j.wml.Text objects
docx4j provides a couple of ways to help you to find the text you are looking for. (This is in the current English Getting Started guide; maybe not in the old Chinese translation.)
XPathyou can do use XPath to select JAXB nodes:
- Code: Select all
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.
TraversalUtilTraversalUtil is a general approach for traversing the JAXB object tree in the main document part. It can also be applied to headers, footers etc. TraversalUtil has an interface Callback, which you use to specify how you want to traverse the nodes, and what you want to do to them.