source: trunk/src/xml2struct.m @ 849

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