2017-08-01 82 views
1

我嘗試使用groovy(SoapUI)創建以下XML。如何使用groovy將xsi名稱空間前綴添加到xml

預期的XML

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:save xmlns:ns2="http://service.generic.abc.it"> 
     <arg1 xsi:type="ns4:ContractGroup" xmlns:ns4="http://generic.abc.it" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <discounts xsi:type="ns4:DiscountPrice"></discounts> 
     </arg1> 
     </ns2:save> 
    </S:Body> 
</S:Envelope> 

使用的代碼:(沒有工作)

def grUtils = new com.eviware.soapui.support.GroovyUtils(context) 

// create XmlHolder for request 
def requestHolder = grUtils.getXmlHolder("Contract_save#Request") 

// find the Node that I am interested in 
def requestNodeMain = requestHolder.getDomNode("//*:Body/*:save/*:arg1[1]") 

// the Document object is used to create new nodes 
def requestDocMain = requestNodeMain.getOwnerDocument() 

def WideSearchRQElement = requestDocMain.createElementNS("ns4:DiscountPrice","type:discounts") 
requestNodeMain.appendChild(WideSearchRQElement) 

requestHolder.updateProperty(true) 
+0

您是否嘗試過給定的解決方案,看看是否有幫助? – Rao

回答

0

在這裏你去,評論在線。

import groovy.xml.StreamingMarkupBuilder 
import groovy.xml.XmlUtil 
def builder = new StreamingMarkupBuilder() 
builder.encoding = 'UTF-8' 
def xml = builder.bind { 
    mkp.xmlDeclaration() 
//Declare the namespaces 
    namespaces << [ S:'http://schemas.xmlsoap.org/soap/envelope/', 
    xsi: 'http://www.w3.org/2001/XMLSchema-instance', 
    ns2: 'http://service.generic.abc.it', 
    ns4: 'http://generic.abc.it'] 
//You can see building the xml as you wanted it 
    S.Envelope { 
     S.Body { 
      ns2.save { 
       arg1('xsi.type': 'ns4:ContractGroup'){ 
        discounts('xsi.type': 'ns4:DiscountPrice') 
       } 
      }    
     }   
    } 
} 

println XmlUtil.serialize(xml) 

您可以快速地在線試用Demo

相關問題