Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   *  Copyright 2007-2008, Plutext Pty Ltd.
   *  This file is part of docx4j.
      docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
      you may not use this file except in compliance with the License. 
      You may obtain a copy of the License at 
     Unless required by applicable law or agreed to in writing, software 
     distributed under the License is distributed on an "AS IS" BASIS, 
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     See the License for the specific language governing permissions and 
     limitations under the License.
 package org.docx4j.samples;
 import java.util.List;
To see what parts comprise your docx, try the PartsList sample. There will always be a MainDocumentPart, usually called document.xml. This sample shows you what objects are in that part. It also shows a general approach for traversing the JAXB object tree in the Main Document part. It can also be applied to headers, footers etc. It is an alternative to XSLT, and doesn't require marshalling/unmarshalling. If many cases, the method getJAXBNodesViaXPath would be more convenient, but there are 3 JAXB bugs which detract from that (see Getting Started). See related classes SingleTraversalUtilVisitorCallback and CompoundTraversalUtilVisitorCallback

 public class OpenMainDocumentAndTraverse extends AbstractSample {
 	public static JAXBContext context = ....;
 	public static void main(String[] argsthrows Exception {
 		 * You can invoke this from an OS command line with something like:
 		 * java -cp dist/docx4j.jar:dist/log4j-1.2.15.jar
 		 * org.docx4j.samples.OpenMainDocumentAndTraverse inputdocx
 		 * Note the minimal set of supporting jars.
 		 * If there are any images in the document, you will also need:
 		 * dist/xmlgraphics-commons-1.4.jar:dist/commons-logging-1.1.1.jar
 		try {
 		} catch (IllegalArgumentException e) {
 			 = System.getProperty("user.dir")
 					+ "/sample-docs/word/sample-docx.docx";
 		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
 		MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
 		// Uncomment to see the raw XML
 		//System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
 		org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.DocumentdocumentPart
 		Body body = wmlDocumentEl.getBody();
 		new TraversalUtil(body,
 		new Callback() {
 			String indent = "";
			public List<Objectapply(Object o) {
				String text = "";
				if (o instanceof org.docx4j.wml.Text)
					text = ((org.docx4j.wml.Texto).getValue();
				..println( + o.getClass().getName() + "  \""
text + "\"");
				return null;
			public boolean shouldTraverse(Object o) {
				return true;
			// Depth first
			public void walkJAXBElements(Object parent) {
				 += "    ";
				List children = getChildren(parent);
				if (children != null) {
					for (Object o : children) {
						// if its wrapped in javax.xml.bind.JAXBElement, get its
						// value
						o = XmlUtils.unwrap(o);
						if (this.shouldTraverse(o)) {
			public List<ObjectgetChildren(Object o) {
				return TraversalUtil.getChildrenImpl(o);
New to GrepCode? Check out our FAQ X