Convert XML to Word in Java

Andrew Wilson
1 min readJun 17, 2022

An XML file is a rich text format used for documents with a lot of specific formatting involved. The XML file is designed to store data, but in certain cases, you may need to convert the XML files to Word. This article will share how to accomplish this task programmatically using Free Spire.Doc for Java.

Import JAR Dependency

Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

Sample Code

To convert an Xml file to Word, you just need to load an XML sample file and then convert it using using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class XMLToWord {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();

//Load an XML sample document
document.loadFromFile("E:\\Files\\test.xml");

//Save the document to Word
document.saveToFile("XMLToWord.docx", FileFormat.Docx );
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.