Steven Diaz
Steven Diaz

Reputation: 139

How to pass by value XML Groovy nodes to a variable

I'm trying to duplicate te result of this instruction:

 def xmlEntrada = new File("input.txt").text
    def entrada = new XmlParser().parseText(xmlEntrada)

        def usuarios = entrada.cust_PS_SF_compensation.findAll{ e-> e.cust_userId.toString() == codigoActual }
        def usuariosWithEndDate = entrada.cust_PS_SF_compensation.findAll{ e-> e.cust_userId.toString() == codigoActual }

but all the nodes of the variable usuariosWithEndDate are with the same reference and i need a copy by value of this operation result for edit in parallel.

usuariosWithEndDate is an arrayList with a different reference repect to usuarios but the content (the nodes) have the same reference, help pleaseenter image description here

enter image description here

Upvotes: 1

Views: 358

Answers (1)

Michael Easter
Michael Easter

Reputation: 24468

From what I can tell, we want clone() here.

Given this XML:

<entrada>
<cust_PS_SF_compensation>
    <cust_userId>5150</cust_userId>
</cust_PS_SF_compensation>
<cust_PS_SF_compensation>
    <cust_userId>6160</cust_userId>
</cust_PS_SF_compensation>
<cust_PS_SF_compensation>
    <cust_userId>7170</cust_userId>
</cust_PS_SF_compensation>
</entrada>

Here is the Groovy code with assert statements acting as a form of specification (if I understand the question):

def xmlEntrada = new File("input.xml").text
def entrada = new XmlParser().parseText(xmlEntrada)
def codigoActual = "5150"

def usuarios = entrada.cust_PS_SF_compensation.findAll{ e ->
    e.cust_userId.text() == codigoActual 
}
assert 1 == usuarios.size()

def usuariosWithEndDate = entrada.cust_PS_SF_compensation.findAll{ e -> 
    e.cust_userId.text() == codigoActual 
}.collect { node ->
    node.clone()
}

assert 1 == usuariosWithEndDate.size()

assert ! usuarios[0].is(usuariosWithEndDate[0])
assert codigoActual == usuarios[0].cust_userId.text()
assert codigoActual == usuariosWithEndDate[0].cust_userId.text()

Upvotes: 1

Related Questions