Java: Convert XML to Word

An XML file is a plain text file that uses custom tags to display a document's structure and other features. In daily work, you sometimes need to convert Word to XML for storing and organizing data, or convert XML to Word for working on them more easily and efficiently. This article will demonstrate how to programmatically convert XML to Word using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Convert XML to Word

The following are steps to convert XML to Word using Spire.Doc for Java.

  • Create a Document instance.
  • Load an XML sample document using Document.loadFromFile() method.
  • Save the document as a Word file using Document.saveToFile() method.
  • Java
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(sample.xml");

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

Java: Convert XML to Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.