我正在使用SoapUI,我有一个Soap请求,它以Json String格式输入,在处理之后,我得到一个XML格式的Soap响应,其中包含CDATA中的JSON格式。
我正在尝试使用以下代码从Soap Response中提取数据但我收到错误“prolog中不允许内容”
import com.eviware.soapui.support.XmlHolder
import net.sf.json.groovy.JsonSlurper
import groovy.xml.Namespace
respXmlHolder = new XmlHolder(messageExchange.getResponseContentAsXml())
respXmlHolder.declareNamespace("ns1","http://tempuri.org/")
CDATAXml = respXmlHolder.getNodeValue("//ns1:ReportResult")
log.info(CDATAXml)
def data = new XmlParser().parseText(CDATAXml).Rpt
log info "data.findAll{it.'Name'}.size()"
Soap Response看起来如下所示:
<ReportResponse xmlns="http://tempuri.org/">
<ReportResult><![CDATA[{
"Report": [
{
"Name": "ABC",
"Number": "123",
"Type": "XYZ",
"Desc": "EFGH",
"Group Name": "Name123",
"Group Number": "123",
"End Date": "12/30/2014",
"Due Date": "04/15/2015",
"Completion Date": null,
"Status": "Status1",
"Received Date": "",
"Delivery Date": "",
"Location": "",
"Role": "",
},
{
"Name": "EFG",
"Number": "123",
"Type": "XYZ",
"Desc": "EFGH",
"Group Name": "Name123",
"Group Number": "123",
"End Date": "12/30/2014",
"Due Date": "04/15/2015",
"Completion Date": null,
"Status": "Status1",
"Received Date": "",
"Delivery Date": "",
"Location": "",
"Role": "Manager",
}
]
} ]]></ReportResult>
</ReportResponse>
</s:Body>
</s:Envelope>
请帮助我。
答案 0 :(得分:1)
正如@Rao在评论中注明的那样,您使用//ns1:Result
作为 XPath 但是此节点并不存在于您的回复中。无论如何作为替代而不是使用和XmlHolder
,我打算使用第一个XmlSlurper
来解析SOAP并获取CDATA
节点,然后使用JSonSlurper
来解析包含在CDATA
中并获得所需的值(在您的情况下似乎您正在寻找Name
值属性),请参阅以下示例:
import groovy.json.JsonSlurper
def soapResponse = '''<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ReportResponse xmlns="http://tempuri.org/">
<ReportResult><![CDATA[{
"Report": [
{
"Name": "ABC",
"Number": "123",
"Type": "XYZ",
"Desc": "EFGH",
"Group Name": "Name123",
"Group Number": "123",
"End Date": "12/30/2014",
"Due Date": "04/15/2015",
"Completion Date": null,
"Status": "Status1",
"Received Date": "",
"Delivery Date": "",
"Location": "",
"Role": "",
},
{
"Name": "EFG",
"Number": "123",
"Type": "XYZ",
"Desc": "EFGH",
"Group Name": "Name123",
"Group Number": "123",
"End Date": "12/30/2014",
"Due Date": "04/15/2015",
"Completion Date": null,
"Status": "Status1",
"Received Date": "",
"Delivery Date": "",
"Location": "",
"Role": "Manager",
}
]
} ]]></ReportResult>
</ReportResponse>
</s:Body>
</s:Envelope>'''
// parse the soap
def xml = new XmlSlurper().parseText(soapResponse)
// get cdata node
def reportCDATA = xml.'**'.findAll{ it.name() == 'ReportResult' }
def jsonTxt = reportCDATA.pop().toString()
// parse CDATA value as JSON
def json = new JsonSlurper().parseText(jsonTxt)
// get the Name attribute values
def names = json.Report.collect { it.Name }
names.each { log.info it }
如果您评论时,您希望得到"Name"
,而不是 JSON 的"Completion Date"
值,则可以将其作为:{/ p>
def completionDates = json.Report.collect { it."Completion Date"}
或者看起来更清楚:
def completionDates = json.Report.collect { it[Completion Date] }
请注意,在您的示例中,Report
数组包含两个对象,这两个对象都null
作为"Completion Date"
的值。因此,使用 JSON 示例def completionDates
将为[null, null]
。
希望它有所帮助,