2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

11/19/2003: Using JudoScript to Convert XML into Java HashMap.

I’m finally a convert to the ranks of JudoScripters.

The following example doesn’t show the Java code (about 15 lines of code) that invokes the JudoScript because I wanted to keep this entry small.

My task was to maintain some information external to my Java program so I chose an XML format:

  <requiredNodes>
    <message type="all">
      <requiredNode name="messageHeader" xpath="/eanucc:envelope/messageHeader"/>
    </message>
    <message type="eanucc:tradeItemDocument">
      <requiredNode name="tid.gtin" xpath="/eanucc:envelope/../gtin"/>
      <requiredNode name="tid.glnOfInformationProvider" xpath="/eanucc:envelope/../gln"/>
    </message>
  </requiredNodes>

In order to convert this file into an easily used Java object, I used JudoScript:

	// ReadRequiredNodes.judo

	requiredNodes = javanew java.util.HashMap();
	$$bsf.registerBean("requiredNodes", requiredNodes);

	do BRRequiredNodesFileName as xml {
		<message>:
			messageMap = requiredNodes.get($_.getAttrValue(0));
			if messageMap == null {
				messageMap = javanew java.util.HashMap();
				requiredNodes.put($_.getAttrValue(0), messageMap);
			}
		<requiredNode>:
			messageMap.put($_.getAttrValue(1), $_.getAttrValue(0));
	}

Some issues to note:

  • When using SAX-based XML processing, I needed to use attributes to hold the name and xpath because the XML processing in JudoScript seems to have a flaw. The $_ variable (the current xml node) can't be used as the key in a HashMap.
  • When using SAX-based XML processing, There is no way to access attributes by name. Therefore the script uses index numbers which is brittle coding.

Overall, I found that JudoScript was easy to use and quite a compact way to handle the XML->HashMap transformation.

10/22/2003: Reading XML Files Using XSLT

I’m learning how to represent non-English data in XML and ran into an article titled “Well-structured XML Goes Cosmopolitan” written by Ilari Aarnio and hosted by DevX.com. Buried in the article was an interesting technique that showed how to read XML documents from within XSLT.

<?xml version=”1.0” encoding=”UTF-8”?>
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns=”http://www.w3.org/1999/xhtml”>
    <xsl:param name=”currLang”>en</xsl:param>
    <xsl:output method=”xml” encoding=”UTF-8” indent=”yes” doctype-public=”-//W3C//DTD XHTML 1.0 Strict//EN”/>

    <xsl:template match=”/”>
        <xsl:variable name=”cvData”>
            <xsl:text>cv-</xsl:text>
            <xsl:value-of select=”$currLang”/>
            <xsl:text>.xml</xsl:text>
    </xsl:variable>
    <xsl:apply-templates select=”document($cvData)”/>
    </xsl:template>

    <!– add normal XSLT processing here –>

</xsl:stylesheet>