在grails中解析xml字符串响应 [英] parse xml string response in grails

查看:145
本文介绍了在grails中解析xml字符串响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

响应xml文本是:

response xml text is :

"<?xml version="1.0" encoding="UTF-8"?><MMP><MERCHANT><RESPONSE><url>http://XXXXXXx/asdasd/asdsd/as</url><param name="ttype">QEQW</param><param name="tempTxnId">155411</param><param name="token">v22mM1NmwpHCKLp%2FZJC%2B7PUKASXr2aS01JPi2ZXDqT0%3D</param><param name="txnStage">1</param></RESPONSE></MERCHANT></MMP>"

我想在grails / groovy中解析这个xml文本。如何迭代所有元素来获取特定元素的数据?

I want to parse this xml text in grails/groovy. How do I iterate through all the elements to fetch the data of a particular element?

推荐答案

您可以使用 XMLSlurper 阅读xml。

You can use the XMLSlurper to read the xml.

以下是您发布的xml的一些示例代码: b
$ b

Here is some example code for your posted xml:

def response = new XmlSlurper().parseText(xml)

// accessing a specific element
println response.MERCHANT.RESPONSE.url
// iterating through a collection
response.MERCHANT.RESPONSE.param.each{println it}
// searching for specific elements
println response.MERCHANT.RESPONSE.param.find{it.@name == 'ttype'}

这篇关于在grails中解析xml字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆