Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Java

Journal trajano's Journal: Pretty printing XML in Java 1

Transformer transformer =
    TransformerFactory.newInstance().newTransformer(
        new StreamSource(
            new StringReader(
                "<?xml version=\"1.0\"?>"
                    + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
                    + "<xsl:output method=\"xml\" indent=\"yes\" />"
                    + "<xsl:strip-space elements=\"*\"/>"
                    + "<xsl:template match=\"/\">"
                    + "<xsl:copy-of select=\".\"/>"
                    + "</xsl:template>"
                    + "<xsl:template match=\"text()\">"
                    + "<xsl:value-of select=\"normalize-space()\"/>"
                    + "</xsl:template>"
                    + "</xsl:stylesheet>")));
StringWriter writer = new StringWriter();
transformer.transform(
    new StreamSource(new StringReader(message)),
    new StreamResult(writer));
This discussion has been archived. No new comments can be posted.

Pretty printing XML in Java

Comments Filter:
  • The problem with this code above is that it will shift everything to the left, the indent="yes" just gets ignored. Supposedly I may be able to use SAXON, but I am trying to do it within JDK 1.4.1.

"The only way I can lose this election is if I'm caught in bed with a dead girl or a live boy." -- Louisiana governor Edwin Edwards

Working...