Groovy web console

subscribe to the feed Subscribe
to this
update xml (via #groovywebconsole)
tweet this snippet Tweet

update xml

Published 11 months ago by nmrao with tags xml
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xml = """<project name='Common'>
  <service name='name' pattern='something' isReliable='maybe'>
    <receiver name='name' isUsingTwoWaySsl='maybe' isWsRmDisabled='maybe' 
      <endpoint name='local_tst01'>URL1</endpoint>
      <endpoint name='local_tst02'>URL2</endpoint>
      <endpoint name='local_tst03'>URL3</endpoint>
      <environment name='dev' default='local_dev' />
      <environment name='tst01' default='test' />
      <environment name='tst02' default='local_tst02' />
    <operation name='name'>
      <attribute name='operation' type='String'>name</attribute>

//Set your endpoint name attribute value and new endpoint url in a map
//This would be flexible to have the respective url
def endpointBinding = ['local_tst01': '', 'local_tst02': '', 'local_tst03': '']

pXml = new XmlSlurper().parseText(xml)

//Update the values in xml as per the binding defined above
getUpdatedXml = { 
  endpointBinding.collect { k, v -> pXml.'**'.find{ == 'endpoint' && it.@name == k }.replaceBody(v) }

println groovy.xml.XmlUtil.serialize( getUpdatedXml() )