1

I am trying to work with the Apache Commons Configuration XML class and I think I have a basic configuration writer working. Now I need to have the XML output indented correctly.

I have tried implementing the solution from this other post (The answer by dfa). The problem I am running into is the XML data gets written out with line breaks, so it's not passed as a single line to the prettyFormat class and it's not indenting the data. Instead, it's just writing it out the same as before.

Here is my code for making the config file:

import java.io.StringWriter;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class Config {

    public static void load () {

        XMLConfiguration xmlConfig = new XMLConfiguration();

        xmlConfig.setRootElementName("config");
        xmlConfig.addProperty("vmware.ssh.username", "root");
        xmlConfig.addProperty("vmware.ssh.password", "password");

        StringWriter sw = new StringWriter();

        // Save the XML data to StringWriter.
        try {               
            xmlConfig.save(sw);    

        } catch (ConfigurationException ce) {               
            ce.printStackTrace();               
        }

        String xmlOutput = XMLtransform.prettyFormat(sw.toString(), 2);         
        System.out.print(xmlOutput);            
    }       
}

I probably should mention also that my Java skills are still somewhat novice.

Community
  • 1
  • 1
AtomicPorkchop
  • 2,625
  • 5
  • 36
  • 55
  • Possible duplicate of [Format XML output with apache commons configuration XMLConfiguration][1] [1]: http://stackoverflow.com/questions/5761610/format-xml-output-with-apache-commons-configuration-xmlconfiguration/16211353#16211353 – Ozgun Alan Apr 25 '13 at 09:43

0 Answers0