Working with XML responses

Hey all,

I’m using sendHubCommand to send a HTTP GET to my local device, and then parse the XML it returns. So I now have my XML content in msg.body, which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="1">
    <Server name="LivingRoom" host="192.168.1.128" address="192.168.1.128" />
    <Server name="Bedroom" host="192.168.1.129" address="192.168.1.129" />
</MediaContainer>

I need to be able to loop through each server element and get the ‘address’ attribute. Could anyone lend a hand?

OK, so I worked it out. I’ll leave it up here for reference for anyone who stumbles upon this in the future:

def response(evt){	 

    def msg = parseLanMessage(evt.description);
    
    if(msg && msg.body){    
        def statusrsp = new XmlSlurper().parseText(msg.body)    
 	def status = statusrsp.Server.each { thing ->

        log.trace thing.@name.text() // Prints out the name attribute
        log.trace thing.@address.text() // Prints out the address attribute
        }
    }
}
1 Like