Note that there are some explanatory texts on larger screens.

plurals
  1. POGroovy: Using Closure to create and return Map Elements
    text
    copied!<p>I am importing an XML and then creating a list of objects based on the information from the XML.</p> <p>This is a sample of my XML:</p> <pre><code>&lt;DCUniverse&gt; &lt;SuperHeroes&gt; &lt;SuperHero&gt; &lt;SuperHeroName&gt;SuperMan&lt;/SuperHeroName&gt; &lt;SuperHeroDesc&gt;Surviver of Krypton; Son of Jor-el&lt;/SuperHeroDesc&gt; &lt;SuperHeroCode&gt;SM&lt;/SuperHeroCode&gt; &lt;SuperHeroAttrs&gt; &lt;SuperHeroAttr Name="Strength"&gt;All&lt;/SuperHeroAttr&gt; &lt;SuperHeroAttr Name="Weakness"&gt;Kryptonite&lt;/SuperHeroAttr&gt; &lt;SuperHeroAttr Name="AlterEgo"&gt;Clark Kent&lt;/SuperHeroAttr&gt; &lt;/SuperHeroAttrs&gt; &lt;/SuperHero&gt; &lt;SuperHero&gt; &lt;SuperHeroName&gt;Batman&lt;/SuperHeroName&gt; &lt;SuperHeroDesc&gt;The Dark Knight of Gothom City&lt;/SuperHeroDesc&gt; &lt;SuperHeroCode&gt;BM&lt;/SuperHeroCode&gt; &lt;SuperHeroAttrs&gt; &lt;SuperHeroAttr Name="Strength"&gt;Intellect&lt;/SuperHeroAttr&gt; &lt;SuperHeroAttr Name="Weakness"&gt;Bullets&lt;/SuperHeroAttr&gt; &lt;SuperHeroAttr Name="AlterEgo"&gt;Bruce Wayne&lt;/SuperHeroAttr&gt; &lt;/SuperHeroAttrs&gt; &lt;/SuperHero&gt; &lt;/SuperHeroes&gt; &lt;DCUniverse&gt; </code></pre> <p>Here is an example of the groovy script that I am running to create the objects:</p> <pre><code>class Hero{ def SuperHeroName def SuperHeroDesc def SuperHeroCode def SuperHeroAttrLst = [:] Hero(String name, String desc, String code, attrLst){ this.SuperHeroName=name this.SuperHeroDesc=desc this.SuperHeroCode=code this.SuperHeroAttrLst.putAll(attrLst) } } def heroList = [] def heroDoc = new XmlParser().parse('dossier.xml') heroDoc.SuperHeroes.each{ faction -&gt; faction.SuperHero.each{ hero -&gt; heroList += new Hero( hero.SuperHeroName.text(), hero.SuperHeroDesc.text(), hero.SuperHeroCode.text(), hero.SuperHeroAttrs.SuperHeroAttr.each{ attr -&gt; return [ (attr.'@Name') : (attr.text()) ] }) } } </code></pre> <p>When I run the above code, I get the following error:</p> <pre><code>java.lang.ClassCastException: groovy.util.Node cannot be cast to java.util.Map$Entry </code></pre> <p>I have a strong feeling that it has something to do with the last variable that the closure is trying to send to Hero Class Constructor. Commenting out </p> <pre><code>this.SuperHeroAttrLst.putAll(attrLst) </code></pre> <p>in the Hero Constructor allows the script to at least parse correctly. What I am trying to do is create a class based on the XML and place it in the list like:</p> <pre><code>heroList += new Hero('Batman', 'The Dark Knight of Gothom City', 'BM', ['Strength':'Intellect', 'Weakness':'Bullets', 'AlterEgo':'Bruce Wayne'] ) </code></pre> <p>However, my variable typing is incorrect and I dont know enough about Groovy's (or Java's) syntax to make it work.</p> <p>Any help that can be provided would be much appreciated. Thank you for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload