source: trunk/src/struct2xml.m @ 45

Last change on this file since 45 was 42, checked in by sommeria, 14 years ago

-proj_field, plot_field, civ_3D: bug repair for 3D3C velocity fields (not finished)
-check_function: introduce try/catch for function datenum (error with french systems)
-cleaning of warndlg_uvmat in different fcts (replaced by msgbox_uvmat)

File size: 2.1 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   eval(['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
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);
50 elseif isnumeric(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 end   
Note: See TracBrowser for help on using the repository browser.