2014-09-22 3 views
0

"나는 예상대로 작동하는 코드를 가지고 있지만 지금은 다른 형식의 요소를 찾을 수있다. 예 아래XML Slurper를 사용하여 XML의 요소를 찾기

<car-load> 
<car-model model="i10"> 
    <model-year> 
     <year.make> 
      <name>corolla</name> 
     </year.make> 
    </model-year> 
</car-model> 
</car-load> 

하다 내가 값을 찾을 수있다 이 XML에서 "화관". 회신 해 주시기 바랍니다.

+0

이 homeworky 소리 콘솔. 그리고 도대체 넣어의 기간이 실행할 수 있습니다!? – billjamesdev

답변

2

당신은 XML 태그 이름의 그루비

def text = ''' 
<car-load> 
<car-model model="i10"> 
    <model-year> 
     <year.make> 
      <name>corolla</name> 
     </year.make> 
    </model-year> 
</car-model> 
</car-load>''' 


def records = new XmlSlurper().parseText(text) 

// a quick and dirty solution 
assert 'corolla' == records.toString() 

// a more verbose, but more robust solution that specifies the complete path 
// to the node of interest 
assert 'corolla' == records.'car-model'.'model-year'.'year.make'.name.text()