source: trunk/src/xml2struct.m @ 471

Last change on this file since 471 was 471, checked in by sommeria, 12 years ago

a few bugs repaired

File size: 1.5 KB
Line 
1% 'xml2struct': read an xml file as a Matlab structure, converts numeric character strings into numbers
2%-----------------------------------------------------------------------
3% function s=xml2struct(filename)
4%
5% OUTPUT:
6% s= Matlab structure corresponding to the input xml file
7%
8% INPUT:
9% filename: name of the xml file
10
11function s=xml2struct(filename)
12t=xmltree(filename);
13ss=convert(t);
14s=convert_string(ss);
15
16
17function out=convert_string(ss)
18info=whos('ss');
19switch info.class
20    case 'struct'
21        out=[];%default
22        names = fieldnames(ss);
23        for k=1:length(names)
24            out.(names{k})=convert_string(ss.(names{k}));
25        end
26    case 'char'   
27        if isempty(regexp(ss,'^(-*\d+\.*\d*\ *)+$'))% if the string does not contains a set of numbers (with possible sign and decimal) separated by blanks
28            sep_ind=regexp(ss,'\s&\s');% check for separator ' & ' which indicates column separation in tables
29            if ~isempty(sep_ind)
30                sep_ind=[-2 sep_ind length(ss)+1];
31                for icolumn=1:length(sep_ind)-1
32                    out{1,icolumn}=ss(sep_ind(icolumn)+3:sep_ind(icolumn+1)-1);
33                end
34            else
35                out=ss; %reproduce the input string
36            end
37        else
38            out=str2num(ss);
39        end
40    case 'cell'
41        out=[];%default
42        for ilist=1:numel(ss)
43            if ~isempty(str2num(ss{ilist}))
44            out(ilist,:)=str2num(ss{ilist});
45            end
46        end
47    otherwise
48        out=ss;
49end
50
51   
Note: See TracBrowser for help on using the repository browser.