2009-10-05 2 views
1

도메인 클래스에 기반한 스키마로 XML 파일을 읽었습니다. 여기 Grails 도메인 클래스로 XML 가져 오기

는 (현재의 내 상황이 많은 클래스에서 필드의 많은 우려) 그림에 대한 간단한 예입니다 : 읽을 수

class Player { 
    String name 
    Date birthDate 
} 

XML 파일은 다음과 같습니다 그래서 내

<players> 
<player name='P1' birthDate='12-09-1983'/> 
</players> 

질문 : XML 파일을 구문 분석 할 때 다음 Groovy 코드로 Player 인스턴스를 만듭니다.

def players = new XmlSlurper().parse(xmlFile) 
players.player.each() {p -> 
    new Player(name: [email protected], birthDate: [email protected]).save() 
} 

더 간단한 방법이 있습니까? new Player(params) 또는 player.properties = params과 같은 코드를 사용하여 도메인 객체를 만들거나 업데이트 할 때 params 바인딩과 비슷합니까?

답변

8

사실 도메인 클래스 생성자에 속성 목록 ()을 직접 입력 할 수 있습니다 ().

def players = new XmlSlurper().parse(xmlFile) 
players.player.each() {p -> 
    new Player(p.attributes()).save() 
} 
+0

Thx! 이것은 내가 필요로했던 exacly이었다. – fabien7474