Mirco
Mirco

Reputation: 1

Java - Convert XML file to Docx file

I have a problem with which I get stuck after several different solutions that I have tried.

I have an XML file template that I have generated from my Word template. I convert this XML document into a string and search it for a keyword, which I replace with another string. Then I create an XML document from this string again:

String xmlAsString = "XYZ"; // My XML String    

    try {
            java.io.FileWriter fw = new FileWriter("src/test/resources/test.xml");
            fw.write(xmlAsString);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

Now I want to convert this XML document back to a valid DOCX document, but I can not find a solution to this problem.

Upvotes: 0

Views: 4168

Answers (2)

JasonPlutext
JasonPlutext

Reputation: 15863

There are many ways to tackle your problem. Examples here use docx4j.

The first is simple content replacement (eg replace "${titleofproject}" with "I am the title"); see https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/VariableReplace.java but this is fragile (split runs) and limited to simple text replacement.

The second is replacing the content in the MainDocument part (document.xml) at an XML level:

String xml = wordMLPackage.getMainDocumentPart().getXML();
// do something
String result = xml;
// now inject your result contents
wordMLPackage.getMainDocumentPart().unmarshal(
        new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));

The third is to work with FlatOPC XML (where the whole zip file is represented in a single XML file); see https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/ConvertOutFlatOpenPackage.java and ConvertInFlatOpenPackage.java

But a better alternative to working at the XML level as above, is generally to use content control XML data binding.

Upvotes: 1

Brajesh
Brajesh

Reputation: 1555

I am not sure but Might be it is helpful for you. This code snapshot will convert your .xml file into .docx file.

public class ReadXmlFile {       
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { 

            File xmlFile = new File("inFile.xml"); 

            Reader fileReader = new FileReader(xmlFile); 
            BufferedReader bufReader = new BufferedReader(fileReader); 
            StringBuilder sb = new StringBuilder(); 
            String line = bufReader.readLine();

            Writer out = new FileWriter("outFile.docx");      

            while( line != null)
            { 
                sb.append(line).append("\n");  
                line = bufReader.readLine();
                if(line !=null)                    
                 out.write(line);
              } 
            out.close();
            fileReader.close();

            String xml2String = sb.toString();
            System.out.println(xml2String);

          }
}

Upvotes: 0

Related Questions