Parsing XML within Smartapp/Device

(Brad Sileo) #1

Has anyone found a way to parse an XMl result coming back from an HTTPGET or HTTPPost request? if the result is JSOn this is nice and easy, but I cna not find a way to do this with XML. I tried to use an XMLParse object but this failed.

I then tried to just use a Pattern and this also failed:

  def p = ~/<isEnable>?<\/isEnable>/
  assert p instanceof java.util.regex.Pattern
  assert p.matcher(aline).matches() { log.debug("Match=" + it) }

9:33:26 PM: error Invoking methods on class java.util.regex.Pattern is not allowed

Am I coding this wrong or is it just not supported?

It would be great if all objects had a JSON API but this is not the case for the Foscam I am working on.

(Todd Wackford) #2

Take a look at parsing the xml dom or xmlslurper. These might show you how to do what you’re looking for.

(Yogesh Mhatre) #3

Im trying to do same. Any help with this?

Im trying to get output

def fullStatus = [
    	uri: "fullStatus",
        body: [ SessionID: token, LocationID: location, LastSequenceNumber: 0, LastUpdatedTimestampTicks: 0, PartitionID: 1]
    httpPost(fullStatus) {responseData -> 
        def list = new XmlSlurper().parseText(responseData)
        def state =
        log.debug list

The output of this Post call is an XML result that looks like this

<StatusResults xmlns:xsi="" xmlns:xsd="" xmlns="/TC2/">
<Status LastUpdatedTimestampTicks="635763294780000000" ConfigurationSequenceNumber="10" IsInACLoss="false" IsInLowBattery="false">

Of this, Im really interested in the value of State (def state), which I can easily get, but Im not able to get the value of the “LastUpdateTimestampTicks” field

log.debug of list, ends up with groovy errors

( - Make your home your butler!) #5

Todd how does one do a Regex match and extract the groups in ST?

(the rooms manager guy) #6

while this code works on HE, it does not work on ST. sorry.

use group … heres a snippet from my own code:

def str2 = [] def regX2 = /\[(.+?):\[(.+?,.+?)\]\]/ if ((m = findInString =~ regX2)) str2[i] = [,]