source: trunk/src/struct2xml.m @ 525

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

various bugs repaired, tests in Windows OS

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);
[493]49     val=regexprep(val,'\','\\');
[8]50     [t]=add(t,new_uid,'chardata',val);
[453]51 elseif isnumeric(val)||islogical(val)
[8]52       siz=size(val);
53       if length(siz)<=2 %do not translate matrices with more than 2 indices
54           for iline=1:siz(1)
55                val_str=num2str(val(iline,:),'%g\t');
56                [t,new_uid]=add(t,uid,'element',key);
57                if siz(1)>1
58                    t = attributes(t,'add',new_uid,'i',num2str(iline));
59                end
60                [t]=add(t,new_uid,'chardata',val_str);
61           end
62       end
[450]63 elseif iscell(val)
64      siz=size(val);
65      if length(siz)<=2 %do not translate cell matrices with more than 2 indices
66          separator='   '; %mark the separation of columns
67          for iline=1:siz(1)
68                val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & '
69                [t,new_uid]=add(t,uid,'element',key);
70                if siz(1)>1
71                    t = attributes(t,'add',new_uid,'i',num2str(iline));
72                end
73                [t]=add(t,new_uid,'chardata',val_str);
74          end
75      end
[8]76 end   
Note: See TracBrowser for help on using the repository browser.