关于 Grails 地图的帮助
我正在尝试动态创建地图集合,但我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不知道子节点名称并希望将它们用作映射中的键,请使用:
这将导致
[id: '123245', 'first-name': 'me', ' last-name': 'you']
无关:您可以使用 GString 缩短调试代码:
If you don't know the child node names and want to use them as the keys in the map, use this:
This will result in
[id: '123245', 'first-name': 'me', 'last-name': 'you']
Unrelated: you can shorted up your debug code with a GString:
好吧,我最终只使用了一个多维数组,这似乎工作得很好。再次感谢您的帮助
通过这种方式,我能够将其传递到我的常规页面并迭代数组
well, i ended up just using a multidimensional array, and that seems to have worked fine. Thanks again for your help
This way, i was able to pass it on to my groovy page and iterate over the array