I am trying to parse an XML using Groovy Script's XMLSlurper plugin. I need to read the value in d:editStatus element.
import groovy.xml.*;
def myxml = '<?xml version="1.0" encoding="utf-8"?>' +
'<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">' +
'<entry>' +
'<content type="application/xml">' +
'<m:properties>' +
'<d:key>JobApplication/applicationId=94319</d:key>' +
'<d:status>OK</d:status>' +
'<d:editStatus>UPDATED</d:editStatus>' +
'<d:message>Application has been updated successfully</d:message>' +
'<d:index m:type="Edm.Int32">0</d:index>' +
'<d:httpCode m:type="Edm.Int32">204</d:httpCode>' +
'<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>' +
'</m:properties>' +
'</content>' +
'</entry>' +
'</feed>'
def mystatus = new XmlSlurper().parseText(myxml)
println mystatus
Here, the output should have showed the object form of the xml but it gives me the following output
JobApplication/applicationId=94319OKUPDATEDApplication has been updated successfully0204
It is very wierd as i cannot see any elements, it is concatenating all the values and showing as output. I cannot fetch a single element.
Printing out the value of your GPathResult will by default print out all of the values of its nodes.
To extract the values of individual nodes:
import groovy.xml.*
def myxml = '''
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<entry>
<content type="application/xml">
<m:properties>
<d:key>JobApplication/applicationId=94319</d:key>
<d:status>OK</d:status>
<d:editStatus>UPDATED</d:editStatus>
<d:message>Application has been updated successfully</d:message>
<d:index m:type="Edm.Int32">0</d:index>
<d:httpCode m:type="Edm.Int32">204</d:httpCode>
<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>
</m:properties>
</content>
</entry>
</feed>
'''
def xml = new XmlSlurper().parseText(myxml)
println "key: ${xml.entry.content.properties.key}"
println "status: ${xml.entry.content.properties.status}"
println "editStatus: ${xml.entry.content.properties.editStatus}"
println "message: ${xml.entry.content.properties.message}"
println "index: ${xml.entry.content.properties.index}"
println "httpCode: ${xml.entry.content.properties.httpCode}"
println "inlineResults: ${xml.entry.content.properties.inlineResults}"
You can also extract the value of a node attribute using @
println xml.entry.content.@type