groovy - Extracting an xml value in Groovy
问题描述
I have this code
String dbresponse = '''
<rows>
<row>
<file_data>One</file_data>
<time_inserted>2019-01-30T10:29:20.543</time_inserted>
</row>
<row>
<file_data>two</file_data>
<time_inserted>2019-01-30T10:29:20.547</time_inserted>
</row>
<row>
<file_data>three</file_data>
<time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
<file_data>four</file_data>
<time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
<file_data>five</file_data>
<time_inserted>2019-01-30T10:29:20.553</time_inserted>
</row>
</rows>
'''
def response = new XmlSlurper().parseText(dbresponse)
def data = response.rows.row[1].file_data
print data
I have two questions:
1] With the above code why am I not getting the response of: two ? 2] How do I iterate through the entire xml doc to get this response: one two three four five
Thanks
解决方案
Here is how it works:
def rows = new XmlSlurper().parseText(dbresponse)
print (rows.row[1])
print (rows.row[1].file_data)
The identifier, rows
, gives a handle on the object returned when parsing dbresponse
(<rows>
in this case). I named it rows
since this is the convention slurp'ers use; it doesn't have to be.
推荐阅读
- php - Laravel如何将一部分url添加到当前url?
- java - 获取泛型类型或泛型实例的类
- r - 从数据框中的一行向后查找匹配项
- javascript - 如何在猫鼬数据库中添加用户信息?
- xamarin - 如何使用 ViewModel 中的代码添加到我的页面布局?
- python - 神经网络在第一次训练后产生 NaN
- javascript - 在ng表中选择,在ng表中重新加载数据时页面返回顶部
- ios - iOS 中的位置说明符,Swift 4.x
- database - 如何检查 created_at 在两个日期之间是否存在
- python-3.x - 在熊猫系列python 3.x中拆分csv?