Changeset 925 for trunk/src/@xmltree/convert.m
- Timestamp:
- Feb 17, 2016, 12:52:48 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/@xmltree/convert.m
r821 r925 1 1 function s = convert(tree,uid) 2 % XMLTREE/CONVERT Converter an XML tree in a Matlabstructure2 % XMLTREE/CONVERT Converter an XML tree in a structure 3 3 % 4 4 % tree - XMLTree object … … 6 6 % Default is root 7 7 % s - converted structure 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 % Convert an xmltree into a Matlabstructure, when possible.10 % Convert an XMLTree into a structure, when possible. 11 11 % When several identical tags are present, a cell array is used. 12 12 % The root tag is not saved in the structure. 13 13 % If provided, only the structure corresponding to the subtree defined 14 14 % by the uid UID is returned. 15 %_______________________________________________________________________ 16 % @(#)convert.m Guillaume Flandin 02/04/11 15 %__________________________________________________________________________ 16 % Copyright (C) 2002-2015 http://www.artefact.tk/ 17 18 % Guillaume Flandin 19 % $Id: convert.m 6480 2015-06-13 01:08:30Z guillaume $ 17 20 18 21 % Exemple: 19 % tree : <toto><titi>field1</titi><tutu>field2</tutu><titi>field3</titi></toto>20 % toto = convert( tree);21 % <=> toto = struct(' titi',{{'field1', 'field3'}},'tutu','field2')22 % tree = '<a><b>field1</b><c>field2</c><b>field3</b></a>'; 23 % toto = convert(xmltree(tree)); 24 % <=> toto = struct('b',{{'field1', 'field3'}},'c','field2') 22 25 23 error(nargchk(1,2,nargin));26 %error(nargchk(1,2,nargin)); 24 27 25 28 % Get the root uid of the output structure 26 29 if nargin == 1 27 28 30 % Get the root uid of the XML tree 31 root_uid = root(tree); 29 32 else 30 31 33 % Uid provided by user 34 root_uid = uid; 32 35 end 33 36 … … 43 46 s = rmfield(s,'deletedummy'); 44 47 45 %======================================================================= 48 %========================================================================== 46 49 function s = sub_convert(tree,s,uid,arg) 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 if nboccur| (nboccur2>1)65 66 67 68 69 70 71 72 73 74 75 76 50 type = get(tree,uid,'type'); 51 switch type 52 case 'element' 53 child = children(tree,uid); 54 l = {}; 55 ll = {}; 56 for i=1:length(child) 57 if isfield(tree,child(i),'name') 58 ll = { ll{:}, get(tree,child(i),'name') }; 59 end 60 end 61 for i=1:length(child) 62 if isfield(tree,child(i),'name') 63 name = get(tree,child(i),'name'); 64 nboccur = sum(ismember(l,name)); 65 nboccur2 = sum(ismember(ll,name)); 66 l = { l{:}, name }; 67 if nboccur || (nboccur2>1) 68 arg2 = { arg{:}, name, {nboccur+1} }; 69 else 70 arg2 = { arg{:}, name}; 71 end 72 else 73 arg2 = arg; 74 end 75 s = sub_convert(tree,s,child(i),arg2); 76 end 77 if isempty(child) 78 s = sub_setfield(s,arg{:},''); 79 end 77 80 %- saving attributes : does not work with <a t='q'>b</a> 78 81 %- but ok with <a t='q'><c>b</c></a> … … 81 84 % arg2 = {arg{:} 'attributes'}; %- 82 85 % s = sub_setfield(s,arg2{:},attrb); %- 83 % 84 85 86 %- convert strings into their Matlabequivalent when possible87 86 % end %- 87 case 'chardata' 88 s = sub_setfield(s,arg{:},get(tree,uid,'value')); 89 %- convert strings into their numerical equivalent when possible 90 %- e.g. string '3.14159' becomes double scalar 3.14159 88 91 % v = get(tree,uid,'value'); %- 89 % 90 % 91 % 92 % 93 % 92 % cv = str2num(v); %- 93 % if isempty(cv) %- 94 % s = sub_setfield(s,arg{:},v); %- 95 % else %- 96 % s = sub_setfield(s,arg{:},cv); %- 94 97 % end %- 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 try, 111 112 catch, 113 warning('[XMLTREE] Unknown target application');114 115 116 117 118 119 120 121 122 %======================================================================= 98 case 'cdata' 99 s = sub_setfield(s,arg{:},get(tree,uid,'value')); 100 case 'pi' 101 % Processing instructions are evaluated if possible 102 app = get(tree,uid,'target'); 103 switch app 104 case {'matlab',''} 105 s = sub_setfield(s,arg{:},eval(get(tree,uid,'value'))); 106 case 'unix' 107 s = sub_setfield(s,arg{:},unix(get(tree,uid,'value'))); 108 case 'dos' 109 s = sub_setfield(s,arg{:},dos(get(tree,uid,'value'))); 110 case 'system' 111 s = sub_setfield(s,arg{:},system(get(tree,uid,'value'))); 112 otherwise 113 try 114 s = sub_setfield(s,arg{:},feval(app,get(tree,uid,'value'))); 115 catch 116 warning('[XMLTree] Unknown target application'); 117 end 118 end 119 case 'comment' 120 % Comments are forgotten 121 otherwise 122 warning(sprintf('Type %s unknown : not saved',get(tree,uid,'type'))); 123 end 124 125 %========================================================================== 123 126 function s = sub_setfield(s,varargin) 124 127 % Same as setfield but using '{}' rather than '()' … … 131 134 if (isa(varargin{i}, 'cell')) 132 135 types{i} = '{}'; 133 elseif is str(varargin{i})136 elseif ischar(varargin{i}) 134 137 types{i} = '.'; 135 138 subs{i} = varargin{i}; %strrep(varargin{i},' ',''); % deblank field name
Note: See TracChangeset
for help on using the changeset viewer.