XML Manipulation

I’m stuck. I hope someone will take pity on a hack (programmed many years ago) who has been googling his fingers off, but still can’t find the answer to what I will think is a very simple answer.

I’m trying to separate the elements in this very simple xml file (temporarily published to the broader internet) : Wel Link

Using this code:

def params = [
    uri: Waddress,
]

try {
    httpGet(params) { resp ->
        resp.headers.each {
        log.debug "${it.name} : ${it.value}"
    }
    log.debug "response data: ${resp.data}"
    }
} catch (e) {
    log.error "something went wrong: $e"
}

I get:

response data: date08/08/2015time08:43:42TotalBTU13994.269500
Content-Type : text/xml
Connection : close
Server : Z-World Rabbit
Date : Sat, 8 Aug 2015 08:43:14 GMT

Now, I could write code that could parse out the text response, but it would be ugly. I have read multiple places that httpGet returns a response that is an XMLSlurper object, but I can’t for the life of me figure out how to traverse/pull out the elements.

Anyone able to point me hopefully to one duh moment to get me over this seemingly simple issue?

thanks in advance.

Can’t believe I wrote that, and now I think I found the answer. 17th time is the charm… this line helped me:

response.data.children().each { log.debug it.name }

Leaving up in case this helps someone in the future.

PS: It would still be nice to figure out how to access the elements by name, but I’m working on that next myself. I would think I could use the tree (.devices.device) instead of .children. Hmm.

[quote=“bridaus, post:2, topic:20253”]
It would still be nice to figure out how to access the elements by name
[/quote]If response.data has been parsed into an object, you can just treat it like an object. Based on your XML

response.data (or response.data.devices - not sure :slight_smile: ) is an array, so
response.data[0].name
response.data[0].value
etc…

try log.debug response.data.inspect to get a dump of the response, it should be clear what’s in there.

so, if you want to see the xml returned, go ahead and store the xml response in a state var like this

state.xml = response.data

then go into the smartapp detail view in the ide under locations and you will see the whole xml string returned.

I wish the IDE logger would actually log the xml response instead of stripping out the xml formatting.

Ok, so close… thanks for the help!

This line: log.debug resp.data.device[0].name … returns “date”

Now I want to do something like: log.debug resp.data.device.date.value (access the element by it’s name).

I get null. ? I’m trying stuff, suggestions welcome.

This gave me a null, just for reference. Not sure why. But the other code worked.

This is my latest status, dnw=does not work, returns null.

log.debug resp.data.device[0].name //works "date"
log.debug resp.data.device.date.value //dnw
log.debug resp.data.date.value //dnw
log.debug resp.data.date //dnw

Dear,

Did you figure out how to access the value parameter?
I need some help in getting out the data I need from a solar inverter attached to my LAN that I can acces via xlm.

Below is my code:

def parse(String description) {
//this is automatically called when the hub action returns
log.debug "Got Reply"
def xml = parseLanMessage(description)
log.debug “body: ${xml.body}”

def evt2 = createEvent (name: “voltage”, value: ((xml.Vac_l1.text())), unit:“V”)
log.debug "evt2: $evt2"
return [evt2]

The log I receive is:

3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:24 PM: error java.lang.NullPointerException: Cannot invoke method text() on null object
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:24 PM: debug body: <?xml version="1.0" ?>
<real_time_data>
Normal
<Vac_l1>229.9</Vac_l1>
<Vac_l2>225.9</Vac_l2>
<Vac_l3>234.2</Vac_l3>
<Iac_l1>1.23</Iac_l1>
<Iac_l2>1.19</Iac_l2>
<Iac_l3>1.23</Iac_l3>
49.98
49.99
49.97
239
234
247
720
29.8
4.74
5.3
959.40
956.52
975.6
298.7
714.6

711.5
2480
1.24

0.69

</real_time_data>
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:24 PM: debug Got Reply
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug GET /real_time_data.xml HTTP/1.1
Accept: /
User-Agent: Linux UPnP/1.0 SmartThings
HOST: 192.168.1.221:80

3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug Making xml request to C0A801DD:0050
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug Uses which method: null
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug The Header is [HOST:192.168.1.221:80]
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug The device id configured is: C0A801DD:0050
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: debug IP address entered is 192.168.1.221 and the converted hex code is c0a801dd
3a0fa8f1-a32c-4223-8bc9-8bd32ddea5a1 2:13:23 PM: trace Poll Called