English 中文(简体)
Groovy:利用封锁制作和归还地图要素
原标题:Groovy: Using Closure to create and return Map Elements

我正在进口一项XML,然后根据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>

这里是我用来制造物体的粗体文字的一个例子:

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()) ]
                                })
    }
}

在我操作上述法典时,我有以下错误:

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

我强烈地感到,它有一些东西可以与最后几个变数做,即封锁正试图向海洛因级司机运送。 评论

this.SuperHeroAttrLst.putAll(attrLst)

在海洛因司机中,该笔药至少能正确穿透。 我试图做的是,根据《国际洗钱法》设立一个班级,并将其列入名单如下:

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

然而,我不同的打字不正确,我全心全意知道格罗莫夫· s(或 Java)的辛塔克斯,以便发挥作用。

可以提供的任何帮助都将受到高度赞赏。 感谢你时间。

最佳回答

我认为,你应当修改<代码>hero.SuperHeroAttrs.SuperHeroAttr.each{//blah blah:

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

暂无回答




相关问题
Closure/scope JavaScript/jQuery

I m trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I m not quite getting it to work. First, all the JS works outside my anonymous ...

Nested Lambdas in Python

I m a beginning python programmer, and I d like someone to clarify the following behavior. I have the following code: env = lambda id: -1 def add(id, val, myenv): return lambda x: val if x == ...

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a long field. List<Long> ids = session....

Is nested XMLHttpRequests with multiple closures a good idea?

I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the ...

Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...