---
title: 'Simple OpenOffice templating with the ODF toolkit'
url: 'https://tutego.de/blog/javainsel/2010/06/simple-openoffice-templating-with-the-odf-toolkit'
markdown: 'https://tutego.de/blog/javainsel/2010/06/simple-openoffice-templating-with-the-odf-toolkit.md'
date: '2010-06-29'
description: 'To open an OpenOffice text document and access the text you first have to download odfdom-0.8.5-binaries.zip (Homepage of ODF TOOLKIT) and add odfdom.jar to your classpath together with a recent Xerces implementation like xercesImpl-2.9.0.jar. Then you can start with Java: File file = new File( "C:…'
taxonomy:
  category:
    - 'Open Source'
---

# Simple OpenOffice templating with the ODF toolkit

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Simple OpenOffice templating with the ODF toolkit

29.06.2010 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

To open an OpenOffice text document and access the text you first have to download odfdom-0.8.5-binaries.zip ([Homepage of ODF TOOLKIT](http://odftoolkit.org/)) and add odfdom.jar to your classpath together with a recent Xerces implementation like `xercesImpl`-2.9.0.jar. Then you can start with Java:

```java
File file = new File( "C:/Users/Christian/Desktop/TEMPLATE.odt" );
OdfTextDocument odf = (OdfTextDocument) OdfTextDocument.loadDocument( file );
OdfOfficeText contentRoot = odf.getContentRoot();
```

The `OdfOfficeText` gives access to the XML-structure of the document. If you print it out

```java
System.out.println( contentRoot );
```

it looks like this:

```
<office:text>
```

<office:forms form:apply-design-mode="`false`"

```
 form:automatic-focus="false"></office:forms>
 <text:variable-decls>
```

…

```
 </text:p>
</office:text>
```

With regular XML-DOM operations you can now modify the document.

To use a template you have to a) find a certain element you want to fill b) modify it and c) save the document.

a) For templates you can use user variables. If you set a variable it looks like this in XML:

```
<text:variable-set office:value-type="string" text:name="COURSETITLE">COURSETITLE</text:variable-set>
```

(Date and other formats look different!)

b) After your found the XML element either by hand or XPath, you can modify an entry with `setNodeValue()` or different method if the value is stored in attributes.

```java
c) Save the modified document with odf.save( file );
```

A simple but complete example to read a document, set variables and write the modified ODT:

```java
import java.io.File;
import java.util.*;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.doc.office.OdfOfficeText;
import org.w3c.dom.*;

public class OdtTemplate
{
 private OdfTextDocument odf;
 private Map<String, String> map = new HashMap<String, String>();
```

public static void main( `String`\[\] args ) throws `Exception`

```java
 {
 File file1 = new File( "C:/Users/Christian/Desktop/TEMPLATE.odt" );
 File file2 = new File( "C:/Users/Christian/Desktop/result.odt" );

 OdtTemplate template = new OdtTemplate();
 template.readOdt( file1 );
 template.setVariable( "COURSETITLE", "Donuts backen für Kernkraftfahrer" );
 template.setVariable( "COURSECODE", "DONUT" );
 template.setVariable( "CREATOR", "ull" );
 template.saveOdt( file2 );
 }

 public void readOdt( File file ) throws Exception
 {
 odf = (OdfTextDocument) OdfTextDocument.loadDocument( file );
 }

 public void setVariable( String key, String value )
 {
 map.put( key, value );
 }

 public void saveOdt( File file ) throws Exception
 {
 OdfOfficeText contentRoot = odf.getContentRoot();
 iteratorOverEveryVariableSet( contentRoot.getChildNodes() );
 odf.save( file );
 }

 private void iteratorOverEveryVariableSet( NodeList childNodes )
 {
 for ( int i = 0; i < childNodes.getLength(); i++ )
 {
 Node item = childNodes.item( i );

 if ( item instanceof NodeList )
 iteratorOverEveryVariableSet( item.getChildNodes() );

 if ( item.getNodeName().equals( "text:variable-set" ) )
 {
 String nodeValue = item.getAttributes().getNamedItem( "text:name" ).getNodeValue();
 item.getChildNodes().item( 0 ).setNodeValue( map.get( nodeValue ) );
 }
 }
 }
}
```

Kategorien: [Open Source](https://tutego.de/blog/javainsel/category/open-source/)

## 1 Kommentare

**Christian** · [01.07.2010 05:53](#comment-3379)

Beware: This doesnt work on the Google App Engine because the lib wants to create temp. files!

Die bisherigen Kommentare wurden aus WordPress übernommen. Neue Kommentare sind derzeit geschlossen.

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Erste Realisierung von ARM-Blöcken](https://tutego.de/blog/javainsel/2010/06/erste-realisierung-von-arm-blcken.md)
