[919] | 1 | %'struct2xml': transforms a matlab structure to a xml tree. |
---|
[8] | 2 | %-------------------------------------------------------------- |
---|
[919] | 3 | % |
---|
[8] | 4 | % each field with char string or num vector is transformed into a corresponding xml element |
---|
| 5 | % each field with a matrix containing n lines is transformed into a xml element repeated n times |
---|
[919] | 6 | % each substructure of the input matlab structure is translateed into a subtree in the xml object |
---|
| 7 | % it is also possible to append a subtree to an existing xml object t |
---|
| 8 | % |
---|
| 9 | % t=struct2xml(Object,t,root_uid) |
---|
[8] | 10 | %%%%%%%%%%%%%%%%%%%%%%% |
---|
| 11 | % OUTPUT: |
---|
| 12 | % t: xmltree reproducing the structure of Object |
---|
[939] | 13 | % type 'save(t)' to visualize the xml text and save(t,filename) to save it in a file |
---|
[919] | 14 | % to set the title tag of the file, type t=set(t,1,'name','titletag') |
---|
[8] | 15 | % |
---|
| 16 | % INPUT: |
---|
| 17 | % Object: matlab structure, possibly hierarchical |
---|
[919] | 18 | % t: optional input xml tree in which a subtree needs to be appended |
---|
[8] | 19 | % root_uid: optional uid of the xml element under which the new subtree must be appended |
---|
| 20 | |
---|
[809] | 21 | %======================================================================= |
---|
[1126] | 22 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 23 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 24 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
[809] | 25 | % |
---|
| 26 | % This file is part of the toolbox UVMAT. |
---|
| 27 | % |
---|
| 28 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 29 | % it under the terms of the GNU General Public License as published |
---|
| 30 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 31 | % or (at your option) any later version. |
---|
| 32 | % |
---|
| 33 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 34 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 35 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 36 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 37 | %======================================================================= |
---|
| 38 | |
---|
[8] | 39 | function t=struct2xml(Object,t,root_uid) |
---|
[42] | 40 | |
---|
[8] | 41 | if ~exist('t','var') |
---|
| 42 | t=xmltree; |
---|
| 43 | end |
---|
| 44 | if ~exist('root_uid','var') |
---|
| 45 | root_uid=1; |
---|
| 46 | end |
---|
| 47 | fieldnames=fields(Object); |
---|
| 48 | for ilist=1:length(fieldnames) |
---|
[450] | 49 | val=Object.(fieldnames{ilist}); |
---|
[8] | 50 | if isstruct(val) |
---|
[598] | 51 | [t,branch_uid]=add(t,root_uid,'element',fieldnames{ilist}); |
---|
| 52 | t=struct2xml(val,t,branch_uid); |
---|
| 53 | |
---|
| 54 | % fieldnames_sub=fields(val) |
---|
| 55 | % for ilist_sub=1:length(fieldnames_sub) |
---|
| 56 | % if isstruct(fieldnames_sub{ilist_sub}) |
---|
| 57 | % t=struct2xml(fieldnames_sub{ilist_sub},t,uid); |
---|
| 58 | % % save(t) |
---|
| 59 | % else |
---|
| 60 | % val_sub=val.(fieldnames_sub{ilist_sub}); |
---|
| 61 | % t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub); |
---|
| 62 | % end |
---|
| 63 | % end |
---|
[8] | 64 | else |
---|
| 65 | t=add_element(t,root_uid,fieldnames{ilist},val); |
---|
| 66 | end |
---|
| 67 | end |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 71 | function t=add_element(t,uid,key,val) |
---|
| 72 | if ischar(val) |
---|
| 73 | [t,new_uid]=add(t,uid,'element',key); |
---|
[493] | 74 | val=regexprep(val,'\','\\'); |
---|
[8] | 75 | [t]=add(t,new_uid,'chardata',val); |
---|
[453] | 76 | elseif isnumeric(val)||islogical(val) |
---|
[8] | 77 | siz=size(val); |
---|
| 78 | if length(siz)<=2 %do not translate matrices with more than 2 indices |
---|
| 79 | for iline=1:siz(1) |
---|
| 80 | val_str=num2str(val(iline,:),'%g\t'); |
---|
| 81 | [t,new_uid]=add(t,uid,'element',key); |
---|
| 82 | if siz(1)>1 |
---|
| 83 | t = attributes(t,'add',new_uid,'i',num2str(iline)); |
---|
| 84 | end |
---|
| 85 | [t]=add(t,new_uid,'chardata',val_str); |
---|
| 86 | end |
---|
| 87 | end |
---|
[450] | 88 | elseif iscell(val) |
---|
| 89 | siz=size(val); |
---|
| 90 | if length(siz)<=2 %do not translate cell matrices with more than 2 indices |
---|
| 91 | separator=' '; %mark the separation of columns |
---|
| 92 | for iline=1:siz(1) |
---|
| 93 | val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & ' |
---|
| 94 | [t,new_uid]=add(t,uid,'element',key); |
---|
| 95 | if siz(1)>1 |
---|
| 96 | t = attributes(t,'add',new_uid,'i',num2str(iline)); |
---|
| 97 | end |
---|
| 98 | [t]=add(t,new_uid,'chardata',val_str); |
---|
| 99 | end |
---|
| 100 | end |
---|
[809] | 101 | end |
---|