关于 Grails 地图的帮助

发布于 2024-12-01 20:30:36 字数 939 浏览 1 评论 0原文

我正在尝试动态创建地图集合,但我对 grails 仍然很陌生,希望有人可以帮助我。我想要做的是解析 xml 文件并将值添加到地图中。我已经解析完毕,但只是不知道如何动态地将节点值添加到地图中。这是我到目前为止所拥有的:

示例 xml 流:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <connections total="29">
      <person>
        <id>123245</id>
        <first-name>me</first-name>
        <last-name>you</last-name>          
      </person>
    </connections>
 </person>

然后我像这样解析它:

  def alum = new XmlSlurper().parseText(xmlResponse)
   alum.connections.person.each{ conName ->
print conName.'id'.toString() + " " +  conName.'first-name'.toString() + " " + conName.'last-name'.toString() + "\n"
   }

因此,这允许我迭代并解析 xml 流。我的问题是,如果我想动态地将值添加到这样的地图中:

   def myMap= [fName:"SomeName", lName:"Sme last Name", id:1234]

我该怎么做?

谢谢 杰森

I'm trying to dynamically create a map collection but I am still new to grails and was hoping someone could help me. What I want to do is parse and xml file and add the values to a map. I've got the parsing down, but just dont know how to dynamically add the node values to the map. here's what i have so far:

example xml stream:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <connections total="29">
      <person>
        <id>123245</id>
        <first-name>me</first-name>
        <last-name>you</last-name>          
      </person>
    </connections>
 </person>

I then parse it like this:

  def alum = new XmlSlurper().parseText(xmlResponse)
   alum.connections.person.each{ conName ->
print conName.'id'.toString() + " " +  conName.'first-name'.toString() + " " + conName.'last-name'.toString() + "\n"
   }

So, this allows me to iterate over, and parse, the xml stream. my question is, if i wanted to add the values, dynamically, to a map like this:

   def myMap= [fName:"SomeName", lName:"Sme last Name", id:1234]

how would i do this?

Thank you
jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚殇 2024-12-08 20:30:36

如果您不知道子节点名称并希望将它们用作映射中的键,请使用:

def alum = new XmlSlurper().parseText(xmlResponse)
alum.connections.person.each { conName ->
   def myMap = [:]
   conName.children().each { child -> myMap[child.name()] = child.text() }
}

这将导致 [id: '123245', 'first-name': 'me', ' last-name': 'you']

无关:您可以使用 GString 缩短调试代码:

print "${conName.'id'} ${conName.'first-name'} ${conName.'last-name'}\n"

If you don't know the child node names and want to use them as the keys in the map, use this:

def alum = new XmlSlurper().parseText(xmlResponse)
alum.connections.person.each { conName ->
   def myMap = [:]
   conName.children().each { child -> myMap[child.name()] = child.text() }
}

This will result in [id: '123245', 'first-name': 'me', 'last-name': 'you']

Unrelated: you can shorted up your debug code with a GString:

print "${conName.'id'} ${conName.'first-name'} ${conName.'last-name'}\n"
三五鸿雁 2024-12-08 20:30:36

好吧,我最终只使用了一个多维数组,这似乎工作得很好。再次感谢您的帮助

int i=0
String[][] friends = new String[test][4]
alum.connections.person.each{ conName ->
friends[i][0] =conName.'id'.toString()
friends[i][1] =conName.'first-name'.toString()
friends[i][2] =conName.'last-name'.toString()
friends[i][3] =conName.'picture-url'.toString()
i++
}
[Friends:friends]

通过这种方式,我能够将其传递到我的常规页面并迭代数组

well, i ended up just using a multidimensional array, and that seems to have worked fine. Thanks again for your help

int i=0
String[][] friends = new String[test][4]
alum.connections.person.each{ conName ->
friends[i][0] =conName.'id'.toString()
friends[i][1] =conName.'first-name'.toString()
friends[i][2] =conName.'last-name'.toString()
friends[i][3] =conName.'picture-url'.toString()
i++
}
[Friends:friends]

This way, i was able to pass it on to my groovy page and iterate over the array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文