The Source for Java Technology Collaboration
User: Password:



Tom White

Tom White's Blog

User-Friendly XML Config

Posted by tomwhite on October 27, 2005 at 02:49 PM | Comments (2)

There's been a bit of a backlash against XML config files lately. The Ruby On Rails community has a crisp putdown: avoid "doing XML sit-ups". And the arrival of Java annotations in J2SE 5.0 seemed to muddy the waters somewhat - do I still need to use XML config? Well yes, they are really designed for different things (as Dennis Sosnoski points out).

So if XML config files are going to continue to be a part of a Java developer's life, it's worth making them a little easier to work with. A trick that I noticed in one of Nutch's XML config files was the use of an xml-stylesheet processing instruction to render the file in a nice table when viewed in a browser:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="nutch-conf.xsl"?>
<nutch-conf>
...
</nutch-conf>

The stylesheet, nutch-conf.xsl, simply transforms the configuration XML into an HTML table. You can view the result here. It's a technique that is used elsewhere - quite a few RSS feeds do it, for example, such as the BBC. In the context of Java XML config, I think it's a good way to make large config files more approachable. If you're new to a project it's easy to open the file in a browser and quickly scan through it. It's certainly easier than grokking the XML. So if you're writing an XML config file - please add some style!


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Hi!

    I use using the following style to display dependencies with an ant build.xml file.


    <?xml version="1.0" encoding="cp1252"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="project">
    <html><head>
    <style>
    .left { font-size: 15px; color: #000000; text-align: right; font-family: arial, helvetica, sans-serif; background: #eeeeee; vertical-align: text-middle; }
    .leftHigh { font-size: 15px; color: #FF0000; text-align: right; font-family: arial, helvetica, sans-serif; background: #eeeeee; vertical-align: text-middle; }
    .right { font-size: 15px; color: #000000; text-align: left; font-family: arial, helvetica, sans-serif; font-style: italic; background: #f7f7f7; vertical-align: text-top; }
    .rightDetail { font-size: 12px; color: #000000; text-indent: 15px; font-family: "Times New Roman", arial, helvetica, sans-serif; text-align: left; }
    </style>
    </head>
    <script type="text/javascript">
    function highlightList(origin, depends)
    {
    highlight(origin);
    var token = depends.split(", ");
    for (var i=0; i < token.length;i++)
    {
    highlight(token[i]);
    }
    }


    function highlight(dep)
    {
    element = document.getElementById(dep);
    if (element)
    {
    element.className = "leftHigh";
    if (element.getAttribute("depends"))
    {
    highlightList('', element.getAttribute("depends"));
    }
    }
    }
    </script>

    <body>
    <table border="0" width="100%">
    <xsl:apply-templates select="target" />
    </table>
    </body></html>
    </xsl:template>

    <xsl:template match="target">
    <tr>
    <td class="left">
    <xsl:if test="@description">
    <a depends="{@depends}" id="{@name}" href="javascript:highlightList('{@name}','{@depends}')">
    <b>[<xsl:value-of select="@name" />]</b>
    </a>
    </xsl:if>
    <xsl:if test="not(@description)">
    <a depends="{@depends}" id="{@name}" href="javascript:highlightList('{@name}','{@depends}')">
    [<xsl:value-of select="@name" />]
    </a>
    </xsl:if>
    </td>
    <td class="right">
    <xsl:apply-templates select="@description" />
    </td>
    </tr>
    <tr>
    <td colspan="2" height="2px" bgcolor="#000000"></td>
    </tr>
    </xsl:template>
    </xsl:stylesheet>


    Hopefully I don't break this site's javascript code with this sniplet here.

    Posted by: th_langer on October 31, 2005 at 01:30 AM

  • th_langer

    Thanks for sharing your ant style. I've just got it working and it's very neat. (BTW I had to change the encoding - cp1252 wasn't recognised by my browser - and escape the less than symbol in the highlightList function.)

    Tom

    Posted by: tomwhite on November 01, 2005 at 06:05 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds