0

I have a groovy system batch script that needs to read in an XML document, change the values, and then save it.

I have figured out how to read the value and write it. I cannot figure out for the life of me how to remove 'members' and its values. I need to be able to remove all the members and replace it with a custom filename I just cannot figure it out.

The XML looks like this:

<types>
    <members>*</members>
    <members>Account</members>
    <members>Activity</members>
    <members>Contact</members>
    <members>Task</members>
    <members>User</members>
    <members>ContentVersion</members>
    <name>CustomObject</name>
</types>

I would search for the name "CustomObject" and remove all the sibling's members with this string:

def replace = "MyCustomFile"

So the XML would like this:

<types>
    <members>MyCustomFile</members>
    <name>CustomObject</name>
</types>

I have tried the below code I found online

println "Testing Slurper"
def root = new XmlSlurper().parse(new File(theFile))
root.types.each { types ->
println "names: ${types.name}"
    types.members.each {
        println "members: " + it.text()
    }
}

    println "Testing replace"
    def book = "Booking__c"


    def cleanUpNode(node) {
        println node
        def childs = node.children()

        def remove = []
        childs.each {
            if (it instanceof Node) {

                if (!it.children()) {
                    remove.add it
                } else {
                    cleanUpNode it
                    if (!it.children()) {
                        remove.add it
                    }
                }
            }
        }

        remove.each { node.remove(it) }
    }

    cleanUpNode root.types.name

It is not actually removing anything instead this was the output:

  <tag0:types>
    <tag0:members>*</tag0:members>
    <tag0:members>Account</tag0:members>
    <tag0:members>Activity</tag0:members>
    <tag0:members>Contact</tag0:members>
    <tag0:members>Task</tag0:members>
    <tag0:members>User</tag0:members>
    <tag0:members>ContentVersion</tag0:members>
    <tag0:name>CustomObject</tag0:name>
  </tag0:types>

I am still trying to get the hang of this so any help would be great

Nicole Phillips
  • 753
  • 1
  • 18
  • 41

1 Answers1

1

You can do something like this:

List newMembersToAdd = ['myCustomFile', 'anotherCustomFile']

Node xml = new XmlParser(false, false, false).parse("myXml.xml")

xml.members?.each {
    xml.remove it
}

newMembersToAdd.each { String newMember ->
    new Node(xml, 'members', newMember)
}

new File("myNewXml.xml").withWriter { writer ->
    def printer = new XmlNodePrinter( new PrintWriter(writer) )
    printer.preserveWhitespace = true
    printer.print( xml )
}
Edumelzer
  • 1,066
  • 1
  • 11
  • 22
  • I do need to add new members after with a value of a file I read in. This also makes a namespace and I do not use a namespace in my XML. The remove method did work however – Nicole Phillips Mar 26 '18 at 13:12
  • @NicolePhillips I edited the answer, I think it will not create namespaces now. But I did not undertand exactly the part about adding new `members` from a file; Will you add a single `members` tag containing all the file content? Or do you need a new `members` tag for each file line? Can you provide some examples? – Edumelzer Mar 26 '18 at 13:34
  • No. in the second XML example in the question I will need to add a new members value there may be more than one. So basically I need to remove old members values with new ones. There may be times when there are no members at all. – Nicole Phillips Mar 26 '18 at 15:45
  • @NicolePhillips Ok I think I got it. I changed my code so you can pass a list/array of new members to add. Does it help you? – Edumelzer Mar 26 '18 at 16:20
  • Getting this error: startup failed: Script1.groovy: 80: You cannot create an instance from the abstract class 'hudson.model.Node'. @ line 80, column 5. new Node(xml, 'members', newMember) ^ – Nicole Phillips Apr 03 '18 at 19:38