source: trunk/src/struct2xml.m @ 809

Last change on this file since 809 was 809, checked in by g7moreau, 7 years ago
  • Add license
File size: 3.7 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
16%=======================================================================
17% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
18%   http://www.legi.grenoble-inp.fr
19%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
20%
21%     This file is part of the toolbox UVMAT.
22%
23%     UVMAT is free software; you can redistribute it and/or modify
24%     it under the terms of the GNU General Public License as published
25%     by the Free Software Foundation; either version 2 of the license,
26%     or (at your option) any later version.
27%
28%     UVMAT is distributed in the hope that it will be useful,
29%     but WITHOUT ANY WARRANTY; without even the implied warranty of
30%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31%     GNU General Public License (see LICENSE.txt) for more details.
32%=======================================================================
33
34function t=struct2xml(Object,t,root_uid)
35
36if ~exist('t','var')
37    t=xmltree;
38end
39if ~exist('root_uid','var')
40    root_uid=1;
41end
42fieldnames=fields(Object);
43for ilist=1:length(fieldnames)
44   val=Object.(fieldnames{ilist});
45   if isstruct(val)
46      [t,branch_uid]=add(t,root_uid,'element',fieldnames{ilist});
47       t=struct2xml(val,t,branch_uid);
48     
49%       fieldnames_sub=fields(val)
50%       for ilist_sub=1:length(fieldnames_sub)
51%           if isstruct(fieldnames_sub{ilist_sub})
52%                 t=struct2xml(fieldnames_sub{ilist_sub},t,uid);
53% %                 save(t)
54%           else
55%               val_sub=val.(fieldnames_sub{ilist_sub});
56%               t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub);
57%           end
58%       end
59   else
60       t=add_element(t,root_uid,fieldnames{ilist},val);
61   end
62end
63
64   
65%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66function t=add_element(t,uid,key,val)
67 if ischar(val)
68     [t,new_uid]=add(t,uid,'element',key);
69     val=regexprep(val,'\','\\');
70     [t]=add(t,new_uid,'chardata',val);
71 elseif isnumeric(val)||islogical(val)
72       siz=size(val);
73       if length(siz)<=2 %do not translate matrices with more than 2 indices
74           for iline=1:siz(1)
75                val_str=num2str(val(iline,:),'%g\t');
76                [t,new_uid]=add(t,uid,'element',key);
77                if siz(1)>1
78                    t = attributes(t,'add',new_uid,'i',num2str(iline));
79                end
80                [t]=add(t,new_uid,'chardata',val_str);
81           end
82       end
83 elseif iscell(val)
84      siz=size(val);
85      if length(siz)<=2 %do not translate cell matrices with more than 2 indices
86          separator='   '; %mark the separation of columns
87          for iline=1:siz(1)
88                val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & '
89                [t,new_uid]=add(t,uid,'element',key);
90                if siz(1)>1
91                    t = attributes(t,'add',new_uid,'i',num2str(iline));
92                end
93                [t]=add(t,new_uid,'chardata',val_str);
94          end
95      end
96 end   
Note: See TracBrowser for help on using the repository browser.