Retrieve and parse xml data

I'm trying to get data from my Roku device and parse it but my httpGet response seems to always strip out the tags and leave me with unusable strings. See my request below

def params = [
        uri: "http://192.168.7.243:8060/query/apps",	
        contentType: "text/xml", //"application/x-www-form-urlencoded",
        requestContentType: "text/xml",
        //body: myComm
    ]
    //log.debug params
        try {
            httpGet(params){response ->
                if(response.status != 200) {
                    log.error "error: ${response.status}."
                } else {
                    log.debug "Connected to Roku Successfully"
                    log.info response.data
                    log.info response.contentType
                }
            }
        } catch (e){
            log.error "error:${e.getResponse().getData()}"
        } 

This returns

When I open in a browser, this is what I get. What am I doing wrong?

what happens if you change the contentType to "application/xml"

Cant remember but it didn't work. I will try again and post the logs.

Same output. Just a string of all the channels joined together.

Will probably have to use an xmslurper to parse the string into an object.

Would that allow me capture the app id's, versions, etc?

Maybe @ericm can chime in? I think he got around a similar issue with his SmartLife controller.

@stephack Can you print out the headers from the response?

Content-Type: text/xml; charset="utf-8"

Cache-Control: no-cache

Content-Length: 1729

Server: Roku UPnP/1.0 MiniUPnPd/1.4

1 Like

In Internet explorer the output looks like this

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

-<apps>

<app version="2.8.155" type="appl" subtype="rsga" id="32828">DisneyNow</app>

<app version="2.5.1808241101" type="appl" subtype="rsga" id="65978">CNNgo</app>

<app version="1.0.70500038" type="appl" subtype="ndka" id="195316">YouTube TV</app>

<app version="4.5.36" type="appl" subtype="rsga" id="8378">HBO GO</app>

<app version="5.0.81179002" type="appl" subtype="ndka" id="12">Netflix</app>

<app version="10.3.2018092809" type="appl" subtype="ndka" id="13">Amazon Prime Video</app>

<app version="3.4.25" type="appl" subtype="rsga" id="65067">STARZ</app>

<app version="2.4.787" type="appl" subtype="rsga" id="7767">EPIX</app>

<app version="2.0.820" type="appl" subtype="rsga" id="50025">Google Play Movies</app>

<app version="2.5.4" type="appl" subtype="rsga" id="8838">SHOWTIME</app>

<app version="1.3.255395" type="appl" subtype="ndka" id="13842">VUDU</app>

<app version="2.5.4" type="appl" subtype="rsga" id="23333">PBS KIDS Video</app>

<app version="1.19.383" type="appl" subtype="sdka" id="32614">HappyKids.tv</app>

<app version="1.5.9" type="appl" subtype="rsga" id="151908">The Roku Channel</app>

<app version="3.0.10" type="appl" subtype="rsga" id="69091">4K Spotlight</app>

<app version="2.0.3" type="appl" subtype="rsga" id="41922">Roku Recommends</app>

<app version="4.1.1602" type="appl" subtype="sdka" id="2213">Roku Media Player</app>

<app version="2.2.6" type="appl" subtype="rsga" id="35059">HISTORY</app>

<app version="5.4.11" type="appl" subtype="rsga" id="2016">Crackle</app>

<app version="2.5.4" type="appl" subtype="rsga" id="38820">SHOWTIME ANYTIME</app>

<app version="1.0.70500270" type="appl" subtype="ndka" id="837">YouTube</app>

</apps>
1 Like

What does it look like in Chrome or Firefox?

See OP for Chrome

Alright, so the response from the server is getting parsed into a GPathResult automatically. You can read up about it here: The Apache Groovy programming language - Processing XML

result.data is your "apps" node of the xml and you can get its first child like this:
response.data.app[0]

and get the data in the node like this:
response.data.app[0].text()
or any attribute on the node with the @ symbol like this:
response.data.app[0].@version

Here is your chunk of code modified so it will print out the name and version of each Node in "apps":

    def params = [
        uri: "http://192.168.7.243:8060/query/apps",	
        contentType: "text/xml"
    ]
    try {
        httpGet(params){response ->
            if(response.status != 200) {
                log.error "error: ${response.status}."
            } else {
                log.debug "Connected to Roku Successfully"
                response.data.app.each { app ->
                    log.debug "name: ${app.text()} version: ${app.@version}"
        		}
				log.info response.contentType
            }
        }
    } catch (e){
        log.error "error:${e.getResponse().getData()}"
    } 
4 Likes

Thanks as always!!
The link to the documentation should be very helpful as well.