| 1 | %'struct2xml': transform a matlab structure to a xml tree. |
|---|
| 2 | %-------------------------------------------------------------- |
|---|
| 3 | % each field with char string or num vector is transformed into a corresponding xml element |
|---|
| 4 | % each field with a matrix containing n lines is transformed into a xml element repeated n times |
|---|
| 5 | % WARNING: PROBLEM WITH HIERARCHICAL structures |
|---|
| 6 | %%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 7 | % OUTPUT: |
|---|
| 8 | % t: xmltree reproducing the structure of Object |
|---|
| 9 | % type 'save(t)' to visualize the xml text and save(filename,t) to save it in a file |
|---|
| 10 | % |
|---|
| 11 | % INPUT: |
|---|
| 12 | % Object: matlab structure, possibly hierarchical |
|---|
| 13 | % t: optional input xml tree in which a new branch needs to be appended |
|---|
| 14 | % root_uid: optional uid of the xml element under which the new subtree must be appended |
|---|
| 15 | |
|---|
| 16 | function t=struct2xml(Object,t,root_uid) |
|---|
| 17 | |
|---|
| 18 | if ~exist('t','var') |
|---|
| 19 | t=xmltree; |
|---|
| 20 | end |
|---|
| 21 | if ~exist('root_uid','var') |
|---|
| 22 | root_uid=1; |
|---|
| 23 | end |
|---|
| 24 | fieldnames=fields(Object); |
|---|
| 25 | for ilist=1:length(fieldnames) |
|---|
| 26 | val=Object.(fieldnames{ilist}); |
|---|
| 27 | if isstruct(val) |
|---|
| 28 | [t,uid]=add(t,root_uid,'element',fieldnames{ilist}); |
|---|
| 29 | fieldnames_sub=fields(val); |
|---|
| 30 | for ilist_sub=1:length(fieldnames_sub) |
|---|
| 31 | if isstruct(fieldnames_sub{ilist_sub}) |
|---|
| 32 | t=struct2xml(fieldnames_sub{ilist_sub},t,uid); |
|---|
| 33 | save(t) |
|---|
| 34 | else |
|---|
| 35 | eval(['val_sub=val.' fieldnames_sub{ilist_sub} ';']) |
|---|
| 36 | t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub); |
|---|
| 37 | end |
|---|
| 38 | end |
|---|
| 39 | else |
|---|
| 40 | t=add_element(t,root_uid,fieldnames{ilist},val); |
|---|
| 41 | end |
|---|
| 42 | end |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 46 | function t=add_element(t,uid,key,val) |
|---|
| 47 | if ischar(val) |
|---|
| 48 | [t,new_uid]=add(t,uid,'element',key); |
|---|
| 49 | [t]=add(t,new_uid,'chardata',val); |
|---|
| 50 | elseif isnumeric(val)||islogical(val) |
|---|
| 51 | siz=size(val); |
|---|
| 52 | if length(siz)<=2 %do not translate matrices with more than 2 indices |
|---|
| 53 | for iline=1:siz(1) |
|---|
| 54 | val_str=num2str(val(iline,:),'%g\t'); |
|---|
| 55 | [t,new_uid]=add(t,uid,'element',key); |
|---|
| 56 | if siz(1)>1 |
|---|
| 57 | t = attributes(t,'add',new_uid,'i',num2str(iline)); |
|---|
| 58 | end |
|---|
| 59 | [t]=add(t,new_uid,'chardata',val_str); |
|---|
| 60 | end |
|---|
| 61 | end |
|---|
| 62 | elseif iscell(val) |
|---|
| 63 | siz=size(val); |
|---|
| 64 | if length(siz)<=2 %do not translate cell matrices with more than 2 indices |
|---|
| 65 | separator=' '; %mark the separation of columns |
|---|
| 66 | for iline=1:siz(1) |
|---|
| 67 | val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & ' |
|---|
| 68 | [t,new_uid]=add(t,uid,'element',key); |
|---|
| 69 | if siz(1)>1 |
|---|
| 70 | t = attributes(t,'add',new_uid,'i',num2str(iline)); |
|---|
| 71 | end |
|---|
| 72 | [t]=add(t,new_uid,'chardata',val_str); |
|---|
| 73 | end |
|---|
| 74 | end |
|---|
| 75 | end |
|---|