XPATH parsing XML into array

I am retreiving the favorite list from my Marantz receiver by http. It looks like:

<rx>
<cmd>
<list>
<favorite>
<No>01</No>
<FuncName>TUNER</FuncName>
<ItemName>NDR Info</ItemName>
</favorite>
<favorite>
<No>02</No>
<FuncName>TUNER</FuncName>
<ItemName>NDR Kult</ItemName>
</favorite>
<favorite>
<No>03</No>
<FuncName>Internet Radio</FuncName>
<ItemName>Radio Swiss Jazz</ItemName>
</favorite>
</list>
</cmd>
</rx>

I want to store the fields from the XML into an array (later), but I can’t find out how to read the next item from the XML. I’ve put the above XML into the variable “favs”, and then this works:

val mytest1 = transform("XPATH", "/rx/cmd/list/favorite/No/text()"      , favs )
val mytest2 = transform("XPATH", "/rx/cmd/list/favorite/FuncName/text()", favs )
val mytest3 = transform("XPATH", "/rx/cmd/list/favorite/ItemName/text()", favs )

logInfo('Warn',"1: "+mytest1)
logInfo('Warn',"2: "+mytest2)
logInfo('Warn',"3: "+mytest3)

returns:

2019-05-06 21:43:21.003 [INFO ] [.eclipse.smarthome.model.script.Warn] - 1: 01
2019-05-06 21:43:21.004 [INFO ] [.eclipse.smarthome.model.script.Warn] - 2: TUNER
2019-05-06 21:43:21.004 [INFO ] [.eclipse.smarthome.model.script.Warn] - 3: NDR Info

But I have no idea how I can read further down the string “favs” and how to retrieve the next “favorite”.
Thanks for any advice

Udo

https://stackoverflow.com/questions/4007413/xpath-query-to-get-nth-instance-of-an-element should work. I’ve never done this in OH though.

Thanks Rich, this leeds to the right direction.

This works:

val mytest4 = transform("XPATH", "/descendant::No[2]"   , favs )
val mytest5 = transform("XPATH", "/descendant::FuncName[2]", favs )
val mytest6 = transform("XPATH", "/descendant::ItemName[2]", favs )

and retrieves the 2nd item.

My loop now looks like this:

var i = 0
while((i=i+1) < 4) {

  val myno   = transform("XPATH", "/descendant::No["+i+"]"      , favs )
  val myfunc = transform("XPATH", "/descendant::FuncName["+i+"]", favs )
  val myitem = transform("XPATH", "/descendant::ItemName["+i+"]", favs )
 
  logInfo('Warn',"--------------- "+i)

  logInfo('Warn',"myno  : "+myno)
  logInfo('Warn',"myfunc: "+myfunc)
  logInfo('Warn',"myitem: "+myitem)
}

and output is:

2019-05-06 23:37:03.005 [INFO ] [.eclipse.smarthome.model.script.Warn] - --------------- 1
2019-05-06 23:37:03.006 [INFO ] [.eclipse.smarthome.model.script.Warn] - myno  : 01
2019-05-06 23:37:03.007 [INFO ] [.eclipse.smarthome.model.script.Warn] - myfunc: TUNER
2019-05-06 23:37:03.007 [INFO ] [.eclipse.smarthome.model.script.Warn] - myitem: NDR Info
2019-05-06 23:37:03.011 [INFO ] [.eclipse.smarthome.model.script.Warn] - --------------- 2
2019-05-06 23:37:03.012 [INFO ] [.eclipse.smarthome.model.script.Warn] - myno  : 02
2019-05-06 23:37:03.012 [INFO ] [.eclipse.smarthome.model.script.Warn] - myfunc: TUNER
2019-05-06 23:37:03.013 [INFO ] [.eclipse.smarthome.model.script.Warn] - myitem: NDR Kult
2019-05-06 23:37:03.015 [INFO ] [.eclipse.smarthome.model.script.Warn] - --------------- 3
2019-05-06 23:37:03.016 [INFO ] [.eclipse.smarthome.model.script.Warn] - myno  : 03
2019-05-06 23:37:03.016 [INFO ] [.eclipse.smarthome.model.script.Warn] - myfunc: Internet Radio
2019-05-06 23:37:03.016 [INFO ] [.eclipse.smarthome.model.script.Warn] - myitem: Radio Swiss Jazz

So, next I will try to put this into an array, but not more today :wink:
Udo