Here is a way to create xml dynamically in Java. We start with an arbitrary non-xml representation of the data and then make a DOM. We can then use LSSerializer to write to a file, or in the case below, to convert to a String. The source code is HERE.

import org.w3c.dom.*;


import java.util.*;

import javax.xml.parsers.*;

public class XmlGenerator {
    public static void main(String[] args) throws Exception {
        String xml = "ROOT(PARAMS,PARAM1,PARAM2,PARAM3)";
        Scanner s = new Scanner(xml);

        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()

        Node root = doc.createElement(;

        Node sheet = doc.createElement(;

        while (s.hasNext()) {
            Node param = doc.createElement(;


    public static String getStringFromDoc(org.w3c.dom.Document doc) {
        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        LSSerializer lsSerializer = domImplementation.createLSSerializer();
                    .setParameter("format-pretty-print", Boolean.TRUE);

        return lsSerializer.writeToString(doc);

Please support this site using the button below: