简体   繁体   English

Groovy:使用闭包创建和返回地图元素

[英]Groovy: Using Closure to create and return Map Elements

I am importing an XML and then creating a list of objects based on the information from the XML. 我正在导入XML,然后根据XML中的信息创建对象列表。

This is a sample of my XML: 这是我的XML的示例:

<DCUniverse>
   <SuperHeroes>
       <SuperHero>
           <SuperHeroName>SuperMan</SuperHeroName>
           <SuperHeroDesc>Surviver of Krypton; Son of Jor-el</SuperHeroDesc>
           <SuperHeroCode>SM</SuperHeroCode>
           <SuperHeroAttrs>
               <SuperHeroAttr Name="Strength">All</SuperHeroAttr>
               <SuperHeroAttr Name="Weakness">Kryptonite</SuperHeroAttr>
               <SuperHeroAttr Name="AlterEgo">Clark Kent</SuperHeroAttr>
           </SuperHeroAttrs>
       </SuperHero>
       <SuperHero>
           <SuperHeroName>Batman</SuperHeroName>
           <SuperHeroDesc>The Dark Knight of Gothom City</SuperHeroDesc>
           <SuperHeroCode>BM</SuperHeroCode>
           <SuperHeroAttrs>
               <SuperHeroAttr Name="Strength">Intellect</SuperHeroAttr>
               <SuperHeroAttr Name="Weakness">Bullets</SuperHeroAttr>
               <SuperHeroAttr Name="AlterEgo">Bruce Wayne</SuperHeroAttr>
           </SuperHeroAttrs>
       </SuperHero>
   </SuperHeroes>
<DCUniverse>

Here is an example of the groovy script that I am running to create the objects: 这是我正在运行的用于创建对象的groovy脚本的示例:

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 ->
    faction.SuperHero.each{ hero ->
        heroList += new Hero(   hero.SuperHeroName.text(),
                                hero.SuperHeroDesc.text(),
                                hero.SuperHeroCode.text(),
                                hero.SuperHeroAttrs.SuperHeroAttr.each{ attr ->
                                    return  [ (attr.'@Name') : (attr.text()) ]
                                })
    }
}

When I run the above code, I get the following error: 当我运行上面的代码时,出现以下错误:

java.lang.ClassCastException: groovy.util.Node cannot be cast to java.util.Map$Entry

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. 我有一种强烈的感觉,它与闭包试图发送给Hero Class Constructor的最后一个变量有关。 Commenting out 注释掉

this.SuperHeroAttrLst.putAll(attrLst)

in the Hero Constructor allows the script to at least parse correctly. 在Hero构造函数中,该脚本至少可以正确解析。 What I am trying to do is create a class based on the XML and place it in the list like: 我想做的是基于XML创建一个类,并将其放在列表中,例如:

heroList += new Hero('Batman',
 'The Dark Knight of Gothom City',
 'BM',
 ['Strength':'Intellect', 'Weakness':'Bullets', 'AlterEgo':'Bruce Wayne'] )

However, my variable typing is incorrect and I dont know enough about Groovy's (or Java's) syntax to make it work. 但是,我的变量类型输入不正确,我对Groovy(或Java)的语法了解不足,无法使其正常工作。

Any help that can be provided would be much appreciated. 可以提供的任何帮助将不胜感激。 Thank you for your time. 感谢您的时间。

I think you should change hero.SuperHeroAttrs.SuperHeroAttr.each{ //blah blah to: 我认为您应该将hero.SuperHeroAttrs.SuperHeroAttr.each{ //blah blah更改为:

hero.SuperHeroAttrs.inject([:]) { attributes, attr ->
    attributes[attr.'@Name'] = attr.text()
    return attributes
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM