source: trunk/src/xml2struct.m @ 781

Last change on this file since 781 was 772, checked in by sommeria, 10 years ago

manip corrections and cleaning

File size: 3.8 KB
RevLine 
[356]1% 'xml2struct': read an xml file as a Matlab structure, converts numeric character strings into numbers
2%-----------------------------------------------------------------------
[686]3% function [s,RootTag,errormsg]=xml2struct(filename,varargin)
[356]4%
5% OUTPUT:
6% s= Matlab structure corresponding to the input xml file
[686]7% RootTag= name of the root tag in the xml file
8% errormsg: errormessage, ='' by default
[356]9%
10% INPUT:
11% filename: name of the xml file
[686]12% varargin: optional list of strings to restrict the reading to a selection of subtrees, for instance 'GeometryCalib' (save reading time)
[356]13
[686]14function [s,RootTag,errormsg]=xml2struct(filename,varargin)
[685]15s=[];
[686]16RootTag='';
[685]17errormsg='';
18try
[772]19    t=xmltree(filename);% read the file as an xmltree object t
[685]20catch ME
21    errormsg=ME.message;
[687]22    if ~isempty(regexp(ME.message,'Undefined function'))||~isempty(regexp(ME.message,'Missing'))
[686]23        errormsg=[errormsg ': package xmltree not correctly installed, reload it from www.artefact.tk/software/matlab/xml'];
[685]24    end
25    return
26end
[565]27iline=0;
[685]28
[686]29while isempty(RootTag)
[565]30    iline=iline+1;
31    if strcmp(get(t,iline,'type'),'element')
[686]32        RootTag=get(t,iline,'name');
[565]33    end
34end
[560]35if nargin>1
36    for isub=1:nargin-1
[686]37        uid_sub=find(t,['/' RootTag '/' varargin{isub}]);
[565]38        if isempty(uid_sub)
39            s.(varargin{isub})=[];
40        else
[560]41        tsub=branch(t,uid_sub);
42        ss=convert(tsub);
43        s.(varargin{isub})=convert_string(ss);
[565]44        end
[560]45    end
46else
[772]47    ss=convert(t);%transform the xmltree object into a Matlab structure.
[560]48    s=convert_string(ss);
49end
[320]50
51
[450]52function out=convert_string(ss)
53info=whos('ss');
[320]54switch info.class
55    case 'struct'
[471]56        out=[];%default
[450]57        names = fieldnames(ss);
[320]58        for k=1:length(names)
[450]59            out.(names{k})=convert_string(ss.(names{k}));
[320]60        end
[663]61    case 'char'
[675]62        % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval')
[772]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
[675]65        else
[772]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
[450]77                end
78            end
[320]79        end
[379]80    case 'cell'
[697]81        out={};%default
[477]82        check_numeric=zeros(size(ss));
83        for ilist=1:numel(ss)
[663]84            if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist}))
[477]85                out{ilist,1}=str2num(ss{ilist});
86                check_numeric(ilist)=1;
87            else
[472]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
[453]97            end
[379]98        end
[477]99        if isequal(check_numeric,ones(size(ss)))
100            out=cell2mat(out);
101        end
[320]102    otherwise
[450]103        out=ss;
[320]104end
105
106   
Note: See TracBrowser for help on using the repository browser.