Thursday, June 7, 2012

ByteArrayOutputStream.toString() human readable

I can do this for a readable output:

public class Changer {
    public static void main(String[] args) {
        try {
        java.io.InputStream in = new java.io.FileInputStream(new java.io.File("DBSDashboard.XML"));
        java.io.OutputStream out = new java.io.ByteArrayOutputStream();
        org.jdom2.input.SAXBuilder builder = new org.jdom2.input.SAXBuilder();
        org.jdom2.Document document = (org.jdom2.Document) builder.build(in);
        org.jdom2.Element rootNode = document.getRootElement();
        org.jdom2.Element ccc= rootNode.getChild("CallCentreClosed").getChild("callCentreClosed");
        ccc.setText("true");
        /* or, ccc.setText("false") */
        org.jdom2.output.XMLOutputter xmlOutputter = new org.jdom2.output.XMLOutputter();
        xmlOutputter.setFormat(org.jdom2.output.Format.getPrettyFormat());
        xmlOutputter.output(document, out);
        System.out.println(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And voila:



 
    false
 
(...)

Good to know.