[英]Using groovy in soapui to set the text for an xml element that is currently set to xsi:nil
我正在編寫一個groovy腳本,當前正在使用groovyUtils更新請求消息中的xml元素文本。 我在更新具有xsi:nil屬性集的元素的文本時遇到麻煩。 我想設置文本並擺脫xsi:nil屬性。 以下腳本:
def text = '''
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<technology>
<name i:nil="true" />
</technology>
</list>
'''
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder(text)
holder["//name"] = "newtext"
log.info holder.xml
返回值:
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<technology>
<name i:nil="true">newtext</name>
</technology>
</list>
我應該使用什么腳本來擺脫i:nil屬性。 我希望輸出為:
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<technology>
<name>newtext</name>
</technology>
</list>
只需使用removeAttribute(String nodeName)
方法,如下所示:
def text = '''
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<technology>
<name i:nil="true" />
</technology>
</list>
'''
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder(text)
holder["//name"] = "newtext"
def node = holder.getDomNode('//name')
node.removeAttribute('i:nil')
log.info holder.xml
或者,您也可以使用removeAttributeNS(String namespaceUri,String localName)
:
def node = holder.getDomNode('//name')
node.removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance','nil')
此代碼輸出:
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<technology>
<name>newtext</name>
</technology>
</list>
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.