2014-09-22 76 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。而誰到底放的時間在XML標記名稱運行此!? – billjamesdev 2014-09-23 01:17:26

回答

2

可以在Groovy的控制檯

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()