Page 1 of 1

Batch creation of XML files

Posted: Mon Mar 29, 2010 7:47 am
by bazirker
Hi all, I'm trying to figure out a way to edit a large number of XML files automatically rather than opening up each one in an editor. My goal is to create XML files to be used to run IK simulations from command line on each of the data trials I've collected, so all I really need to be able to do is take the first XML file I've created for a given subject and rename the input/output file names and the start/end times for simulation. It seems like matlab would be a good solution since I can easily read trc files to get my trial names and simulation times, but it appears that editing XML files in matlab isn't particularly straight-forward. Are there any quick ways to automate XML file generation?

RE: Batch creation of XML files

Posted: Mon Mar 29, 2010 8:18 am
by danielleib
Hi Chris,

There is a way to do this in MATLAB though it's a bit clunky. It's specifically the .xml extension in MATLAB that causes it to load differently than you'd like, not the actual content of the file, so what you could do is iterate through in a loop where you execute system commands to copy the xml file and name the copy as foo.txt and then import the text file. It will load as an n x 1 cell vector where n is the number of lines in the xml file; each full line will be contained within a cell. You can then use the strrep function to change what you need to and write out the variable using fprintf. You may need to write it out as .txt and then copy and rename as .xml again, I'm not sure as I haven't written out xml files with fprintf.

Again, like I said, a bit clunky, but it works. Alternately you might look in to a more powerful text editor such as Vim, though that of course means you'll have to learn a new program if you don't already know it.

I'm sure there are more elegant solutions out there, hopefully someone else will have some suggestions too!

Hope this helps,

Dan

RE: Batch creation of XML files

Posted: Mon Mar 29, 2010 3:45 pm
by tcorrea
Chris & Daniel:
There's a great MATLAB toolbox with XML read/write functions here:
http://www.mathworks.com/matlabcentral/ ... xmliotools

A simple script for modifying an existing XML file would be:
(1) INPUT: [struct root] = xml_read(filename);
(2) MODIFICATION: struct.parameter = new_parameter_entry;
(3) OUTPUT: xml_write(newfilename,struct,root);


Tom

RE: Batch creation of XML files

Posted: Tue Mar 30, 2010 6:55 am
by bazirker
Thanks guys! I'll give the toolbox a shot and let everyone know how it goes.

RE: Batch creation of XML files

Posted: Wed Apr 07, 2010 11:40 pm
by rynee
Works quite well if all XML Tags are filled with values, but if there are empty tags in the template file 'xml_write' generates a special empty tag close delimiter, /> , as in the following :" <empty-element /> " which causes OpenSim (or as far as I tried the analyze.exe) to stop cooperation.

I don't know how to handle this, therefore I remove all empty tags in the template

RE: Batch creation of XML files

Posted: Thu Apr 08, 2010 7:52 am
by aymanh
Reinhard,

We fixed a similar issue a while ago with some other classes so please report a bug and attach the XML files with empty tags that were problematic so we can fix it.

Thanks,
-Ayman