reading XML -- String manipulation - Driving me nutz

Moderator: leecollings

Post Reply
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

reading XML -- String manipulation - Driving me nutz

Post by DutchHans »

Good evening,

I am trying to read and manipulate a XML feed from my Sat receiver to find the active channel
My Receiver gives as XML

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<deviceInfoTable>
<deviceInfo>
<ipAdress>XXX.XXX.XXX.XXX</ipAdress>
<productType>ufs923</productType>
<xmlInterfaceVersion>8</xmlInterfaceVersion>
<TVgenial>1</TVgenial>
<connectVersion>10</connectVersion>
<name>ufs923 (XXX.XXX.XXX.XXX)</name>
<VirtualStandBy>FALSE</VirtualStandBy>
<textInput>0</textInput>
<liveChannel>TV.2</liveChannel>
</deviceInfo>
</deviceInfoTable>
As you can see is the Live Channel TV.2

I found the XML Capture function

Code: Select all

function XML_Capture(cmd,flatten)
   local f = assert(io.popen(cmd, 'r'))
   local s = assert(f:read('*a'))
   f:close()
   if flatten  then
      s = string.gsub(s, '^%s+', '')
      s = string.gsub(s, '%s+$', '')
      s = string.gsub(s, '[\n\r]+', ' ')
   end
   return s
end
Which returns:
XML_string=XML_Capture("curl -s 'http://XXX.XXX.XXX.XXX:9000/xmlfeeds/deviceInfo'",1)
XML_string = XXX.XXX.XXX.XXX ufs923 8 1 10 ufs923 (XXX.XXX.XXX.XXX) FALSE 0 TV.2
I want to extract the "TV.2" from the string so:
valid = string.find(XML_string, "TV.")
valid returns 188

but when I change XML_string to the actual string(hardcoded):
valid = string.find("XXX.XXX.XXX.XXX ufs923 8 1 10 ufs923 (XXX.XXX.XXX.XXX) FALSE 0 TV.2", "TV.")
then valid returns 64 (Which is right!)

Where does the valid = 188 come from???

All help is welcome...
regards, Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest