XMLParse - Some better examples please

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 454
Joined: Tue Jan 16, 2007 9:00 am
Contact:

XMLParse - Some better examples please

Post by Phil Pendlebury » Thu Jul 20, 2017 9:14 am

For example I want to get (and edit) the data from a branch in this XML file:

Trying to get the <list name="Path" type="string"> data so I can edit it and paste it back.

I can figure out the editing and re-writing part but I have tried everything using the one example in the manual and cannot get the XMLParse command to work at all. Just keep getting errors.

I read the file into a var and then tried various experiments with the XMLParse command but no luck.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<PAP_TEST>
   <int name="Can Modify" value="1"/>
   <member name="Preset">
      <int name="Version" value="1"/>
      <int name="Level" value="4"/>
      <int name="MidiClick" value="1"/>
      <int name="Channel" value="8"/>
      <list name="Pitch" type="int">
         <item value="60"/>
         <item value="60"/>
         <item value="60"/>
         <item value="60"/>
      </list>
      <list name="Vel" type="int">
         <item value="100"/>
         <item value="100"/>
         <item value="100"/>
         <item value="100"/>
      </list>
      <list name="AudioVol" type="int">
         <item value="32767"/>
         <item value="16383"/>
         <item value="16383"/>
         <item value="20946"/>
      </list>
      <list name="UserSample" type="int">
         <item value="0"/>
         <item value="0"/>
         <item value="0"/>
         <item value="0"/>
      </list>
      <list name="Path" type="string">
         <item value="E:\Samples\Metronome\Various\Can H.wav"/>
         <item value=""/>
         <item value=""/>
         <item value="E:\Samples\Metronome\Various\Can L.wav"/>
      </list>
      <int name="AudioClick" value="1"/>
      <int name="Beep" value="0"/>
      <int name="CustomClick" value="1"/>
   </member>
</PAP_TEST>
Phil Pendlebury - Web - Soundcloud - Facebook - MEAP

User avatar
Marcus Tettmar
Site Admin
Posts: 7337
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: XMLParse - Some better examples please

Post by Marcus Tettmar » Mon Jul 24, 2017 11:04 am

Here you go - a couple of simple examples before a loop which reads through the items in the "Path" list you are referring to:

Code: Select all

//get the xml to a string
LabelToVar>xml,strXML

//some examples - get the first int value in member Preset using index
XMLParse>strXML,/PAP_TEST/member[@name='Preset']/int[1]/@value,res,ni

//get the value of int with name=Level in the member with name=Preset
XMLParse>strXML,/PAP_TEST/member[@name='Preset']/int[@name='Level']/@value,res,ni

//now - what you want to do - loop through all the items in the list of name "Path" and get each item's "value"
XMLParse>strXML,/PAP_TEST/member[@name='Preset']/list[@name='Path']/item,res,NumItems
If>NumItems>0
  Let>k=0
  Repeat>k
    Let>k=k+1
      XMLParse>strXML,/PAP_TEST/member[@name='Preset']/list[@name='Path']/item[%k%]/@value,res,n
      MessageModal>res
  Until>k=NumItems
Endif

/*
xml:
<?xml version="1.0" encoding="utf-8"?>
<PAP_TEST>
   <int name="Can Modify" value="1"/>
   <member name="Preset">
      <int name="Version" value="1"/>
      <int name="Level" value="4"/>
      <int name="MidiClick" value="1"/>
      <int name="Channel" value="8"/>
      <list name="Pitch" type="int">
         <item value="60"/>
         <item value="60"/>
         <item value="60"/>
         <item value="60"/>
      </list>
      <list name="Vel" type="int">
         <item value="100"/>
         <item value="100"/>
         <item value="100"/>
         <item value="100"/>
      </list>
      <list name="AudioVol" type="int">
         <item value="32767"/>
         <item value="16383"/>
         <item value="16383"/>
         <item value="20946"/>
      </list>
      <list name="UserSample" type="int">
         <item value="0"/>
         <item value="0"/>
         <item value="0"/>
         <item value="0"/>
      </list>
      <list name="Path" type="string">
         <item value="E:\Samples\Metronome\Various\Can H.wav"/>
         <item value=""/>
         <item value=""/>
         <item value="E:\Samples\Metronome\Various\Can L.wav"/>
      </list>
      <int name="AudioClick" value="1"/>
      <int name="Beep" value="0"/>
      <int name="CustomClick" value="1"/>
   </member>
</PAP_TEST>
*/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 454
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: XMLParse - Some better examples please

Post by Phil Pendlebury » Mon Jul 24, 2017 12:21 pm

Thanks Marcus, wasn't expecting all that. Appreciate the time. :-) I am sure this will help other too though.
Phil Pendlebury - Web - Soundcloud - Facebook - MEAP

User avatar
Phil Pendlebury
Automation Wizard
Posts: 454
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: XMLParse - Some better examples please

Post by Phil Pendlebury » Mon Jul 24, 2017 12:52 pm

Was trying to figure how to get data from an actual xml file rather than one included in the script.

Edited for clarity.
Last edited by Phil Pendlebury on Mon Jul 24, 2017 1:18 pm, edited 1 time in total.
Phil Pendlebury - Web - Soundcloud - Facebook - MEAP

User avatar
Phil Pendlebury
Automation Wizard
Posts: 454
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: XMLParse - Some better examples please

Post by Phil Pendlebury » Mon Jul 24, 2017 1:03 pm

LabelToVar (I never used that command before) is so you can include the text within the script.

So to read from a file named ThisXFile.xml on Desktop:

Code: Select all

ReadFile>%DESKTOP_DIR%\ThisXFile.xml,strXML
Does it.
Phil Pendlebury - Web - Soundcloud - Facebook - MEAP

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts