Search code examples
groovyxmlslurper

appendNode using xmlSlurper in a specific position


I'm gonna include the xml structure below:

@Rao, @tim_yates. The actual xml is:

<prnReq>
    <ltrPrnReqs>
        <ltrPrnReq>
            <ltrData>encoded64 text</ltrData>
        </ltrPrnReq>
    </ltrPrnReqs>
</prnReq>

I need to include a new Node in . The new XML must be:

<prnReq>
  <ltrPrnReqs>
    <ltrPrnReq>
      <ltrData>
        <Salutation>text</Salutation>
      </ltrData>
    </ltrPrnReq>
  </ltrPrnReqs>
</prnReq>

The question is how to append a new node in ?

I've found many samples how to use appendNode, however, it is always a root.child. I need to go further in my XML structure and append a node at

prnReq.ltrPrnReqs.ltrPrnReq.ltrData

the node to be included is <salutation>

Any comments are welcome.

Below the current code. Many thanks!

import groovy.xml.QName
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil

File doc = new File("C:/Temp/letter_.xml")

def prnReq = new XmlSlurper().parse(doc)
prnReq.ltrPrnReqs.ltrPrnReq.each {    

    def encodedString = it.ltrData.toString()

    Base64.Decoder decoder = Base64.getMimeDecoder()
    byte[] decodedByteArray = decoder.decode(encodedString)

    def output = new String(decodedByteArray)

    println output   

    output.splitEachLine(';') { items ->
        println "raSalutation: " + items[0] 
        println "raFromAcc: " + items[1] 
        println "raPayableTo: " + items[2]         
        println "raSortCode: " + items[3] 
        println "raAccNum: " + items[4] 
        println "raReference: " + items[5] 
        println "raSendDate: " + items[6] 
        println "raRecDate: " + items[7] 
        println "raAmount: " + items[8] 
        println "raDummy1: " + items[9]         
        println "raFirstAmt: " + items[10]       
        println "raFirstDate: " + items[11]       
        println "raRegularAmt: " + items[12]       
        println "raRegularDate: " + items[13]       
        println "raFrequency: " + items[14]       
        println "raFee: " + items[15]

        def toAdd = '"<salutation>$item[0]</salutation>"'
        fragToAdd = new XmlSlurper().parseText(toAdd)
        prnReq.ltrPrnReqs.ltrPrnReq.ltrData.appendNode(fragToAdd)

    }

    String outputFileName = "C:/Temp/letter_.xml"

    XmlUtil xmlUtil = new XmlUtil()   
    xmlUtil.serialize(prnReq, new FileWriter(new File(outputFileName)))   

}

Solution

  • You should be able to add the new node using appendNode.

    Here is complete sample showing how to do it.

    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)
    

    You can quickly try it online demo