如何在soapui中提取数据库xml数据
how to extract database xml data in soapui
我的数据以 xml 格式保存在 SQL Server 2014 中,在 SOAPUI JDBC 中请求它以这种格式出现
<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<ConfigurationXML><![CDATA[<ROOT><Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</Device></ROOT>]]></ConfigurationXML>
</Row>
</ResultSet>
</Results>
我想将此数据与我的 REST Json 响应进行比较,但是如何从该 CDATA 中获取数据
我尝试将 XML 转换为字符串然后替换字符,最后将替换的字符表达式转换回 XML
declare @xml xml = '
<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<CONFIGURATIONXML><ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT></CONFIGURATIONXML>
</Row>
</ResultSet>
</Results>
'
select replace(replace( CAST(@xml as nvarchar(max)),'<','<'),'>','>')
-- CAST( replace(replace( CAST(@xml as nvarchar(max)),'<','<'),'>','>') as xml)
但是因为DEVICE标签失败了
它应该在
正确读取值将隐式解码字符。
但是:你的内在XML无效!:
DECLARE @xml XML=
'<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<CONFIGURATIONXML><ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT></CONFIGURATIONXML>
</Row>
</ResultSet>
</Results>';
SELECT @xml
SELECT @xml.value('(/Results/ResultSet/Row/CONFIGURATIONXML)[1]','nvarchar(max)')
导致:
<ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT>
如您所见,</DEVICE>
是一个结束标签,但是没有开始标签...
不知道这是怎么创建的...如果它总是一样的,你可以这样修复它:
SELECT CAST(REPLACE(@xml.value('(/Results/ResultSet/Row/CONFIGURATIONXML)[1]','nvarchar(max)'),'</Device>','') AS XML).value('/ROOT[1]','varchar(max)')
结果
[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]
jdbc 提供的示例响应有点复杂。需要根据数据的性质分多个阶段重试。
- 响应 xml 有
CDATA
- CDATA 又有
Xml
- 那个 xml 有
json
请找到下面的 groovy 脚本重试最终 json 并检索其属性,如下所示。
脚本在行中添加了适当的注释。
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//Sample response
def response = '''<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<ConfigurationXML><![CDATA[<ROOT><Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</Device></ROOT>]]></ConfigurationXML>
</Row>
</ResultSet>
</Results>'''
//Create the xml holder object for the jdbc response
def holder = new XmlHolder(response)
//Get ConfigurationXML Node value using Xpath from holder object, which will retrive CDATA
def configurationXmlCdata = holder.getNodeValue('//*:ConfigurationXML')
log.info "Configruation Xml Cdata: ${configurationXmlCdata}"
//There is again xml inside CDATA, so create xml holder object to retried json string
def cdataHolder = new XmlHolder(configurationXmlCdata)
def jsonString = cdataHolder.getNodeValue('//ROOT/Device')
log.info "Device Json data: ${jsonString}"
//Create Json slurper object if you need to access json
def jsonData = new JsonSlurper().parseText(jsonString)
log.info jsonData.Name
log.info jsonData.Index
log.info jsonData.Profile.ID
我的数据以 xml 格式保存在 SQL Server 2014 中,在 SOAPUI JDBC 中请求它以这种格式出现
<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<ConfigurationXML><![CDATA[<ROOT><Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</Device></ROOT>]]></ConfigurationXML>
</Row>
</ResultSet>
</Results>
我想将此数据与我的 REST Json 响应进行比较,但是如何从该 CDATA 中获取数据
我尝试将 XML 转换为字符串然后替换字符,最后将替换的字符表达式转换回 XML
declare @xml xml = '
<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<CONFIGURATIONXML><ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT></CONFIGURATIONXML>
</Row>
</ResultSet>
</Results>
'
select replace(replace( CAST(@xml as nvarchar(max)),'<','<'),'>','>')
-- CAST( replace(replace( CAST(@xml as nvarchar(max)),'<','<'),'>','>') as xml)
但是因为DEVICE标签失败了 它应该在
正确读取值将隐式解码字符。
但是:你的内在XML无效!:
DECLARE @xml XML=
'<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<CONFIGURATIONXML><ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT></CONFIGURATIONXML>
</Row>
</ResultSet>
</Results>';
SELECT @xml
SELECT @xml.value('(/Results/ResultSet/Row/CONFIGURATIONXML)[1]','nvarchar(max)')
导致:
<ROOT></Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</ROOT>
如您所见,</DEVICE>
是一个结束标签,但是没有开始标签...
不知道这是怎么创建的...如果它总是一样的,你可以这样修复它:
SELECT CAST(REPLACE(@xml.value('(/Results/ResultSet/Row/CONFIGURATIONXML)[1]','nvarchar(max)'),'</Device>','') AS XML).value('/ROOT[1]','varchar(max)')
结果
[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]
jdbc 提供的示例响应有点复杂。需要根据数据的性质分多个阶段重试。
- 响应 xml 有
CDATA
- CDATA 又有
Xml
- 那个 xml 有
json
请找到下面的 groovy 脚本重试最终 json 并检索其属性,如下所示。
脚本在行中添加了适当的注释。
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//Sample response
def response = '''<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<ConfigurationXML><![CDATA[<ROOT><Device>[{"Name":"AL","Profile":{"ID":1,"Height":240},"Index":-1}]</Device></ROOT>]]></ConfigurationXML>
</Row>
</ResultSet>
</Results>'''
//Create the xml holder object for the jdbc response
def holder = new XmlHolder(response)
//Get ConfigurationXML Node value using Xpath from holder object, which will retrive CDATA
def configurationXmlCdata = holder.getNodeValue('//*:ConfigurationXML')
log.info "Configruation Xml Cdata: ${configurationXmlCdata}"
//There is again xml inside CDATA, so create xml holder object to retried json string
def cdataHolder = new XmlHolder(configurationXmlCdata)
def jsonString = cdataHolder.getNodeValue('//ROOT/Device')
log.info "Device Json data: ${jsonString}"
//Create Json slurper object if you need to access json
def jsonData = new JsonSlurper().parseText(jsonString)
log.info jsonData.Name
log.info jsonData.Index
log.info jsonData.Profile.ID