From http://www.robert-nemet.com/2011/11/groovy-xml-parsing-in-soapui.html

Introduction

Since soapUI allows users to add Groovy scripts in large number of
places ( property expansions, setup /teardown scripts, listeners, test
step, etc... ) users can use any technology to reach to the goal. Lets
look at those which can make our life easier when it comes to XML
parsing.

soapUI XML Support

Old way would be soapUI way or using GroovyUtils from soapUI. Its goal is to simplify scripting and it can be instantiate from any Groovy script in soapUI, with:

where context is available in any Groovy script in soapUI. Lets look at GroovyUtils:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
GroovyUtils Methods
expand(property)  
returns value of property, if there are property expansions they are expanded. 
extractErrorLineNumber(error) 
returns line number where error occurred
getProjectPath() 
 
returns path to project file           
getXml(node)
returns xml as a string from passed node
getXmlHolder(xmlPropertyOrString) 
returns XmlHolder as a result of parsing passed Xml string           
registerJdbcDriver(driver) 
register JDBC driver for use in Groovy script
setPropertyValue(testStep, property, value) 
sets a value for given test step property

This time getXmlHolder is one that does what we need. Pars a given string as XML and returns object which we can use to manipulate with it.

XmlHolder Methods
declareNamespace(prefix, uri)  declare namespace prefix and URI
getDomNode(xpath)            
returns a DOM node pointed by XPath expression 
getDomNodes(xpath)  returns an array of DOM nodes pointed by XPath expression 
getNamespaces()  returns a Map of namespaces 
getNodeValue(xpath)  returns a value of node specified by XPath expression
getNodeValues(xpath)  returns an array of values specified by XPath expression
getPrettyXml()            
returns a formatted XML 
getXml()      
returns a XML 
getXmlObject() returns a XmlBean Object 
removeDomNodes(xpath) removes a nodes specified by XPath expression 
setNodeValue(xpath, value)  set a node value  
updateProperty()  if a XmlHolder is created from test step property this updates that property 
updateProperty(prettyPrint) 

 

if a XmlHolder is created from test step property this updates that property with well formatted XML 

Few Examples

Assume you have response:

For this examples I added a Properties Test Step  with property response.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sim="http://www.example.org/SimpleSocialSOA/">
<soapenv:Header/>
<soapenv:Body>
<sim:LoginResponse>
<id>4456643453</id>
</sim:LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

1. Parsing XML


According to that to get id from response you can do:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
log.info holder.getNodeValue("//id")
log.info holder['//id']

notice two ways to get node value with getNodeValue method and with [] operator. Difference is that getNodeValue will return first value that can be found with set XPath, while [] operator returns a array of values.
If you assume that response is like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sim="http://www.example.org/SimpleSocialSOA/">
<soapenv:Header/>
<soapenv:Body>
<sim:LoginResponse>
<id>4456643453</id>
<id>4456643452</id>
<id>4456643451</id>
<id>4456643450</id>
</sim:LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

Than this script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
log.info holder.getNodeValue("//id")
for( node in holder['//id'] )
log.info node

will produce result like this:

2. Updating XML


Response as above and we do:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
holder.setNodeValue("//id", "aaaaa")
for( node in holder['//id'] )
log.info node
def holder2 = groovyUtils.getXmlHolder("Properties#response")
holder2.getNodeValue("//id")
for( node in holder2['//id'] )
log.info node

Result is:

from this we can learn:

  • if there are several nodes as result of XPath expression only first's node value will be changed with setNodeValue
  • setting node value is not enough to update property value, we forgot to use updateProperty method
Correct Groovy script would be:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
holder.setNodeValue("//id", "aaaaa")
for( node in holder['//id'] )
log.info node
holder.updateProperty()
def holder2 = groovyUtils.getXmlHolder("Properties#response")
holder2.getNodeValue("//id")
for( node in holder2['//id'] )
log.info node

Instead of using:

holder.setNodeValue("//id", "aaaaa")

you can use also:

holder["//id"] = "aaaaa"

And if XmlNode is created from property as in example above use updateProperty method to apply your changes to property or use GroovyUtils.setPropertyValue method.

Groovy XML

There are three classes built in Groovy for processing XML: XMLParser, XMLSlurper and DOMCategory. Best practice is to use XMLSlurper in
soapUI. Reason is that it have smallest memory footprint from all
three. I would no go in depths for those, you can read here XML processing.
This for example:

import groovy.xml.StreamingMarkupBuilder
def response = context.testCase.testSteps['Properties'].properties['response'].value
def responseXml = new XmlSlurper().parseText(response)
responseXml.breadthFirst().each {
def v = it.toString()
if ( it.name() == 'id')
it.replaceBody("$v XX")
}

appends " XX" to values of all id nodes.

Generally it is easier to use GroovyUtils but if you are familiar or invest some time in XmlSlurper you can get better results and more flexible scripts.

SoapUI中XML解析的更多相关文章

  1. 2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

    一: iOS中xml解析的几种方式简介 1.官方原生 NSXMLParser :SAX方式解析,使用起来比较简单 2.第三方框架 libxml2 :纯C 同时支持DOM与SAX GDataXML: D ...

  2. Android中XML解析-Dom解析

    Android中需要解析服务器端传过来的数据,由于XML是与平台无关的特性,被广泛运用于数据通信中,有的时候需要解析xml数据,格式有三种方式,分别是DOM.SAX以及PULL三种方式,本文就简单以D ...

  3. Android中XML解析-SAX解析

    昨天由于时间比较匆忙只写了Android中的XML解析的Dom方式,这种方式比较方便,很容易理解,最大的不足就是内容多的时候,会消耗内存.SAX(Simple API for XML)是一个解析速度快 ...

  4. Android中XML解析-PULL解析

    前面写了两篇XML解析的Dom和SAX方式,Dom比较符合思维方式,SAX事件驱动注重效率,除了这两种方式以外也可以使用Android内置的Pull解析器解析XML文件. Pull解析器的运行方式与 ...

  5. Android中XML解析,保存的三种方法

    简单介绍 在Android开发中,关于XML解析有三种方式,各自是: SAX 基于事件的解析器.解析速度快.占用内存少.非常适合在Android移动设备中使用. DOM 在内存中以树形结构存放,因此检 ...

  6. Android中XML解析

    package com.example.thebroadproject; public class Book { private int id; private String name; privat ...

  7. xml解析----java中4中xml解析方法(转载)

    转载:https://www.cnblogs.com/longqingyang/p/5577937.html 描述 XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与 ...

  8. cocos2d-x 中XML解析与数据存储

    一不小心就玩了一周的游戏了.哎.玩的时候时间过得总是这么快... 于是今天决定看一下之前不怎么非常熟悉的XML;(之前做游戏时数据的储存用到过XML,但这块是还有一个同事在做,所以不怎么熟悉), 看了 ...

  9. C# 将XML格式字符串,写入数据集的表中 XML解析

    将XML格式字符串,写入数据集的表1中   命名空间:using System.Xml;               string strRead;//strRead为以下xml值           ...

随机推荐

  1. 漫谈格兰杰因果关系(Granger Causality)——第一章 野火烧不尽,春风吹又生

    2017年7月9日上午6点10分,先师胡三清同志--新因果关系的提出者.植入式脑部电极癫痫治疗法的提出者.IEEE高级会员,因肺癌医治无效于杭州肿瘤医院去世,享年50岁.余蒙先师厚恩数载,一朝忽闻先师 ...

  2. Go语言学习笔记(四)结构体struct & 接口Interface & 反射

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...

  3. 对JavaScript闭包的理解

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 在开始了解闭包前我们必须要先理解JavaScript的变量作用域. 一.变量的作用域无非就是两 ...

  4. Linux系统7个运行级别(runlevel)(转)

    原文地址:http://www.cnblogs.com/dkblog/archive/2011/08/30/2160191.html Linux系统有7个运行级别(runlevel) 运行级别0:系统 ...

  5. QT QT creator QTsdk的区别

    Qt是一个跨平台的C++图形用户界面应用程序框架.它提供给应用程序开发者建立艺术级的图形用户界面所需的所用功能.Qt是完全面向对象的,很容易扩展,并且允许真正地组件编程. QT Creator 跨平台 ...

  6. tomato dualwan /root目录的特殊用途

    测试发现tomato dualwan /root目录下存储的文件重启后会自动清掉.利用这个特性可以把测试生成的临时文件丢到这里. root下本应该存在的.vimrc 文件 采用如下方法生成: 在/op ...

  7. 蓝桥杯比赛javaB组练习《牌型种数》

    牌型种数 小明被劫持到X赌城,被迫与其他3人玩牌.一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张.这时,小明脑子里突然冒出一个问题:如果不考虑花色,只考虑点数,也不考虑自己得到的牌 ...

  8. jsp页面中某个src,如某个iframe的src,应该填写什么?可以是html、jsp、servlet、action吗?是如何加载的?

    jsp页面中某个src,如某个iframe的src,应该填写什么?可以是html.jsp.servlet.action吗?是如何加载的? 如有个test工程,其某个jsp中有个iframe,代码如下: ...

  9. 用java调用.net的wcf其实还是很简单的

      前些天和我们的一个邮件服务商对接,双方需要进行一些通讯,对方是java团队,而作为.net团队的我们,只能公布出去的是一个wcf的basicbinding,想不 到问题来了,对方不知道怎么去调用这 ...

  10. 微信小程序(有始有终,全部代码)开发--- 新增【录音】以及UI改进

    开篇语 寒假发了一篇练手文章,不出意外地火了: <简年15: 微信小程序(有始有终,全部代码)开发---跑步App+音乐播放器 > 后来又发了BUG修复的版本,出乎意料的火了: 简年18: ...