source: trunk/src/struct2xml.m @ 1157

Last change on this file since 1157 was 1127, checked in by g7moreau, 11 months ago

Update Joel email

File size: 3.9 KB
Line 
1%'struct2xml': transforms a matlab structure to a xml tree.
2%--------------------------------------------------------------
3%
4% each field with char string or num vector is transformed into a corresponding  xml element
5% each field with a matrix containing n lines is transformed into a xml element repeated n times
6% each substructure of the input matlab structure is translateed into a subtree in the xml object
7% it is also possible to append a subtree to an existing xml object t
8%
9% t=struct2xml(Object,t,root_uid)
10%%%%%%%%%%%%%%%%%%%%%%%
11% OUTPUT:
12% t: xmltree reproducing the structure of Object
13% type 'save(t)' to visualize the xml text and save(t,filename) to save it in a file
14% to set the title tag of the file, type t=set(t,1,'name','titletag')
15%
16% INPUT:
17%  Object: matlab structure, possibly hierarchical
18%  t: optional input xml tree in which a subtree needs to be appended
19%  root_uid: optional uid of the xml element under which the new subtree must be appended
20
21%=======================================================================
22% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
23%   http://www.legi.grenoble-inp.fr
24%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
25%
26%     This file is part of the toolbox UVMAT.
27%
28%     UVMAT is free software; you can redistribute it and/or modify
29%     it under the terms of the GNU General Public License as published
30%     by the Free Software Foundation; either version 2 of the license,
31%     or (at your option) any later version.
32%
33%     UVMAT is distributed in the hope that it will be useful,
34%     but WITHOUT ANY WARRANTY; without even the implied warranty of
35%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36%     GNU General Public License (see LICENSE.txt) for more details.
37%=======================================================================
38
39function t=struct2xml(Object,t,root_uid)
40
41if ~exist('t','var')
42    t=xmltree;
43end
44if ~exist('root_uid','var')
45    root_uid=1;
46end
47fieldnames=fields(Object);
48for ilist=1:length(fieldnames)
49   val=Object.(fieldnames{ilist});
50   if isstruct(val)
51      [t,branch_uid]=add(t,root_uid,'element',fieldnames{ilist});
52       t=struct2xml(val,t,branch_uid);
53     
54%       fieldnames_sub=fields(val)
55%       for ilist_sub=1:length(fieldnames_sub)
56%           if isstruct(fieldnames_sub{ilist_sub})
57%                 t=struct2xml(fieldnames_sub{ilist_sub},t,uid);
58% %                 save(t)
59%           else
60%               val_sub=val.(fieldnames_sub{ilist_sub});
61%               t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub);
62%           end
63%       end
64   else
65       t=add_element(t,root_uid,fieldnames{ilist},val);
66   end
67end
68
69   
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71function t=add_element(t,uid,key,val)
72 if ischar(val)
73     [t,new_uid]=add(t,uid,'element',key);
74     val=regexprep(val,'\','\\');
75     [t]=add(t,new_uid,'chardata',val);
76 elseif isnumeric(val)||islogical(val)
77       siz=size(val);
78       if length(siz)<=2 %do not translate matrices with more than 2 indices
79           for iline=1:siz(1)
80                val_str=num2str(val(iline,:),'%g\t');
81                [t,new_uid]=add(t,uid,'element',key);
82                if siz(1)>1
83                    t = attributes(t,'add',new_uid,'i',num2str(iline));
84                end
85                [t]=add(t,new_uid,'chardata',val_str);
86           end
87       end
88 elseif iscell(val)
89      siz=size(val);
90      if length(siz)<=2 %do not translate cell matrices with more than 2 indices
91          separator='   '; %mark the separation of columns
92          for iline=1:siz(1)
93                val_str=cell2mat(cell2tab(val(iline,:),' & ')); % produce a line string with column separator ' & '
94                [t,new_uid]=add(t,uid,'element',key);
95                if siz(1)>1
96                    t = attributes(t,'add',new_uid,'i',num2str(iline));
97                end
98                [t]=add(t,new_uid,'chardata',val_str);
99          end
100      end
101 end   
Note: See TracBrowser for help on using the repository browser.