source: trunk/src/xml2struct.m @ 690

Last change on this file since 690 was 687, checked in by sommeria, 11 years ago

further corrections in error messages fo xml2struct

File size: 3.5 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);
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);
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        out=ss; %reproduce the input string
63        % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval')
64        if ~isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$')) || ~isempty(regexp(ss,'\d+e(-|+)\d+')) % if the string corresponds to a set of numbers (with possible sign and decimal, or scientific notation) separated by blanks
65            out=str2num(ss);
66        else
67            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
68            if ~isempty(sep_ind)
69                sep_ind=[-2 sep_ind length(ss)+1];
70                for icolumn=1:length(sep_ind)-1
71                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
72                end
73            else
74                out=ss; %reproduce the input string
75            end
76        end
77    case 'cell'
78        out=[];%default
79        check_numeric=zeros(size(ss));
80        for ilist=1:numel(ss)
81            if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist}))
82                out{ilist,1}=str2num(ss{ilist});
83                check_numeric(ilist)=1;
84            else
85                sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables
86                if ~isempty(sep_ind)
87                    sep_ind=[-2 sep_ind length(ss{ilist})+1];
88                    for icolumn=1:length(sep_ind)-1
89                        out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
90                    end
91                else
92                    out{ilist,1}=ss{ilist}; %reproduce the input string
93                end
94            end
95        end
96        if isequal(check_numeric,ones(size(ss)))
97            out=cell2mat(out);
98        end
99    otherwise
100        out=ss;
101end
102
103   
Note: See TracBrowser for help on using the repository browser.