Rend and receive data from an XML file
Hi @chrscheuer
I've worked out how to send and receive data from a local text file using the "sf.file.writeJson" and "sf.file.readJson" functions as follows;
SEND
var dataToTextFile = {
booleanValue: false,
stringValue: 'test2',
numberValue: 34,
};
sf.file.writeJson({ path: '~/Desktop/temp.txt', json: dataToTextFile });
RECEIVE (or read)
var dataFromTextFile = sf.file.readJson({ path: '~/Desktop/temp.txt' }).json;
alert(dataFromTextFile.stringValue);
alert(dataFromTextFile.booleanValue);
alert(dataFromTextFile.numberValue);
Which is really powerful!
With this concept in mind... I'd like to be able to use the "sf.file.writeXml" and "sf.file.readXml" functions to read and write data to an XML file (ie iZotope RX Preset XML file seen below).
<?xml version="1.0" standalone="yes" ?>
<RXGain PresetVer="4" PluginVer="7000" PluginBuild="213" LastModified="1536258663">
<RXGain Enabled="1">
<Param ElementID="RXGain" ParamID="Gain Db" Value="-1.00000000" />
</RXGain>
</RXGain>
I can't seem to work it out... It seem the Json format is much easier to work with than XML.
Any help would be much apreciated.
Thanks a million.
Kitch
- Kitch Membery @Kitch2020-02-05 02:55:51.703Z
....I worked it out well the writing part. Thanks to one of @JesperA's cubase scripts.
var path = '~/Desktop/gain.xml'; sf.file.writeText({ text: '<?xml version="1.0" standalone="yes" ?>\r\n' + '<RXGain PresetVer="4" PluginVer="7000" PluginBuild="213" LastModified="1536258663">\r\n' + ' <RXGain Enabled="1">\r\n' + ' <Param ElementID="RXGain" ParamID="Gain Db" Value="-1.00000000" />\r\n' + ' </RXGain>\r\n'+ '</RXGain>', path: path });
Jesper Ankarfeldt @JesperA2020-02-05 04:08:04.348Z
Nice you found the way @Kitch.
I only just started to understand better how to work with objects (and I still sometimes get confused).
So just writing the text as a text file with the right extension is the easy way :)And you can super easy add variables in to the text area etc. I create so many XML's on the fly while working, which is awesome.
Kitch Membery @Kitch2020-02-05 05:10:17.309Z
Its a game changer being able to update XML... I still need to work out how to read / retrieve back the individual elements, parameter & values from the XML file, so if you know how to do that I'd love to know how.
Even though I don't use Cubase I downloaded your SoundFlow packages today... and wished I had done it sooner. So many great scripting ideas in there.
Christian Scheuer @chrscheuer2020-02-05 18:18:35.025Z
The best way to get started with reading the XML contents might be to save a the XML-converted json to a file.
The thing is that the XML format is much more complicated to work with in general, so it doesn't map very well to Javascript concepts.
We opted for a specific "conversion" from XML to JSON which allows you to manipulate the json before it'll then be converted back to XML (if you want).So I would do something like:
var doc = sf.file.readXml({ path: 'some_xml_file.xml' }).document; var jsonTextOfDoc = JSON.stringify(doc, null, 4); sf.file.writeText({ text: jsonTextOfDoc, path: 'some_xml_file_as_json.json' });
And then examine the contents of that json file to look how you can access the contents.
Kitch Membery @Kitch2020-02-05 22:21:01.574Z
Thanks Christian,
Using the following script;
var dataFromTextFile = sf.file.readXml({ path: '~/Desktop/gain.xml' }).document; var jsonTextOfDoc = JSON.stringify(dataFromTextFile, null, 4); sf.file.writeText({ text: jsonTextOfDoc, path: '~/Desktop/json_text.json' });
The XML file is converted from XML;
<?xml version="1.0" standalone="yes" ?> <RXGain PresetVer="4" PluginVer="7000" PluginBuild="213" LastModified="1536258663"> <RXGain Enabled="1"> <Param ElementID="RXGain" ParamID="Gain Db" Value="-1.00000000" /> </RXGain> </RXGain>
to json;
[ [ "RXGain", { "PresetVer": "4", "PluginVer": "7000", "PluginBuild": "213", "LastModified": "1536258663" }, [ "RXGain", { "Enabled": "1" }, [ "Param", { "ElementID": "RXGain", "ParamID": "Gain Db", "Value": "-10" } ] ] ] ]
How do I then read the "Value" tag located under, "RXGain","RXGain" tag. My failed attempt was as follows;
var readJsonData = sf.file.readJson({ path: '~/Desktop/json_text.json' }).json; alert(readJsonData.RXGain.RXGain.Param.Value);
SoundFlow logs the following error "RXGain is undefined" in the line...
alert(readJsonData.RXGain.RXGain.Param.Value);
I sure the issue is due to how I'm trying to navigate inside the converted XML RXGain tags.
Thanks again Christian :-)