0

I have a follow-up question to this post appendNode using xmlSlurper in a specific position . Is it possible to add the Salutation tag with a namespace, for example, achieve the below output

<?xml version="1.0" encoding="UTF-8"?>
<prnReq>
  <ltrPrnReqs>
    <ltrPrnReq>
      <ltrData>
        <ns1:Salutation xmlns:ns1 = "http://namespace">text</Salutation>
      </ltrData>
    </ltrPrnReq>
  </ltrPrnReqs>
</prnReq>

The code given there is

def xmlString = """<prnReq>
    <ltrPrnReqs>
        <ltrPrnReq>
            <ltrData>encoded64 text</ltrData>
        </ltrPrnReq>
    </ltrPrnReqs>
</prnReq>"""
def xml = new XmlSlurper().parseText(xmlString)
def ltrData = xml.'**'.find{it.name() == 'ltrData'}
ltrData.replaceBody()
ltrData.appendNode {
  Salutation('text')
}
println groovy.xml.XmlUtil.serialize(xml)

The above code produces the below output, but I would like to add the namespace as well

<?xml version="1.0" encoding="UTF-8"?><prnReq>
  <ltrPrnReqs>
    <ltrPrnReq>
      <ltrData>
        <Salutation>text</Salutation>
      </ltrData>
    </ltrPrnReq>
  </ltrPrnReqs>
</prnReq>
Vy Do
  • 46,709
  • 59
  • 215
  • 313
lm1234
  • 1

1 Answers1

0

Got it.

ltrData.replaceBody()
ltrData.appendNode {
  'ns2:Salutation'('text',['xmlns:ns2':'http://namespace'])
}
lm1234
  • 1