Logic Machine Forum
Help with scripts for reading and searching in xml files! - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Help with scripts for reading and searching in xml files! (/showthread.php?tid=1107)



Help with scripts for reading and searching in xml files! - phongvucba - 25.11.2017

Can anyone help me?
I have an XML info via a "Get http" :
http:/ip/xml/zone/get.xml?xxx
----------------------------
<rows>
<runtime>
<class>zone</class>
<id>2</id>
<source>
<description>SNR 1001121.46</description>
<basic>
<sourceID>0</sourceID>
<sourceType>Z</sourceType>
<sourceDescription>SNR 1001121.46</sourceDescription>
<sourceStatusCode>0</sourceStatusCode>
<sourceStatusText>Working OK</sourceStatusText>
<sourceMode>2</sourceMode>
<player>127.0.0.1:1335</player>
<cmd>
<action>14</action>
<page>240</page>
<slot>0</slot>
<iconUrl>/imgs/visuIconSearch_128px.png</iconUrl>

</cmd>
<cmdCount>1</cmdCount>

</basic>
<status>
<sourceStatusInfoCount>0</sourceStatusInfoCount>
<airPlay>1</airPlay>
<artist>Cao_20Thai_20Son_2CHuong_20Tram</artist>
<album>Se_20Co_20Nguoi_20Can_20Anh_20_28Single_29</album>
<track>Se_20Co_20Nguoi_20Can_20Anh</track>
<imageURL>/~cover/get?key=airplay:2809</imageURL>
<imageURL>/imgs/visuIconNoCoverart_400px.png</imageURL>
<playerIP>127.0.0.1</playerIP>
<playerPort>1335</playerPort>
<submenu>/xml/music/getSubMenu.xml?which=trackMenuAirPlay</submenu>
<info>
<key>AirPlay</key>
<value>Se Co Nguoi Can Anh</value>
<size>0</size>

</info>
<info>
<key>Artist</key>
<value>Cao Thai Son,Huong Tram</value>
<size>0</size>

</info>
<info>
<key>Album</key>
<value>Se Co Nguoi Can Anh (Single)</value>
<size>0</size>

</info>
<info>
<key>Playing</key>
<value>AirPlay</value>
<size>0</size>

</info>
<info>
<key>Status</key>
<value>Playing 44.1 kHz</value>
<size>1</size>

</info>
<infoCount>5</infoCount>
<mostImportantInfo>0</mostImportantInfo>
<infoLevel>0</infoLevel>

</status>

</source>
<outputcount>0</outputcount>
<status>on</status>
<volume>33</volume>

</runtime>
<info>
<message>not all elements are shown</message>

</info>
<db>
<defined>1</defined>
<description>Living room</description>
<language>Lang-german.cfg</language>
<MaxVolumeZoneOn>56</MaxVolumeZoneOn>
<MaxVolume>99</MaxVolume>
<AlarmAction>0</AlarmAction>
<AlarmVolume>25</AlarmVolume>
<AlarmDue>30</AlarmDue>
<Timer1Active>0</Timer1Active>
<Timer1Mode>0</Timer1Mode>
<Timer1Volume>25</Timer1Volume>
<Timer1FromDayMinute>0</Timer1FromDayMinute>
<Timer1ToDayMinute>0</Timer1ToDayMinute>
<Timer2Active>0</Timer2Active>
<Timer2Mode>0</Timer2Mode>
<Timer2Volume>25</Timer2Volume>
<Timer2FromDayMinute>0</Timer2FromDayMinute>
<Timer2ToDayMinute>0</Timer2ToDayMinute>

</db>
<userdata name="rc">0</userdata>
<userdata name="version">
trivum V9.13 build 12489 (compiled May 8 2017 14:32:50)
</userdata>
<userdata name="time">2017-11-25 11:59:36</userdata>
<userdata name="message">done</userdata>

</rows>
===============================
Here:
<basic>

<sourceID>0</sourceID>

<sourceType>Z</sourceType>

<sourceDescription>SNR 1001121.46</sourceDescription>

<sourceStatusCode>0</sourceStatusCode>

<sourceStatusText>Working OK</sourceStatusText>
<sourceMode>2</sourceMode>
<player>127.0.0.1:1335</player>

<cmd>

<action>14</action>

<page>240</page>

<slot>0</slot>

<iconUrl>/imgs/visuIconSearch_128px.png</iconUrl>

</cmd>

<cmdCount>1</cmdCount>
</basic>
Who can write a script to find the string "SNR 1001121.46" in the above paragraph, or the entire XML above?
And write to 1/1/1 on KNX address on Lm3
Or here:
<sourceStatusText>Working OK</sourceStatusText>
who can write a script to find the string "Working OK" ?

Here:
<info>
<key>AirPlay</key>
<value>Se Co Nguoi Can Anh</value>
<size>0</size>
</info>
Who can get the string in "Se Can Nguoi Can Anh" inside <value> Se Can Nguoi Can Anh </ value>. And write to 1/1/2 on KNX address on Lm3
I am very thankful to everyone!  Heart Heart Heart



RE: Help with scripts for reading and searching in xml files! - Erwin van der Zwart - 26.11.2017

Hi,

Try this:
Code:
xmldata = "your XML reply"
for i in string.gmatch(xmldata, '<basic.-</basic>') do
 result = i:match([[<sourceDescription>(.-)</sourceDescription>]])
 if result ~= nil then
   grp.update('1/1/1', result)
 end
end

for i in string.gmatch(xmldata, '<basic.-</basic>') do
 result = i:match([[<sourceStatusText>(.-)</sourceStatusText>]])
 if result ~= nil then
   grp.update('1/1/2', result)
 end
end

for i in string.gmatch(xmldata, '<status.-</status>') do
 for i in string.gmatch(xmldata, '<info.-</info>') do
   selector = i:match([[<key>(.-)</key>]])
   if selector == 'AirPlay' then
     result = i:match([[<value>(.-)</value>]])
     if result ~= nil then
       grp.update('1/1/3', result)
     end
   end
 end
end
BR,

Erwin


RE: Help with scripts for reading and searching in xml files! - phongvucba - 28.11.2017

Thank so much ,pro!
Great ! <3