0

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)))   

}
Rao
  • 20,781
  • 11
  • 57
  • 77
Raul Biondo
  • 17
  • 1
  • 8
  • Can you show your xml? – Rao Oct 21 '17 at 00:50
  • Hi @Rao the actual xml is: encoded64 text *************************************************** I need to include a new Node in . The new XML must be: Mrs XXX – Raul Biondo Oct 22 '17 at 06:18
  • Hi @Rao the actual xml is: `` `` `` `encoded64 text` *************************************************** I need to include a new Node in . The new XML must be: `` `` `` `` `Mrs XXX` `` – Raul Biondo Oct 22 '17 at 06:24
  • Raul, please check the answer to see if that helps. – Rao Oct 23 '17 at 02:34

1 Answers1

3

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

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Hi @Rao, It is working now! I've some issue in the loop so only the first customer has been updated. I'll do some additional tests now. MANY THANKS!!! – Raul Biondo Oct 23 '17 at 10:02
  • hi @Rao I have an additional question on this one. In your early convenience could you please if you have any idea there? Link: https://stackoverflow.com/questions/46936769/groovy-xmlparser-addnode-inserting-a-array-of-elements . Many thanks – Raul Biondo Oct 26 '17 at 15:28