source: trunk/src/xml2struct.m @ 804

Last change on this file since 804 was 772, checked in by sommeria, 11 years ago

manip corrections and cleaning

File size: 3.8 KB
Line 
1% 'xml2struct': read an xml file as a Matlab structure, converts numeric character strings into numbers
2%-----------------------------------------------------------------------
3% function [s,RootTag,errormsg]=xml2struct(filename,varargin)
4%
5% OUTPUT:
6% s= Matlab structure corresponding to the input xml file
7% RootTag= name of the root tag in the xml file
8% errormsg: errormessage, ='' by default
9%
10% INPUT:
11% filename: name of the xml file
12% varargin: optional list of strings to restrict the reading to a selection of subtrees, for instance 'GeometryCalib' (save reading time)
13
14function [s,RootTag,errormsg]=xml2struct(filename,varargin)
15s=[];
16RootTag='';
17errormsg='';
18try
19    t=xmltree(filename);% read the file as an xmltree object t
20catch ME
21    errormsg=ME.message;
22    if ~isempty(regexp(ME.message,'Undefined function'))||~isempty(regexp(ME.message,'Missing'))
23        errormsg=[errormsg ': package xmltree not correctly installed, reload it from www.artefact.tk/software/matlab/xml'];
24    end
25    return
26end
27iline=0;
28
29while isempty(RootTag)
30    iline=iline+1;
31    if strcmp(get(t,iline,'type'),'element')
32        RootTag=get(t,iline,'name');
33    end
34end
35if nargin>1
36    for isub=1:nargin-1
37        uid_sub=find(t,['/' RootTag '/' varargin{isub}]);
38        if isempty(uid_sub)
39            s.(varargin{isub})=[];
40        else
41        tsub=branch(t,uid_sub);
42        ss=convert(tsub);
43        s.(varargin{isub})=convert_string(ss);
44        end
45    end
46else
47    ss=convert(t);%transform the xmltree object into a Matlab structure.
48    s=convert_string(ss);
49end
50
51
52function out=convert_string(ss)
53info=whos('ss');
54switch info.class
55    case 'struct'
56        out=[];%default
57        names = fieldnames(ss);
58        for k=1:length(names)
59            out.(names{k})=convert_string(ss.(names{k}));
60        end
61    case 'char'
62        % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval')
63        if exist(ss,'builtin')||exist(ss,'file')% ss corresponds to the name of a builtin Matlab function or a file
64            out=ss; %reproduce the input string
65        else
66            out=str2num(ss);% convert to number or vector (str2num applied to a fct name executes this fct by 'eval', thus this possibility had to be ruled out above
67            if isempty(out)
68                sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
69                if ~isempty(sep_ind)
70                    sep_ind=[-2 sep_ind length(ss)+1];
71                    out={};
72                    for icolumn=1:length(sep_ind)-1
73                        out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);% get info between separators as a cell array
74                    end
75                else
76                    out=ss; %reproduce the input string
77                end
78            end
79        end
80    case 'cell'
81        out={};%default
82        check_numeric=zeros(size(ss));
83        for ilist=1:numel(ss)
84            if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist}))
85                out{ilist,1}=str2num(ss{ilist});
86                check_numeric(ilist)=1;
87            else
88                sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables
89                if ~isempty(sep_ind)
90                    sep_ind=[-2 sep_ind length(ss{ilist})+1];
91                    for icolumn=1:length(sep_ind)-1
92                        out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
93                    end
94                else
95                    out{ilist,1}=ss{ilist}; %reproduce the input string
96                end
97            end
98        end
99        if isequal(check_numeric,ones(size(ss)))
100            out=cell2mat(out);
101        end
102    otherwise
103        out=ss;
104end
105
106   
Note: See TracBrowser for help on using the repository browser.