如何从XML Request中读取值并使用Groovy写入XML Response? [英] How to read values from XML Request and write into XML Response using Groovy?

查看:185
本文介绍了如何从XML Request中读取值并使用Groovy写入XML Response?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个groovy脚本,在这个脚本中,我必须从请求XML文件中提取值并将其写入XML响应文件。



我知道如何从普通的XML读取数值,如下所示:

  def text ='''
< list>
< technology>
<名称> Groovy< /名称>
< / technology>
< / list>
'''

def list = new XmlParser()。parseText(text)
println list.technology.name.text()

我可以使用上面的语法轻松访问节点。但在我的情况下,请求文件具有'keyword:label'语法。考虑下面提到的货币转换器的请求文件:

 < soap:Envelope xmlns:soap =http://www.w3 .org / 2003/05 / soap-envelopexmlns:web =http://www.webserviceX.NET/> 
< soap:Header />
< soap:Body>
< web:ConversionRate>
< web:FromCurrency> USD< / web:FromCurrency>
< web:ToCurrency> INR< / web:ToCurrency>
< / web:ConversionRate>
< / soap:Body>
< / soap:Envelope>

如何读取 FromCurrency 的值案件?除了使用XMLParser之外,是否还有其他有效的方法可以处理更大的XML文件?

另外,当在响应中写入值时,我通过创建多个变量脚本并使用它们的值作为响应,使用语法$ {var_name}。

如果我想从请求写入很多值(假设为20+)到响应中,个人写作不是好方法,那么有没有什么好的和有效的方法呢?

解决方案

c> FromCurrency 就像这样(使用 XmlSlurper ):

  def text ='''< soap:Envelope xmlns:soap =http://www.w3.org/2003/05/soap-envelopexmlns:web =http://www.webserviceX > 
< soap:Header />
< soap:Body>
< web:ConversionRate>
< web:FromCurrency> USD< / web:FromCurrency>
< web:ToCurrency> INR< / web:ToCurrency>
< / web:ConversionRate> ;
< / soap:Body>
< / soap:Envelope>'''

def list = new XmlSlurper()。parseText(text)
println list.Body.ConversionRate.FromCurrency.text()

不知道我是否理解了其他问题


I am working on a groovy script in which I have to take values from request XML file and write those into XML response file.

I know how to read values from normal XML as given below:

def text = '''
<list>
    <technology>
        <name>Groovy</name>
    </technology>
</list>
'''

def list = new XmlParser().parseText(text) 
println list.technology.name.text()

I can easily access nodes using above syntax. But in my case the request file have 'keyword:label' syntax. Consider below mentioned request file for currency converter:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
  <web:ConversionRate>
     <web:FromCurrency>USD</web:FromCurrency>
     <web:ToCurrency>INR</web:ToCurrency>
  </web:ConversionRate>
</soap:Body>
</soap:Envelope>

How to read value of FromCurrency in this case? Instead of using XMLParser, is there any other efficient and good way to handle larger XML files?

Also, while writing values in the response I am writing by creating multiple variables in script and using their values in response using syntax "${var_name}.

If I want to write many values (suppose 20+) from request into response, then variable for individual writing is not good way. So is there any good and efficient way for this?

解决方案

You can get FromCurrency like so (using XmlSlurper):

def text = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
  <web:ConversionRate>
     <web:FromCurrency>USD</web:FromCurrency>
     <web:ToCurrency>INR</web:ToCurrency>
  </web:ConversionRate>
</soap:Body>
</soap:Envelope>'''

def list = new XmlSlurper().parseText(text) 
println list.Body.ConversionRate.FromCurrency.text()

Not sure I understand the rest of your question

这篇关于如何从XML Request中读取值并使用Groovy写入XML Response?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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