如何使用JavaSAAJ在Soap请求XML中添加子元素

如何使用JavaSAAJ在Soap请求XML中添加子元素,java,soap,groovy,soapui,saaj,Java,Soap,Groovy,Soapui,Saaj,我想在Soap请求主体中添加子元素(标识符),如下所示: 预期的Soap请求 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="nsurl"> <soapenv:Header/> <soapenv:Body> <ns:GetRequest> <ns:Identifier Type="x" Value="y"

我想在Soap请求主体中添加子元素(标识符),如下所示:

预期的Soap请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="nsurl">
<soapenv:Header/>
<soapenv:Body>
  <ns:GetRequest>
     <ns:Identifier Type="x" Value="y"/>      
  </ns:GetRequest>
</soapenv:Body>
</soapenv:Envelope>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:ns="nsurl">
 <soapenv:Header/>
 <soapenv:Body>
  <ns:GetRequest>
     <ns:Identifier>Type="x" Value="y"</ns:Identifier>      
  </ns:GetRequest>
 </soapenv:Body>
 </soapenv:Envelope>

看起来您正在使用
SoapUI
工具

您可以使用
Groovy脚本
teststep和下面的脚本来更改相同的脚本

def xmlString = """ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="nsurl">  <soapenv:Header/>  <soapenv:Body>   <ns:GetRequest>      <ns:Identifier>Type="x" Value="y"</ns:Identifier>         </ns:GetRequest>  </soapenv:Body>  </soapenv:Envelope>"""
def xml = new XmlSlurper().parseText(xmlString)
//Get the Identifier node
def identifier = xml.'**'.find{it.name() == 'Identifier'}

//Create a map based on Identifier node value
def map = identifier.text().split(' ').collectEntries{ [(it.split('=')[0]) : it.split('=')[1].replace('"','')]}

//Remove the text value for Identifier node
identifier.replaceBody { '' }

//Set the attributes from the map
map.each{k,v -> identifier.@"$k" = v}
def newXml = groovy.xml.XmlUtil.serialize(xml)
log.info newXml
def xmlString=“”Type=“x”Value=“y”
def xml=new XmlSlurper().parseText(xmlString)
//获取标识符节点
def identifier=xml.'**'。查找{it.name()=='identifier'}
//基于标识符节点值创建映射
def map=identifier.text().split(“”).collectEntries{[(it.split('=')[0]):it.split('=')[1]。replace('“,'')]
//删除标识符节点的文本值
identifier.replaceBody{'}
//从贴图中设置属性
map.each{k,v->identifier.@“$k”=v}
def newXml=groovy.xml.XmlUtil.serialize(xml)
log.info newXml

您可以在预期的请求中快速在线尝试它,
类型和
值是属性,而不是
ns:Identifier
元素内容的一部分。因此,您需要使用SAAJ的
addAttribute
(或DOM的
setAttributes
)方法添加它们。

此答案可能与主题无关。我猜OP将问题标记为“soapui”,因为预期的请求是由soapui生成的。
def xmlString = """ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="nsurl">  <soapenv:Header/>  <soapenv:Body>   <ns:GetRequest>      <ns:Identifier>Type="x" Value="y"</ns:Identifier>         </ns:GetRequest>  </soapenv:Body>  </soapenv:Envelope>"""
def xml = new XmlSlurper().parseText(xmlString)
//Get the Identifier node
def identifier = xml.'**'.find{it.name() == 'Identifier'}

//Create a map based on Identifier node value
def map = identifier.text().split(' ').collectEntries{ [(it.split('=')[0]) : it.split('=')[1].replace('"','')]}

//Remove the text value for Identifier node
identifier.replaceBody { '' }

//Set the attributes from the map
map.each{k,v -> identifier.@"$k" = v}
def newXml = groovy.xml.XmlUtil.serialize(xml)
log.info newXml