gpt4 book ai didi

xml - 用grails解析XML的问题

转载 作者:行者123 更新时间:2023-12-02 14:55:28 25 4
gpt4 key购买 nike

我正在开发一个应用程序,该应用程序从Last.fm API中提取有关音乐事件的信息。
但是,由于某种原因,该应用程序仅解析XML文件中的前两个事件,我无法弄清原因。有什么帮助吗?

这是代码:

def resultList = []

if(connection.responseCode == 200){
def xml = connection.content.text
def lfm = new XmlSlurper().parseText(xml)
lfm.events.event.each {
def result = [:]
result.eventId = it.id as String
result.eventTitle = lfm.events.event.find { it.'id' == result.eventId }.title as String

resultList << result

}

最佳答案

我只是发现该错误位于Controller类的save {}闭包中。以下是带有该错误的代码:

def save = {
resultsList.each{
def eventInstance = new Event(params + it)
if (eventInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.id])}"
redirect(action: "show", id: eventInstanceID)
}
else {
render(view: "create", model: [eventInstance: eventInstance])
}
}
}

因此,问题在于我正在重定向每个{}闭包内的 Action “显示”。由于某种原因,我仍然不确定,它在each {}循环的第二次迭代后引发了异常。因此,我要做的就是在each {}闭包之外调用redirect方法,如下所示:
   def save = {
def resultsList = eventService.lastFmVenues(params.artist)
def eventInstanceID
resultsList.each{
def eventInstance = new Event(params + it)
if (eventInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.id])}"
eventInstanceID = eventInstance.id
}
else {
render(view: "create", model: [eventInstance: eventInstance])
}
}
redirect(action: "show", id: eventInstanceID)
}

我仍然很好奇为什么仅在第二次迭代后才引发异常,但是无论如何,问题都得到了解决。谢谢!

关于xml - 用grails解析XML的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6361578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com