Code Cutting

Well this page always seems to get a bit neglected when nothing much happens……

Something interesting that I have come across is the w3c DOM in java and how there isn’t a definitive guide to how to output it to pretty print, ie human readable, there were a few examples with xerces-j but I thought that wouldn’t work but I tried it for good measure and it works. Even with the option that appears to be for xerces. Heres the code.

TransformerFactory tFactory = TransformerFactory.newInstance();
try {
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);
    transformer.transform(source, new StreamResult(f));
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch (TransformerException e) {
    e.printStackTrace();
}

Have fun and I found a great plugin for doing the code colouring, iG:Syntax Highligher.


Leave a Reply