source: trunk/src/struct2xml.m @ 660

Last change on this file since 660 was 598, checked in by sommeria, 8 years ago

various bugs repaired . civ_series further developed

File size: 2.8 KB
Line 
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
16function t=struct2xml(Object,t,root_uid)
17
18if ~exist('t','var')
19    t=xmltree;
20end
21if ~exist('root_uid','var')
22    root_uid=1;
23end
24fieldnames=fields(Object);
25for ilist=1:length(fieldnames)
26   val=Object.(fieldnames{ilist});
27   if isstruct(val)
28      [t,branch_uid]=add(t,root_uid,'element',fieldnames{ilist});
29       t=struct2xml(val,t,branch_uid);
30     
31%       fieldnames_sub=fields(val)
32%       for ilist_sub=1:length(fieldnames_sub)
33%           if isstruct(fieldnames_sub{ilist_sub})
34%                 t=struct2xml(fieldnames_sub{ilist_sub},t,uid);
35% %                 save(t)
36%           else
37%               val_sub=val.(fieldnames_sub{ilist_sub});
38%               t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub);
39%           end
40%       end
41   else
42       t=add_element(t,root_uid,fieldnames{ilist},val);
43   end
44end
45
46   
47%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48function t=add_element(t,uid,key,val)
49 if ischar(val)
50     [t,new_uid]=add(t,uid,'element',key);
51     val=regexprep(val,'\','\\');
52     [t]=add(t,new_uid,'chardata',val);
53 elseif isnumeric(val)||islogical(val)
54       siz=size(val);
55       if length(siz)<=2 %do not translate matrices with more than 2 indices
56           for iline=1:siz(1)
57                val_str=num2str(val(iline,:),'%g\t');
58                [t,new_uid]=add(t,uid,'element',key);
59                if siz(1)>1
60                    t = attributes(t,'add',new_uid,'i',num2str(iline));
61                end
62                [t]=add(t,new_uid,'chardata',val_str);
63           end
64       end
65 elseif iscell(val)
66      siz=size(val);
67      if length(siz)<=2 %do not translate cell matrices with more than 2 indices
68          separator='   '; %mark the separation of columns
69          for iline=1:siz(1)
70                val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & '
71                [t,new_uid]=add(t,uid,'element',key);
72                if siz(1)>1
73                    t = attributes(t,'add',new_uid,'i',num2str(iline));
74                end
75                [t]=add(t,new_uid,'chardata',val_str);
76          end
77      end
78 end   
Note: See TracBrowser for help on using the repository browser.