如何在Groovy中构建XML命名空间

时间:2019-01-10 17:00:24

标签: groovy xml-namespaces

我有一个必须在Groovy中构建的SOAP XML。我是ABAP程序员,我不知道该怎么做。谁能帮帮我吗? 代码下方:

<soapenv:Header>
  <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
	             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
	<wsu:Timestamp wsu:Id="TS-' lv_timestamp_id '">
	  <wsu:Created>+lv_created+</wsu:Created>
	  <wsu:Expires>+lv_expires+</wsu:Expires>
	</wsu:Timestamp>
	<wsse:UsernameToken wsu:Id="UsernameToken-">
	  <wsse:Username>+lv_username+</wsse:Username>
	  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">+lv_sb64password+</wsse:Password>
	  <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">+lv_b64nonce+</wsse:Nonce>
	  <wsu:Created>+lv_created+</wsu:Created>
	</wsse:UsernameToken>
   </wsse:Security>
</soapenv:Header>

1 个答案:

答案 0 :(得分:1)

以下是文档:http://groovy-lang.org/processing-xml.html#_markupbuilder

类似的事情应该起作用。

def xml = new MarkupBuilder(writer)
xml.'soapenv:Header'{
  'wsse:Security'('xmlns:wsse': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'xmlns:wsu': "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")     
    'wsu:Timestamp'('wsu:Id':"TS-' lv_timestamp_id '"){
      'wsu:Created'(lv_created)
      'wsu:Expires'(lv_expires)
    }
  }
}