[mule-user] XML pretty printing (indentation)

114 views
Skip to first unread message

Davide Piazza

unread,
Dec 18, 2007, 9:17:55 AM12/18/07
to us...@mule.codehaus.org
Hi,
I have my config that takes an XML file in input validates it and apply and XSLT transformation before saving the file again on file system.
The only issue I have is that XML is not indented. Is there a way to do that? I looked for indent property on the DomDocumentToXml transformer but I found nothing.

thanks
Davide.

Holger Hoffstätte

unread,
Dec 18, 2007, 9:34:28 AM12/18/07
to us...@mule.codehaus.org

Believe it or not: it's not easy. See for example:
http://raz.cx/blog/2005/12/java-xml-and-pretty-printing.html
for a quick overview of this mess. IOW the official API does not support
it, while the underlying implementation does - but you would have to use a
hardcoded/private class that may or may not be where you expect it in the
next JDK. :-(
We would have to add either an optional XSLT pass for pretty-printing with
a "default" stylesheet (can there be such a thing? no idea) or pipe the
transformed document through another pretty-printer. Maybe you could come
up with an XMLPrettyPrinterTransformer :)
Needless to say, I'm somewhat sceptical that this is such an important
feature - contrary to popular belief XML is meant to be read by machines,
not humans..

-h

---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Curtis Boyden

unread,
Dec 18, 2007, 10:44:48 AM12/18/07
to us...@mule.codehaus.org
Have you tried the <xsl:output/> element with the indent="yes" parameter? My test did not yield indentation, but it did break it into lines. My main solution, if I am just spot checking the XML, is to open it with a browser, they automatically Pretty Print it for you.

Other then that, I have to agree with Holger that XML is meant to be used by machines and that a better tool then Mule could be used for actually editing them.


------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

<xsl:template match ="/" >
<xsl:copy-of select="/" />
</xsl:template>

</xsl:stylesheet>
-------------

Hope that helps.

Curtis Boyden

Davide Piazza

unread,
Dec 18, 2007, 11:56:31 AM12/18/07
to us...@mule.codehaus.org
Thanks for the reply
I understand the point about human readibility of XML.
Anyway something may be easily done using dom4j (http://www.dom4j.org/faq.html#pretty-print).

thanks
Davide.


2007/12/18, Curtis Boyden <Curtis...@islandone.com>:

Davide Piazza

unread,
Dec 18, 2007, 11:56:31 AM12/18/07
to us...@mule.codehaus.org
Thanks for the reply
I understand the point about human readibility of XML.
Anyway something may be easily done using dom4j (http://www.dom4j.org/faq.html#pretty-print).

thanks
Davide.


2007/12/18, Curtis Boyden <Curtis...@islandone.com>:
Have you tried the <xsl:output/> element with the indent="yes" parameter? My test did not yield indentation, but it did break it into lines. My main solution, if I am just spot checking the XML, is to open it with a browser, they automatically Pretty Print it for you.

Holger Hoffstätte

unread,
Dec 18, 2007, 12:27:45 PM12/18/07
to us...@mule.codehaus.org
Davide Piazza wrote:
> Thanks for the reply
> I understand the point about human readibility of XML.
> Anyway something may be easily done using dom4j
> (http://www.dom4j.org/faq.html#pretty-print).

Very nice! That would be a welcome addition to the code, especially since
dom4j is already in the list of xml module dependencies. Can you try to
add that to the XsltTransformer or write a new PrettyPrinter and attach it
to a JIRA?
This has come up in the past so I guess it would be useful for others as well.

Holger

Davide Piazza

unread,
Dec 18, 2007, 4:15:03 PM12/18/07
to us...@mule.codehaus.org
Opened Jira
http://mule.mulesource.org/jira/browse/MULE-2818

Davide.

2007/12/18, Holger Hoffstätte < hol...@wizards.de>:

Carlos Escobar

unread,
Dec 18, 2007, 5:14:28 PM12/18/07
to us...@mule.codehaus.org
Does anybody get a:

java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

When trying to start the Config visualizer via shell on linux?

I have my mule installation @my home dir and looks like this:

/home/cescobar/Mule

I have it linked to /opt/mule and added the latter to my PATH and
MULE_HOME, I also did a symlink to the same directory called mule_base,
but didn't add that var.

The error happened after I typed:

cescobar@oceano:~/Mule/bin$ ./visualizer

The complete StackTrace:

java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getMethod0(Class.java:2670)
at java.lang.Class.getMethod(Class.java:1603)
at
org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:122)
at
org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:160)

I checked the jars @ $MULE_HOME/lib/opt and all the ones asked by the
script are there.. I even chmod'ed them 755 (from 644) but nothing
happened :(

It's not necessary for my project but I thought it'd be useful to avoid
writing the config by "hand" =P

Andrew Perepelytsya

unread,
Dec 18, 2007, 5:42:36 PM12/18/07
to us...@mule.codehaus.org
Could it be a symlink issue? Try referencing it via a direct path.

Andrew

On Dec 18, 2007 5:14 PM, Carlos Escobar <cesc...@bbr.cl> wrote:
Does anybody get a:

java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

When trying to start the Config visualizer via shell on linux?

I have my mule installation @my home dir and looks like this:

/home/cescobar/Mule

I have it linked to /opt/mule and added the latter to my PATH and
MULE_HOME, I also did a symlink to the same directory called mule_base,
but didn't add that var.

The error happened after I typed:

cescobar@oceano:~/Mule/bin$ ./visualizer

The complete StackTrace:

java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
       at java.lang.Class.getDeclaredMethods0(Native Method)
       at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
       at java.lang.Class.getMethod0(Class.java:2670)
       at java.lang.Class.getMethod(Class.java:1603)
       at
org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:122)
       at
org.codehaus.groovy.tools.GroovyStarter.main (GroovyStarter.java:160)

Carlos Escobar

unread,
Dec 19, 2007, 9:15:59 AM12/19/07
to us...@mule.codehaus.org
Yup. Definitely, java doesn't like symlinks, instead I used absolute paths, worked just fine. Thanks Andrew.

Carlos.

Andrew Perepelytsya escribió:

Carlos Escobar

unread,
Dec 19, 2007, 9:22:51 AM12/19/07
to us...@mule.codehaus.org
Forgot to mention, this time I changed MULE_HOME to /home/cescobar/Mule and pointed a MULE_BASE env. var. to he same place as MULE_HOME, I think that did the trick ;-)

Carlos Escobar escribió:
Reply all
Reply to author
Forward
0 new messages