Batch creation of XML files
- Chris Zirker
- Posts: 44
- Joined: Tue Mar 03, 2009 11:09 am
Batch creation of XML files
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?
- Daniel Leib
- Posts: 75
- Joined: Mon Sep 11, 2006 6:44 am
RE: Batch creation of XML files
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
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
- Tom Correa
- Posts: 5
- Joined: Thu Mar 27, 2008 5:43 pm
RE: Batch creation of XML files
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
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
- Chris Zirker
- Posts: 44
- Joined: Tue Mar 03, 2009 11:09 am
RE: Batch creation of XML files
Thanks guys! I'll give the toolbox a shot and let everyone know how it goes.
- Reinhard Hainisch
- Posts: 8
- Joined: Tue Mar 11, 2008 8:40 am
RE: Batch creation of XML files
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
I don't know how to handle this, therefore I remove all empty tags in the template
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Batch creation of XML files
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
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