| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.samples; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 25 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | public class DisplayMainDocumentPartXml extends AbstractSample { |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @param args |
|---|
| 33 | */ |
|---|
| 34 | public static void main(String[] args) throws Exception { |
|---|
| 35 | |
|---|
| 36 | /* |
|---|
| 37 | * You can invoke this from an OS command line with something like: |
|---|
| 38 | * |
|---|
| 39 | * java -cp dist/docx4j.jar:dist/log4j-1.2.15.jar |
|---|
| 40 | * org.docx4j.samples.DisplayMainDocumentPartXml inputdocx |
|---|
| 41 | * |
|---|
| 42 | * Note the minimal set of supporting jars. |
|---|
| 43 | * |
|---|
| 44 | * If there are any images in the document, you will also need: |
|---|
| 45 | * |
|---|
| 46 | * dist/xmlgraphics-commons-1.4.jar:dist/commons-logging-1.1.1.jar |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | try { |
|---|
| 50 | getInputFilePath(args); |
|---|
| 51 | } catch (IllegalArgumentException e) { |
|---|
| 52 | inputfilepath = System.getProperty("user.dir") |
|---|
| 53 | + "/sample-docs/sample-docx.xml"; |
|---|
| 54 | |
|---|
| 55 | // inputfilepath = System.getProperty("user.dir") |
|---|
| 56 | // + "/tmp/toc.docx"; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | // Open a document from the file system |
|---|
| 60 | // 1. Load the Package |
|---|
| 61 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 62 | |
|---|
| 63 | // 2. Fetch the document part |
|---|
| 64 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 65 | |
|---|
| 66 | // Display its contents |
|---|
| 67 | System.out.println( "\n\n OUTPUT " ); |
|---|
| 68 | System.out.println( "====== \n\n " ); |
|---|
| 69 | |
|---|
| 70 | org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement(); |
|---|
| 71 | |
|---|
| 72 | String xml = org.docx4j.XmlUtils.marshaltoString(wmlDocumentEl, true); |
|---|
| 73 | |
|---|
| 74 | System.out.println(xml); |
|---|
| 75 | |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | } |
|---|