read and update xml using groovy ,

read xml file in groovy to read xml file , using  XmlSlurper().parseText and display its content ;xml file: <?xml version='1.0' encoding='UTF-8'?>

<Records>

  <Line>

  <Field1>ABC</Field1>

  <Field2>123</Field2>

  <Field3>XXX</Field3>

  <Field4>567890</Field4>

  </Line>

</Records>; 

ChatGPT

You can use the XmlSlurper class in Groovy to parse an XML file and display its content. Here's a Groovy script that reads the XML content you provided and displays it:


groovy

Copy code

def xmlText = """<?xml version='1.0' encoding='UTF-8'?>

<Records>

  <Line>

    <Field1>ABC</Field1>

    <Field2>123</Field2>

    <Field3>XXX</Field3>

    <Field4>567890</Field4>

  </Line>

</Records>"""


def records = new XmlSlurper().parseText(xmlText)


// Display XML content

println "Root Element Name: ${records.name()}"

records.Line.each { line ->

    println "Line Element:"

    println "  Field1: ${line.Field1.text()}"

    println "  Field2: ${line.Field2.text()}"

    println "  Field3: ${line.Field3.text()}"

    println "  Field4: ${line.Field4.text()}"

}

------------------------

 





-----------------




def xmlText = """<?xml version='1.0' encoding='UTF-8'?>
<Records>
  <Line>
    <Field1>ABC</Field1>
    <Field2>123</Field2>
    <Field3>1111</Field3>
    <Field4>567890</Field4>
  </Line>
</Records>"""

def xml = new XmlSlurper().parseText(xmlText)
def xpath = "/Records/Line/Field4"

// Update the element with XPath
xml.'**'.findAll { it.name() == 'Field4' }.each { element ->
    element.replaceBody("Rama")
}

// Serialize the modified XML back to text
def updatedXmlText = groovy.xml.XmlUtil.serialize(xml)

println updatedXmlText
---------------
https://blogs.sap.com/2016/01/26/hci-parse-xml-easily-with-groovy-scripts/
take reference 








Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024