XML Versus Text Files


The JDepend package dependency analyzer can output its results either as XML or as plain text. Instead of using the XML output, I found myself processing the text output using awk. Am I becoming tied to old-world thinking, or are text files easier to process?

One non trivial query I used involved printing all packages with a low abstractness and instability value (A < 0.1 and I < 0.1). My awk one liner was:

awk -F, 'NF == 9{if ($6 < .1 && $7 < .1) print $1}' all.txt
To see how XML-style processing measured up, my colleague, Vassilios Karakoidas, an experienced XML user, reimplemented the query for JDepend's XML output using XSL. Here is his implementation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
	<xsl:output method="text" />
	<xsl:template match="/">
		<xsl:apply-templates select="JDepend/Packages" />
	<xsl:template match="Package">
		<xsl:for-each select="Stats">
			<xsl:if test="(current()/A &lt; 0.1) and (current()/I &lt; 0.1)">
				<xsl:value-of select="../@name" />

The XSL script is less cryptic, containing the names of the actual values (A and I) in the places where the awk script uses positional parameters ($6 and $7). The structure's parsing is also explicit, whereas the awk script uses a heuristic, guessing the applicable lines by the number of fields they contain (NF = 9). The XML script is also more robust, since it can easily withstand the addition of more elements in the XML schema. On the other hand, the awk one liner is at least one order of magnitude shorter; I don't think I would have the patience to write the XSL script to obtain the same result.

My conclusion: I will continue to use text tool one-liners for anything that is not planned to become part of a production quality system.

Comments   Toot! Share

Last modified: Friday, February 18, 2005 1:05 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.