source: trunk/src/struct2xml.m @ 453

Last change on this file since 453 was 453, checked in by sommeria, 12 years ago

series adapted to BATCH mode. Tests still needed.

File size: 2.7 KB
RevLine 
[8]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)
[42]17
[8]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)
[450]26   val=Object.(fieldnames{ilist});
[8]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
42end
43
44   
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46function 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);
[453]50 elseif isnumeric(val)||islogical(val)
[8]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
[450]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
[8]75 end   
Note: See TracBrowser for help on using the repository browser.