Batch creation of XML files

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Chris Zirker
Posts: 44
Joined: Tue Mar 03, 2009 11:09 am

Batch creation of XML files

Post by Chris Zirker » Mon Mar 29, 2010 7:47 am

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?

User avatar
Daniel Leib
Posts: 75
Joined: Mon Sep 11, 2006 6:44 am

RE: Batch creation of XML files

Post by Daniel Leib » Mon Mar 29, 2010 8:18 am

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

User avatar
Tom Correa
Posts: 5
Joined: Thu Mar 27, 2008 5:43 pm

RE: Batch creation of XML files

Post by Tom Correa » Mon Mar 29, 2010 3:45 pm

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

User avatar
Chris Zirker
Posts: 44
Joined: Tue Mar 03, 2009 11:09 am

RE: Batch creation of XML files

Post by Chris Zirker » Tue Mar 30, 2010 6:55 am

Thanks guys! I'll give the toolbox a shot and let everyone know how it goes.

User avatar
Reinhard Hainisch
Posts: 8
Joined: Tue Mar 11, 2008 8:40 am

RE: Batch creation of XML files

Post by Reinhard Hainisch » Wed Apr 07, 2010 11:40 pm

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

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

RE: Batch creation of XML files

Post by Ayman Habib » Thu Apr 08, 2010 7:52 am

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

POST REPLY