source: trunk/src/xml2struct.m @ 983

Last change on this file since 983 was 978, checked in by g7moreau, 4 years ago
  • Bad Copyright line
File size: 4.6 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
14%=======================================================================
15% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
16
17%   http://www.legi.grenoble-inp.fr
18%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
19%
20%     This file is part of the toolbox UVMAT.
21%
22%     UVMAT is free software; you can redistribute it and/or modify
23%     it under the terms of the GNU General Public License as published
24%     by the Free Software Foundation; either version 2 of the license,
25%     or (at your option) any later version.
26%
27%     UVMAT is distributed in the hope that it will be useful,
28%     but WITHOUT ANY WARRANTY; without even the implied warranty of
29%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30%     GNU General Public License (see LICENSE.txt) for more details.
31%=======================================================================
32
33function [s,RootTag,errormsg]=xml2struct(filename,varargin)
34s=[];
35RootTag='';
36errormsg='';
37try
38    t=xmltree(filename);% read the file as an xmltree object t
39catch ME
40    errormsg=ME.message;
41    if ~isempty(regexp(ME.message,'Undefined function'))||~isempty(regexp(ME.message,'Missing'))
42        errormsg=[errormsg ': package xmltree not correctly installed, reload it from www.artefact.tk/software/matlab/xml'];
43    end
44    return
45end
46iline=0;
47
48while isempty(RootTag)
49    iline=iline+1;
50    if strcmp(get(t,iline,'type'),'element')
51        RootTag=get(t,iline,'name');
52    end
53end
54if nargin>1
55    for isub=1:nargin-1
56        uid_sub=find(t,['/' RootTag '/' varargin{isub}]);
57        if isempty(uid_sub)
58            s.(varargin{isub})=[];
59        else
60        tsub=branch(t,uid_sub);
61        ss=convert(tsub);
62        s.(varargin{isub})=convert_string(ss);
63        end
64    end
65else
66    ss=convert(t);%transform the xmltree object into a Matlab structure.
67    s=convert_string(ss);
68end
69
70
71function out=convert_string(ss)
72info=whos('ss');
73switch info.class
74    case 'struct'
75        out=[];%default
76        names = fieldnames(ss);
77        for k=1:length(names)
78            out.(names{k})=convert_string(ss.(names{k}));
79        end
80    case 'char'
81        % try to convert to number if the char does not correspond to a function (otherwise str2num calls this function as it uses 'eval')
82        if exist(ss,'builtin')||exist(ss,'file')% ss corresponds to the name of a builtin Matlab function or a file
83            out=ss; %reproduce the input string
84        else
85            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
86            if isempty(out)
87                sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
88                if ~isempty(sep_ind)
89                    sep_ind=[-2 sep_ind length(ss)+1];
90                    out={};
91                    for icolumn=1:length(sep_ind)-1
92                        out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);% get info between separators as a cell array
93                    end
94                else
95                    out=ss; %reproduce the input string
96                end
97            end
98        end
99    case 'cell'
100        out={};%default
101        check_numeric=zeros(size(ss));
102        for ilist=1:numel(ss)
103            if ~strcmp(ss{ilist},'image') && ~isempty(str2num(ss{ilist}))
104                out{ilist,1}=str2num(ss{ilist});
105                check_numeric(ilist)=1;
106            else
107                sep_ind=regexp(ss{ilist},'\s&\s');% check for separator ' & ' which indicates column separation in tables
108                if ~isempty(sep_ind)
109                    sep_ind=[-2 sep_ind length(ss{ilist})+1];
110                    for icolumn=1:length(sep_ind)-1
111                        out{ilist,icolumn}=ss{ilist}(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
112                    end
113                else
114                    out{ilist,1}=ss{ilist}; %reproduce the input string
115                end
116            end
117        end
118        if isequal(check_numeric,ones(size(ss)))
119            out=cell2mat(out);
120        end
121    otherwise
122        out=ss;
123end
124
125   
Note: See TracBrowser for help on using the repository browser.