Changeset 925
- Timestamp:
- Feb 17, 2016, 12:52:48 PM (9 years ago)
- Location:
- trunk/src/@xmltree
- Files:
-
- 31 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/@xmltree/Contents.m
r821 r925 1 % XMLTree: XML Toolbox for M atlab.2 % Version 1.2 17-Nov-20041 % XMLTree: XML Toolbox for MATLAB and GNU Octave 2 % Version 2.0 14-Aug-2015 3 3 % 4 4 % XML file I/O. … … 11 11 % branch - Extract a subtree from a tree. 12 12 % children - Return children of a node. 13 % convert - Convert a tree in a M atlabstructure.13 % convert - Convert a tree in a MATLAB structure. 14 14 % copy - Copy nodes within a tree. 15 15 % delete - Delete a node in a tree. … … 24 24 % root - Return the root element of a tree. 25 25 % set - Set node properties. 26 % setfilename - Set filename 26 % setfilename - Set filename. 27 27 % 28 % Graphical user interface methods (work in progress). 29 % editor - Reimplementation of <view> for Matlab 6+ 30 % view - Graphical display of a tree. 31 % view_ui - Useful function for view method. 28 % Graphical user interface methods (basic). 29 % editor - Graphical display of a tree. 32 30 % 33 31 % Low level class methods. 34 32 % char - Convert a tree into a string (for display). 35 % display - Display a tree in to MATLAB.33 % display - Display a tree in the workspace. 36 34 % 37 35 % Private methods. 38 36 % xml_parser - XML parser. 39 % xml_findstr - Find one string within another ( mexfile)37 % xml_findstr - Find one string within another (C-MEX file). 40 38 % 41 % Conversions Matlab <=> XML39 % Conversions struct <=> XML. 42 40 % loadxml - 43 41 % savexml - … … 51 49 % xmldemo3 - Read an XML file, modify some fields and save it. 52 50 53 % Copyright 2002-2004 Guillaume Flandin <Guillaume@artefact.tk> 54 % $Revision: 1.2 $ 51 % Copyright 2002-2015 http://www.artefact.tk/ 52 53 % Guillaume Flandin <Guillaume@artefact.tk> 54 % $Id: Contents.m 6480 2015-06-13 01:08:30Z guillaume $ -
trunk/src/@xmltree/add.m
r723 r925 13 13 % tree = add(tree,uid,type,parameter); 14 14 % [tree, new_uid] = add(tree,uid,type,parameter); 15 %_______________________________________________________________________ 15 %__________________________________________________________________________ 16 16 % 17 17 % Add a node (element, chardata, cdata, pi or comment) in the XML Tree. … … 20 20 % deal with the attributes of an element node (initialized empty). 21 21 % The tree parameter must be in input AND in output. 22 %_______________________________________________________________________ 23 % @(#)add.m Guillaume Flandin 02/03/2922 %__________________________________________________________________________ 23 % Copyright (C) 2002-2011 http://www.artefact.tk/ 24 24 25 error(nargchk(4,4,nargin)); 25 % Guillaume Flandin 26 % $Id: add.m 4460 2011-09-05 14:52:16Z guillaume $ 27 26 28 27 29 if ~isa(uid,'double') 28 30 error('[XMLTree] UID must be a double array.'); 29 31 end 30 32 if ~ischar(type) 31 33 error('[XMLTree] TYPE must be a valid item type.'); 32 34 end 33 35 if strcmp(type,'pi') 34 if ~isfield(parameter,'target') | ~isfield(parameter,'value')| ...35 ~ischar(parameter.target)| ~ischar(parameter.value)36 error('[XMLTree] For a Processing Instruction, ',...37 'PARAMETER must be a struct.');38 36 if ~isfield(parameter,'target') || ~isfield(parameter,'value') || ... 37 ~ischar(parameter.target) || ~ischar(parameter.value) 38 error(['[XMLTree] For a Processing Instruction, ',... 39 'PARAMETER must be a struct.']); 40 end 39 41 elseif ~ischar(parameter) 40 42 error('[XMLTree] PARAMETER must be a string.'); 41 43 end 42 44 43 45 if nargout == 2 44 45 varargout{2} = (l+1):(l+prod(size(uid)));46 l = length(tree.tree); 47 varargout{2} = (l+1):(l+numel(uid)); 46 48 end 47 49 48 for i=1: prod(size(uid))49 if uid(i)<1| uid(i)>length(tree.tree)50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 50 for i=1:numel(uid) 51 if uid(i)<1 || uid(i)>length(tree.tree) 52 error('[XMLTree] Invalid UID.'); 53 end 54 if ~strcmp(tree.tree{uid(i)}.type,'element') 55 error('[XMLTree] Cannot add a child to a non-element node.'); 56 end 57 l = length(tree.tree); 58 switch type 59 case 'element' 60 tree.tree{l+1} = struct('type','element',... 61 'name',parameter,... 62 'attributes',[],... 63 'contents',[],... 64 'parent',[],... 65 'uid',l+1); 66 case 'chardata' 67 tree.tree{l+1} = struct('type','chardata',... 68 'value',parameter,... 69 'parent',[],... 70 'uid',l+1); 71 case 'cdata' 72 tree.tree{l+1} = struct('type','cdata',... 73 'value',parameter,... 74 'parent',[],... 75 'uid',l+1); 76 case 'pi' 77 tree.tree{l+1} = struct('type','pi',... 78 'target',parameter.target,... 79 'value',parameter.value,... 80 'parent',[],... 81 'uid',l+1); 82 case 'comment' 83 tree.tree{l+1} = struct('type','comment',... 84 'value',parameter,... 85 'parent',[],... 86 'uid',l+1); 87 otherwise 88 error(sprintf('[XMLTree] %s: unknown item type.',type)); 89 end 90 tree.tree{uid(i)}.contents = [tree.tree{uid(i)}.contents l+1]; 91 tree.tree{l+1}.parent = uid(i); 90 92 end 91 93 -
trunk/src/@xmltree/attributes.m
r821 r925 13 13 % 14 14 % tree = attributes(tree,'set',uid,n,key,val) 15 % 16 % 17 % 18 % 19 %_______________________________________________________________________ 15 % attr = attributes(tree,'get',uid[,n]) 16 % tree = attributes(tree,'add',uid,key,val) 17 % tree = attributes(tree,'del',uid[,n]) 18 % l = attributes(tree,'length',uid) 19 %__________________________________________________________________________ 20 20 % 21 21 % Handle attributes of an element node. 22 22 % The tree parameter must be in input AND in output for 'set', 'add' and 23 23 % 'del' methods. 24 %_______________________________________________________________________ 25 % @(#)attributes.m Guillaume Flandin 02/04/0524 %__________________________________________________________________________ 25 % Copyright (C) 2002-2011 http://www.artefact.tk/ 26 26 27 error(nargchk(3,6,nargin)); 27 % Guillaume Flandin 28 % $Id: attributes.m 4460 2011-09-05 14:52:16Z guillaume $ 29 30 31 %error(nargchk(3,6,nargin)); 32 28 33 tree = varargin{1}; 29 if ~ischar(varargin{2}) | ...34 if ~ischar(varargin{2}) || ... 30 35 ~any(strcmp(varargin{2},{'set','get','add','del','length'})) 31 36 error('[XMLTree] Unknown method.'); 32 37 end 33 38 uid = varargin{3}; 34 if ~isa(uid,'double') | any(uid>length(tree))| any(uid<1)35 39 if ~isa(uid,'double') || any(uid>length(tree)) || any(uid<1) 40 error('[XMLTree] UID must be a positive integer scalar.'); 36 41 end 37 42 38 43 if ~strcmp(tree.tree{uid}.type,'element') 39 44 error('[XMLTree] This node has no attributes.'); 40 45 end 41 46 42 47 switch varargin{2} 43 44 45 if ~isa(varargin{4},'double')| ...46 any(varargin{4}>length(tree.tree{uid}.attributes))| ...47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 elseif ~isa(varargin{4},'double')| ...65 any(varargin{4}>length(tree.tree{uid}.attributes))| ...66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 if ~isa(varargin{4},'double')| ...97 any(varargin{4}>length(tree.tree{uid}.attributes))| ...98 99 100 101 102 103 104 105 106 107 108 109 110 111 48 case 'set' 49 %error(nargchk(6,6,nargin)); 50 if ~isa(varargin{4},'double') || ... 51 any(varargin{4}>length(tree.tree{uid}.attributes)) || ... 52 any(varargin{4}<1) 53 error('[XMLTree] Invalid attribute indice.'); 54 end 55 ind = varargin{4}; 56 tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6}); 57 varargout{1} = tree; 58 case 'get' 59 %error(nargchk(3,4,nargin)); 60 if nargin == 4 61 if ischar(varargin{4}) 62 for i=1:length(tree.tree{uid}.attributes) 63 if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key) 64 varargout{1} = tree.tree{uid}.attributes{i}.val; 65 return; 66 end 67 end 68 varargout{1} = []; 69 elseif ~isa(varargin{4},'double') || ... 70 any(varargin{4}>length(tree.tree{uid}.attributes)) || ... 71 any(varargin{4}<1) 72 error('[XMLTree] Invalid attribute indice.'); 73 else 74 if length(varargin{4}) == 1 75 varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)}; 76 else 77 varargout{1} = {}; 78 for i=1:length(varargin{4}) 79 varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)}; 80 end 81 end 82 end 83 else 84 if length(tree.tree{uid}.attributes) == 1 85 varargout{1} = tree.tree{uid}.attributes{1}; 86 else 87 varargout{1} = {}; 88 for i=1:length(tree.tree{uid}.attributes) 89 varargout{1}{i} = tree.tree{uid}.attributes{i}; 90 end 91 end 92 end 93 case 'add' 94 %error(nargchk(5,5,nargin)); 95 ind = length(tree.tree{uid}.attributes) + 1; 96 tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5}); 97 varargout{1} = tree; 98 case 'del' 99 %error(nargchk(3,4,nargin)); 100 if nargin == 4 101 if ~isa(varargin{4},'double') || ... 102 any(varargin{4}>length(tree.tree{uid}.attributes)) || ... 103 any(varargin{4}<1) 104 error('[XMLTree] Invalid attribute indice.'); 105 end 106 ind = varargin{4}; 107 tree.tree{uid}.attributes(ind) = []; 108 else 109 tree.tree{uid}.attributes = []; 110 end 111 varargout{1} = tree; 112 case 'length' 113 %error(nargchk(3,3,nargin)); 114 varargout{1} = length(tree.tree{uid}.attributes); 115 otherwise 116 error('[XMLTree] Unknown method.'); 112 117 end -
trunk/src/@xmltree/branch.m
r821 r925 6 6 % uid - UID of the root element of the subtree 7 7 % subtree - XMLTree object (a subtree from tree) 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 10 % Return a subtree from a tree. 11 %_______________________________________________________________________ 12 % @(#)branch.m Guillaume Flandin 02/04/1711 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 13 14 error(nargchk(2,2,nargin)); 14 % Guillaume Flandin 15 % $Id: branch.m 4460 2011-09-05 14:52:16Z guillaume $ 15 16 16 if uid > length(tree) | ... 17 prod(size(uid))~=1 | ... 17 18 %error(nargchk(2,2,nargin)); 19 20 if uid > length(tree) || ... 21 numel(uid)~=1 || ... 18 22 ~strcmp(tree.tree{uid}.type,'element') 19 23 error('[XMLTree] Invalid UID.'); 20 24 end 21 25 … … 28 32 29 33 for i=1:length(child) 30 31 32 34 l = length(subtree); 35 subtree = sub_branch(tree,subtree,child(i),root(subtree)); 36 subtree.tree{root(subtree)}.contents = [subtree.tree{root(subtree)}.contents l+1]; 33 37 end 34 38 35 %======================================================================= 39 %========================================================================== 36 40 function tree = sub_branch(t,tree,uid,p) 37 41 38 39 40 41 42 43 44 45 46 47 48 49 50 51 42 l = length(tree); 43 tree.tree{l+1} = t.tree{uid}; 44 tree.tree{l+1}.uid = l + 1; 45 tree.tree{l+1}.parent = p; 46 tree.tree{l+1}.contents = []; 47 if isfield(t.tree{uid},'contents') 48 contents = get(t,uid,'contents'); 49 m = length(tree); 50 for i=1:length(contents) 51 tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1]; 52 tree = sub_branch(t,tree,contents(i),l+1); 53 m = length(tree); 54 end 55 end -
trunk/src/@xmltree/char.m
r723 r925 5 5 % tree - XMLTree object 6 6 % s - a description string of an XMLTree 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Return a string describing the XMLTree: 10 10 % 'XMLTree object (x nodes) [filename]' 11 %_______________________________________________________________________ 12 % @(#)char.m Guillaume Flandin 02/04/04 11 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 14 % Guillaume Flandin 15 % $Id: char.m 4460 2011-09-05 14:52:16Z guillaume $ 16 13 17 14 18 s = strcat('XMLTree object (',num2str(length(tree)),' nodes) [',getfilename(tree),']'); -
trunk/src/@xmltree/children.m
r723 r925 6 6 % uid - uid of the element 7 7 % child - array of the UIDs of children of node uid 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 10 % Return UID's of children of node uid 11 %_______________________________________________________________________ 12 % @(#)children.m Guillaume Flandin 02/04/0911 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 13 14 error(nargchk(2,2,nargin)); 14 % Guillaume Flandin 15 % $Id: children.m 4460 2011-09-05 14:52:16Z guillaume $ 16 17 %error(nargchk(2,2,nargin)); 15 18 16 19 child = []; … … 18 21 l = length(tree); 19 22 for i=1:length(uid) 20 if uid(i) > 0& uid(i) <= l21 22 23 24 25 26 23 if uid(i) > 0 && uid(i) <= l 24 if strcmp(tree.tree{uid(i)}.type,'element') 25 child = [child tree.tree{uid(i)}.contents]; 26 end 27 else 28 error('[XMLTree] Invalid UID.'); 29 end 27 30 end 28 31 if isempty(child), child = []; end -
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 -
trunk/src/@xmltree/copy.m
r723 r925 6 6 % subuid - UID of the subtree to copy 7 7 % uid - UID of the element where the subtree must be duplicated 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 % Copy a subtree to another branch 11 % The tree parameter must be in input AND in output 12 %_______________________________________________________________________ 13 % @(#)copy.m Guillaume Flandin 02/04/0810 % Copy a subtree to another branch. 11 % The tree parameter must be in input AND in output. 12 %__________________________________________________________________________ 13 % Copyright (C) 2002-2015 http://www.artefact.tk/ 14 14 15 error(nargchk(2,3,nargin)); 15 % Guillaume Flandin 16 % $Id: copy.m 6480 2015-06-13 01:08:30Z guillaume $ 17 18 19 %error(nargchk(2,3,nargin)); 20 16 21 if nargin == 2 17 22 uid = parent(tree,subuid); 18 23 end 19 24 … … 22 27 tree.tree{uid}.contents = [tree.tree{uid}.contents l+1]; 23 28 24 % pour que la copie soit a cote de l'original et pas a la fin?29 % to have the copy next to the original and not at the end? 25 30 % contents = get(tree,parent,'contents'); 26 31 % i = find(contents==uid); 27 32 % tree = set(tree,parent,'contents',[contents(1:i) l+1 contents(i+1:end)]); 28 33 29 %======================================================================= 34 %========================================================================== 30 35 function tree = sub_copy(tree,uid,p) 31 36 32 33 34 35 36 37 38 39 40 41 42 43 44 45 37 l = length(tree); 38 tree.tree{l+1} = tree.tree{uid}; 39 tree.tree{l+1}.uid = l+1; 40 tree.tree{l+1}.parent = p; 41 tree.tree{l+1}.contents = []; 42 if isfield(tree.tree{uid},'contents') 43 contents = get(tree,uid,'contents'); 44 m = length(tree); 45 for i=1:length(contents) 46 tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1]; 47 tree = sub_copy(tree,contents(i),l+1); 48 m = length(tree); 49 end 50 end -
trunk/src/@xmltree/delete.m
r723 r925 4 4 % tree - XMLTree object 5 5 % uid - array of UID's of subtrees to be deleted 6 %_______________________________________________________________________ 6 %__________________________________________________________________________ 7 7 % 8 8 % Delete a subtree given its UID 9 9 % The tree parameter must be in input AND in output 10 %_______________________________________________________________________ 11 % @(#)delete.m Guillaume Flandin 02/04/0810 %__________________________________________________________________________ 11 % Copyright (C) 2002-2011 http://www.artefact.tk/ 12 12 13 error(nargchk(2,2,nargin)); 13 % Guillaume Flandin 14 % $Id: delete.m 4460 2011-09-05 14:52:16Z guillaume $ 15 16 %error(nargchk(2,2,nargin)); 14 17 15 18 uid = uid(:); 16 19 for i=1:length(uid) 17 18 19 20 21 22 23 20 if uid(i)==1 21 warning('[XMLTree] Cannot delete root element.'); 22 else 23 p = tree.tree{uid(i)}.parent; 24 tree = sub_delete(tree,uid(i)); 25 tree.tree{p}.contents(find(tree.tree{p}.contents==uid(i))) = []; 26 end 24 27 end 25 28 26 %======================================================================= 29 %========================================================================== 27 30 function tree = sub_delete(tree,uid) 28 29 30 31 32 33 31 if isfield(tree.tree{uid},'contents') 32 for i=1:length(tree.tree{uid}.contents) 33 tree = sub_delete(tree,tree.tree{uid}.contents(i)); 34 end 35 end 36 tree.tree{uid} = struct('type','deleted'); -
trunk/src/@xmltree/display.m
r723 r925 4 4 % 5 5 % tree - XMLTree object 6 %_______________________________________________________________________ 6 %__________________________________________________________________________ 7 7 % 8 8 % This method is called when the semicolon is not used to terminate a 9 9 % statement which returns an XMLTree. 10 %_______________________________________________________________________ 11 % @(#)display.m Guillaume Flandin 02/04/04 10 %__________________________________________________________________________ 11 % Copyright (C) 2002-2011 http://www.artefact.tk/ 12 13 % Guillaume Flandin 14 % $Id: display.m 4460 2011-09-05 14:52:16Z guillaume $ 12 15 13 16 disp(' '); 14 17 disp([inputname(1),' = ']); 15 18 disp(' '); 16 for i=1: prod(size(tree))17 19 for i=1:numel(tree) 20 disp([blanks(length(inputname(1))+3) char(tree(i))]); 18 21 end 19 22 disp(' '); -
trunk/src/@xmltree/find.m
r723 r925 16 16 % XML Path Language XPath (http://www.w3.org/TR/xpath) 17 17 % Example: /element1//element2[1]/element3[5]/element4 18 %_______________________________________________________________________ 18 %__________________________________________________________________________ 19 19 % 20 20 % Find elements in an XML tree with specified characteristics or given 21 21 % a path (using a subset of XPath language). 22 %_______________________________________________________________________ 23 % @(#)find.m Guillaume Flandin 01/10/29 22 %__________________________________________________________________________ 23 % Copyright (C) 2002-2011 http://www.artefact.tk/ 24 25 % Guillaume Flandin 26 % $Id: find.m 4460 2011-09-05 14:52:16Z guillaume $ 24 27 25 28 % TODO: … … 30 33 31 34 if nargin==0 32 35 error('[XMLTree] A tree must be provided'); 33 36 elseif nargin==1 34 35 37 list = 1:length(tree.tree); 38 return 36 39 elseif mod(nargin,2) 37 list = sub_find_subtree1(varargin{1}.tree,root(tree),varargin{2:end});38 elseif isa(varargin{2},'double') & ...39 ndims(varargin{2}) == 2& ...40 41 42 elseif nargin==2 & ischar(varargin{2})43 40 list = sub_find_subtree1(varargin{1}.tree,root(varargin{1}),varargin{2:end}); 41 elseif isa(varargin{2},'double') && ... 42 ndims(varargin{2}) == 2 && ... 43 min(size(varargin{2})) == 1 44 list = unique(sub_find_subtree1(varargin{1}.tree,varargin{2:end})); 45 elseif nargin==2 && ischar(varargin{2}) 46 list = sub_pathfinder(varargin{:}); 44 47 else 45 48 error('[XMLTree] Arguments must be parameter/value pairs.'); 46 49 end 47 50 48 %======================================================================= 51 %========================================================================== 49 52 function list = sub_find_subtree1(varargin) 50 51 52 53 54 55 53 list = []; 54 for i=1:length(varargin{2}) 55 res = sub_find_subtree2(varargin{1},... 56 varargin{2}(i),varargin{3:end}); 57 list = [list res]; 58 end 56 59 57 %======================================================================= 60 %========================================================================== 58 61 function list = sub_find_subtree2(varargin) 59 60 61 62 63 64 65 66 67 62 uid = varargin{2}; 63 list = []; 64 if sub_comp_element(varargin{1}{uid},varargin{3:end}) 65 list = [list varargin{1}{uid}.uid]; 66 end 67 if isfield(varargin{1}{uid},'contents') 68 list = [list sub_find_subtree1(varargin{1},... 69 varargin{1}{uid}.contents,varargin{3:end})]; 70 end 68 71 69 %======================================================================= 72 %========================================================================== 70 73 function match = sub_comp_element(varargin) 71 74 match = 0; 72 try ,73 74 75 76 77 78 79 80 catch ,75 try 76 % v = getfield(varargin{1}, varargin{2}); % slow... 77 for i=1:floor(nargin/2) 78 v = subsref(varargin{1}, struct('type','.','subs',varargin{i+1})); 79 if strcmp(v,varargin{i+2}) 80 match = 1; 81 end 82 end 83 catch 81 84 end 82 85 … … 85 88 %for i=1:length(floor(nargin/2)) % bug: remove length !!! 86 89 % if isfield(varargin{1},varargin{i+1}) 87 % 88 % 89 % 90 % 91 % 92 % 93 % 94 % 95 % 96 % 97 % 98 % 90 % if ischar(getfield(varargin{1},varargin{i+1})) & ischar(varargin{i+2}) 91 % if strcmp(getfield(varargin{1},varargin{i+1}),char(varargin{i+2})) 92 % match = 1; 93 % end 94 % elseif isa(getfield(varargin{1},varargin{i+1}),'double') & ... 95 % isa(varargin{i+2},'double') 96 % if getfield(varargin{1},varargin{i+1}) == varargin{i+2} 97 % match = 1; 98 % end 99 % else 100 % warning('Cannot compare different objects'); 101 % end 99 102 % end 100 103 %end 101 104 102 %======================================================================= 105 %========================================================================== 103 106 function list = sub_pathfinder(tree,pth) 104 105 i = findstr(pth,'/');106 107 108 109 110 111 112 if j<length(i)& i(j+1)==i(j)+1113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 if val < 1| val > length(list)+1151 152 153 154 155 156 157 158 159 160 161 162 %======================================================================= 107 %- Search for the delimiter '/' in the path 108 i = strfind(pth,'/'); 109 %- Begin search by root 110 list = root(tree); 111 %- Walk through the tree 112 j = 1; 113 while j <= length(i) 114 %- Look for recursion '//' 115 if j<length(i) && i(j+1)==i(j)+1 116 recursive = 1; 117 j = j + 1; 118 else 119 recursive = 0; 120 end 121 %- Catch the current tag 'element[x]' 122 if j ~= length(i) 123 element = pth(i(j)+1:i(j+1)-1); 124 else 125 element = pth(i(j)+1:end); 126 end 127 %- Search for [] brackets 128 k = xml_findstr(element,'[',1,1); 129 %- If brackets are present in current element 130 if ~isempty(k) 131 l = xml_findstr(element,']',1,1); 132 val = str2num(element(k+1:l-1)); 133 element = element(1:k-1); 134 end 135 %- Use recursivity 136 if recursive 137 list = find(tree,list,'name',element); 138 %- Just look at children 139 else 140 if i(j)==1 % if '/root/...' (list = root(tree) in that case) 141 if sub_comp_element(tree.tree{list},'name',element) 142 % list = 1; % list still contains root(tree) 143 else 144 list = []; 145 return; 146 end 147 else 148 list = sub_findchild(tree,list,element); 149 end 150 end 151 % If an element is specified using a key 152 if ~isempty(k) 153 if val < 1 || val > length(list)+1 154 error('[XMLTree] Bad key in the path.'); 155 elseif val == length(list)+1 156 list = []; 157 return; 158 end 159 list = list(val); 160 end 161 if isempty(list), return; end 162 j = j + 1; 163 end 164 165 %========================================================================== 163 166 function list = sub_findchild(tree,listt,elmt) 164 165 166 167 168 169 170 171 167 list = []; 168 for a=1:length(listt) 169 for b=1:length(tree.tree{listt(a)}.contents) 170 if sub_comp_element(tree.tree{tree.tree{listt(a)}.contents(b)},'name',elmt) 171 list = [list tree.tree{tree.tree{listt(a)}.contents(b)}.uid]; 172 end 173 end 174 end -
trunk/src/@xmltree/flush.m
r723 r925 5 5 % uid - array of UID's of subtrees to be cleared 6 6 % Default is root 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Clear a subtree given its UID (remove all the leaves of the tree) 10 10 % The tree parameter must be in input AND in output 11 %_______________________________________________________________________ 12 % @(#)flush.m Guillaume Flandin 02/04/1011 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 13 14 error(nargchk(1,2,nargin)); 14 % Guillaume Flandin 15 % $Id: flush.m 4460 2011-09-05 14:52:16Z guillaume $ 16 17 18 %error(nargchk(1,2,nargin)); 15 19 16 20 if nargin == 1, 17 21 uid = root(tree); 18 22 end 19 23 20 24 uid = uid(:); 21 25 for i=1:length(uid) 22 26 tree = sub_flush(tree,uid(i)); 23 27 end 24 28 25 %======================================================================= 29 %========================================================================== 26 30 function tree = sub_flush(tree,uid) 27 28 29 30 31 32 33 34 if strcmp(tree.tree{uid}.type,'chardata')|...35 strcmp(tree.tree{uid}.type,'pi')|...36 strcmp(tree.tree{uid}.type,'cdata')|...37 38 39 31 if isfield(tree.tree{uid},'contents') 32 % contents is parsed in reverse order because each child is 33 % deleted and the contents vector is then eventually reduced 34 for i=length(tree.tree{uid}.contents):-1:1 35 tree = sub_flush(tree,tree.tree{uid}.contents(i)); 36 end 37 end 38 if strcmp(tree.tree{uid}.type,'chardata') ||... 39 strcmp(tree.tree{uid}.type,'pi') ||... 40 strcmp(tree.tree{uid}.type,'cdata') ||... 41 strcmp(tree.tree{uid}.type,'comment') 42 tree = delete(tree,uid); 43 end -
trunk/src/@xmltree/get.m
r723 r925 7 7 % parameter - property name 8 8 % value - property value 9 %_______________________________________________________________________ 9 %__________________________________________________________________________ 10 10 % 11 11 % Get object properties of a tree given their UIDs. 12 %_______________________________________________________________________ 13 % @(#)get.m Guillaume Flandin 02/03/2712 %__________________________________________________________________________ 13 % Copyright (C) 2002-2011 http://www.artefact.tk/ 14 14 15 error(nargchk(2,3,nargin)); 15 % Guillaume Flandin 16 % $Id: get.m 4460 2011-09-05 14:52:16Z guillaume $ 17 18 19 %error(nargchk(2,3,nargin)); 16 20 17 21 value = cell(size(uid)); 18 22 uid = uid(:); 19 23 if nargin==2 20 21 if uid(i)<1| uid(i)>length(tree.tree)22 23 24 25 26 27 24 for i=1:length(uid) 25 if uid(i)<1 || uid(i)>length(tree.tree) 26 error('[XMLTree] Invalid UID.'); 27 end 28 % According to the type of the node, return only some parameters 29 % Need changes... 30 value{i} = tree.tree{uid(i)}; 31 end 28 32 else 29 30 try, 31 32 catch, 33 34 35 33 for i=1:length(uid) 34 try 35 value{i} = subsref(tree.tree{uid(i)}, struct('type','.','subs',parameter)); 36 catch 37 error(sprintf('[XMLTree] Parameter %s not found.',parameter)); 38 end 39 end 36 40 end 37 41 if length(value)==1 38 42 value = value{1}; 39 43 end -
trunk/src/@xmltree/getfilename.m
r723 r925 5 5 % tree - XMLTree object 6 6 % filename - XML filename 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Return the filename of the XML tree if loaded from disk and an empty 10 10 % string otherwise. 11 %_______________________________________________________________________ 12 % @(#)getfilename.m Guillaume Flandin 02/03/27 11 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 14 % Guillaume Flandin 15 % $Id: getfilename.m 4460 2011-09-05 14:52:16Z guillaume $ 13 16 14 17 filename = tree.filename; -
trunk/src/@xmltree/isfield.m
r723 r925 7 7 % parameter - a field of the root tree 8 8 % F - 1 if present, 0 otherwise 9 %_______________________________________________________________________ 9 %__________________________________________________________________________ 10 10 % 11 11 % Is parameter a field of tree{uid} ? 12 %_______________________________________________________________________ 13 % @(#)isfield.m Guillaume Flandin 01/10/3112 %__________________________________________________________________________ 13 % Copyright (C) 2002-2011 http://www.artefact.tk/ 14 14 15 error(nargchk(3,3,nargin)); 15 % Guillaume Flandin 16 % $Id: isfield.m 4460 2011-09-05 14:52:16Z guillaume $ 17 18 19 %error(nargchk(3,3,nargin)); 16 20 17 21 F = zeros(1,length(uid)); 18 22 for i=1:length(uid) 19 20 21 23 if isfield(tree.tree{uid(i)},parameter) 24 F(i) = 1; 25 end 22 26 end -
trunk/src/@xmltree/length.m
r723 r925 7 7 % tree (deleted nodes aren't populated) 8 8 % l - length of the XML tree (number of nodes) 9 %_______________________________________________________________________ 9 %__________________________________________________________________________ 10 10 % 11 11 % Return the number of nodes of an XMLTree object. 12 %_______________________________________________________________________ 13 % @(#)length.m Guillaume Flandin 02/03/2712 %__________________________________________________________________________ 13 % Copyright (C) 2002-2011 http://www.artefact.tk/ 14 14 15 error(nargchk(1,2,nargin)); 15 % Guillaume Flandin 16 % $Id: length.m 4460 2011-09-05 14:52:16Z guillaume $ 17 18 19 %error(nargchk(1,2,nargin)); 16 20 17 21 % Return the full number of nodes once allocated … … 20 24 % Substract the number of deleted nodes to the previous length 21 25 if nargin == 2 22 23 24 25 26 27 28 29 30 31 32 26 if strcmp(r,'real') 27 ll = 0; 28 for i=1:l 29 if ~strcmp(tree.tree{i}.type,'deleted') 30 ll = ll + 1; 31 end 32 end 33 l = ll; 34 else 35 error('[XMLTree] Bad input argument.'); 36 end 33 37 end -
trunk/src/@xmltree/move.m
r723 r925 5 5 % uida - initial position of the subtree 6 6 % uidb - parent of the final position of the subtree 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Move a subtree inside a tree from A to B. 10 % The tree parameter must be in input AND in output 11 %_______________________________________________________________________ 12 % @(#)move.m Guillaume Flandin 02/04/0810 % The tree parameter must be in input AND in output. 11 %__________________________________________________________________________ 12 % Copyright (C) 2002-2015 http://www.artefact.tk/ 13 13 14 error(nargchk(3,3,nargin)); 14 % Guillaume Flandin 15 % $Id: move.m 6480 2015-06-13 01:08:30Z guillaume $ 16 17 18 %error(nargchk(3,3,nargin)); 15 19 16 20 p = tree.tree{uida}.parent; -
trunk/src/@xmltree/parent.m
r723 r925 6 6 % uid - UID of the lonely child 7 7 % p - UID of the parent ([] if root is the child) 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 10 % Return the uid of the parent of a node. 11 %_______________________________________________________________________ 12 % @(#)parent.m Guillaume Flandin 02/04/08 11 %__________________________________________________________________________ 12 % Copyright (C) 2002-2011 http://www.artefact.tk/ 13 14 % Guillaume Flandin 15 % $Id: parent.m 4460 2011-09-05 14:52:16Z guillaume $ 13 16 14 17 p = tree.tree{uid}.parent; -
trunk/src/@xmltree/private/xml_findstr.c
r820 r925 2 2 3 3 /* 4 Differences with matlab built-in findstr: 4 * $Id: xml_findstr.c 6480 2015-06-13 01:08:30Z guillaume $ 5 * Guillaume Flandin <guillaume@artefact.tk> 6 */ 7 8 /* 9 Differences with built-in findstr: 5 10 - allows to search only the n first occurences of a pattern 6 11 - allows to search only in a substring (given an index of the beginning) 7 12 8 M atlabhack:13 MATLAB hack: 9 14 - doesn't use mxGetString to prevent a copy of the string. 10 - assumes M atlabstores strings as unsigned short (Unicode 16 bits)15 - assumes MATLAB stores strings as unsigned short (Unicode 16 bits) 11 16 matrix.h: typedef uint16_T mxChar; 12 (that's the case for M atlab 5.* and 6.* but Matlab 4.* stores strings13 as double)17 (that's the case for MATLAB 5.*, 6.* and 7.* but MATLAB 4.* stores 18 strings as double and GNU Octave as char, see src/mxarray.h) 14 19 */ 15 20 16 21 /* Comment the following line to use standard mxGetString (slower) */ 22 #if !defined (HAVE_OCTAVE) 17 23 #define __HACK_MXCHAR__ 24 #endif 18 25 19 26 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { … … 28 35 mxArray *out = NULL; 29 36 30 37 /* Check for proper number of arguments. */ 31 38 if ((nrhs == 0) || (nrhs == 1)) 32 39 mexErrMsgTxt("Not enough input arguments."); 33 40 else if (nrhs > 4) 34 41 mexErrMsgTxt("Too many input arguments."); 35 42 else if (nlhs > 1) 36 43 mexErrMsgTxt("Too many output arguments."); 37 44 38 45 /* The input TEXT must be a string */ 39 40 41 46 if (!mxIsChar(prhs[0])) 47 mexErrMsgTxt("Inputs must be character arrays."); 48 stext = mxGetM(prhs[0]) * mxGetN(prhs[0]); 42 49 #ifdef __HACK_MXCHAR__ 43 50 text = mxGetData(prhs[0]); 44 51 #else 45 52 text = mxCalloc(stext+1, sizeof(char)); … … 48 55 49 56 /* The input PATTERN must be a string */ 50 51 57 if (!mxIsChar(prhs[1])) 58 mexErrMsgTxt("Inputs must be character arrays."); 52 59 spattern = mxGetM(prhs[1]) * mxGetN(prhs[1]); 53 60 #ifdef __HACK_MXCHAR__ 54 61 pattern = mxGetData(prhs[1]); 55 62 #else 56 63 pattern = mxCalloc(spattern+1, sizeof(char)); 57 64 mxGetString(prhs[1], pattern, spattern+1); 58 65 #endif 59 66 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 67 /* The input INDEX must be an integer */ 68 if (nrhs > 2) { 69 if ((!mxIsNumeric(prhs[2]) || (mxGetM(prhs[2]) * mxGetN(prhs[2]) != 1))) 70 mexErrMsgTxt("Index input must be an integer."); 71 ind = (unsigned int)mxGetScalar(prhs[2]); 72 if (ind < 1) 73 mexErrMsgTxt("Index must be greater than 1."); 74 } 75 76 /* The input OCCUR must be an integer */ 77 if (nrhs == 4) { 78 if ((!mxIsNumeric(prhs[3]) || (mxGetM(prhs[3]) * mxGetN(prhs[3]) != 1))) 79 mexErrMsgTxt("Index input must be an integer."); 80 nboccur = (unsigned int)mxGetScalar(prhs[3]); 81 } 82 83 /* Find pattern in text */ 77 84 for (i=ind-1;i<stext;i++) { 78 85 for (j=0;j<spattern && i+j<stext;j++) { -
trunk/src/@xmltree/private/xml_findstr.m
r820 r925 20 20 % 21 21 % See also STRFIND, FINDSTR 22 %_______________________________________________________________________ 23 % Copyright (C) 2002-20 08http://www.artefact.tk/22 %__________________________________________________________________________ 23 % Copyright (C) 2002-2011 http://www.artefact.tk/ 24 24 25 % Guillaume Flandin <guillaume@artefact.tk>26 % $Id: xml_findstr.m 2271 2008-09-30 21:19:47Z guillaume $25 % Guillaume Flandin 26 % $Id: xml_findstr.m 4460 2011-09-05 14:52:16Z guillaume $ 27 27 28 28 %error(sprintf('Missing MEX-file: %s', mfilename)); -
trunk/src/@xmltree/private/xml_parser.m
r820 r925 5 5 % xmlstr - XML string to parse 6 6 % tree - tree structure corresponding to the XML file 7 %_______________________________________________________________________ 8 % 9 % xml_parser.m is an XML 1.0 (http://www.w3.org/TR/REC-xml) parser 10 % written in Matlab. It aims to be fully conforming. It is currently not11 % a validatingXML processor.7 %__________________________________________________________________________ 8 % 9 % xml_parser.m is an XML 1.0 (http://www.w3.org/TR/REC-xml) parser. 10 % It aims to be fully conforming. It is currently not a validating 11 % XML processor. 12 12 % 13 13 % A description of the tree structure provided in output is detailed in 14 14 % the header of this m-file. 15 %_______________________________________________________________________ 16 % @(#)xml_parser.m Guillaume Flandin 2002/04/04 17 18 % XML Processor for MATLAB (The Mathworks, Inc.). 19 % Copyright (C) 2002-2003 Guillaume Flandin <Guillaume@artefact.tk> 15 %__________________________________________________________________________ 16 % Copyright (C) 2002-2015 http://www.artefact.tk/ 17 18 % Guillaume Flandin 19 % $Id: xml_parser.m 6480 2015-06-13 01:08:30Z guillaume $ 20 21 % XML Processor for GNU Octave and MATLAB (The Mathworks, Inc.) 22 % Copyright (C) 2002-2015 Guillaume Flandin <Guillaume@artefact.tk> 20 23 % 21 24 % This program is free software; you can redistribute it and/or … … 32 35 % along with this program; if not, write to the Free Software 33 36 % Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA. 34 %----------------------------------------------------------------------- 37 %-------------------------------------------------------------------------- 35 38 36 39 % Suggestions for improvement and fixes are always welcome, although no … … 39 42 % Check also the latest developments on the following webpage: 40 43 % <http://www.artefact.tk/software/matlab/xml/> 41 %----------------------------------------------------------------------- 44 %-------------------------------------------------------------------------- 42 45 43 46 % The implementation of this XML parser is much inspired from a 44 % Javascript parser available at <http://www.jeremie.com/>45 46 % A mex-file xml_findstr.c is also required, to encompass some47 % limitations of the built-in findstr Matlabfunction.47 % Javascript parser that used to be available at <http://www.jeremie.com/> 48 49 % A C-MEX file xml_findstr.c is also required, to encompass some 50 % limitations of the built-in FINDSTR function. 48 51 % Compile it on your architecture using 'mex -O xml_findstr.c' command 49 52 % if the compiled version for your system is not provided. 50 % If this function behaves badly (crash or wrong results), comment the51 % line'#define __HACK_MXCHAR__' in xml_findstr.c and compile it again.52 %----------------------------------------------------------------------- 53 % If this function does not behave as expected, comment the line 54 % '#define __HACK_MXCHAR__' in xml_findstr.c and compile it again. 55 %-------------------------------------------------------------------------- 53 56 54 57 % Structure of the output tree: … … 91 94 % |_ uid: double 92 95 % 93 %----------------------------------------------------------------------- 96 %-------------------------------------------------------------------------- 94 97 95 98 % TODO/BUG/FEATURES: … … 103 106 % - xml_findstr is indeed xml_strfind according to Mathworks vocabulary 104 107 % - problem with entities: do we need to convert them here? (é) 105 %----------------------------------------------------------------------- 108 %-------------------------------------------------------------------------- 106 109 107 110 %- XML string to parse and number of tags read … … 109 112 110 113 %- Check input arguments 111 error(nargchk(1,1,nargin));114 %error(nargchk(1,1,nargin)); 112 115 if isempty(xmlstr) 113 114 elseif ~is str(xmlstr)| sum(size(xmlstr)>1)>1115 116 error('[XML] Not enough parameters.') 117 elseif ~ischar(xmlstr) || sum(size(xmlstr)>1)>1 118 error('[XML] Input must be a string.') 116 119 end 117 120 … … 137 140 clear global xmlstring Xparse_count xtree; 138 141 139 %======================================================================= 142 %========================================================================== 140 143 % SUBFUNCTIONS 141 144 142 %----------------------------------------------------------------------- 145 %-------------------------------------------------------------------------- 143 146 function frag = compile(frag) 144 global xmlstring xtree Xparse_count; 145 146 while 1, 147 if length(xmlstring)<=frag.str | ... 148 (frag.str == length(xmlstring)-1 & strcmp(xmlstring(frag.str:end),' ')) 149 return 150 end 151 TagStart = xml_findstr(xmlstring,'<',frag.str,1); 152 if isempty(TagStart) 153 %- Character data 154 error(sprintf(['[XML] Unknown data at the end of the XML file.\n' ... 155 ' Please send me your XML file at Guillaume@artefact.tk'])); 156 xtree{Xparse_count} = chardata; 157 xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:end))); 158 xtree{Xparse_count}.parent = frag.parent; 159 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 160 frag.str = ''; 161 elseif TagStart > frag.str 162 if strcmp(xmlstring(frag.str:TagStart-1),' ') 163 %- A single white space before a tag (ignore) 164 frag.str = TagStart; 165 else 166 %- Character data 167 xtree{Xparse_count} = chardata; 168 xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:TagStart-1))); 169 xtree{Xparse_count}.parent = frag.parent; 170 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 171 frag.str = TagStart; 172 end 173 else 174 if strcmp(xmlstring(frag.str+1),'?') 175 %- Processing instruction 176 frag = tag_pi(frag); 177 else 178 if length(xmlstring)-frag.str>4 & strcmp(xmlstring(frag.str+1:frag.str+3),'!--') 179 %- Comment 180 frag = tag_comment(frag); 181 else 182 if length(xmlstring)-frag.str>9 & strcmp(xmlstring(frag.str+1:frag.str+8),'![CDATA[') 183 %- Litteral data 184 frag = tag_cdata(frag); 185 else 186 %- A tag element (empty (<.../>) or not) 187 if ~isempty(frag.end) 188 endmk = ['/' frag.end '>']; 189 else 190 endmk = '/>'; 191 end 192 if strcmp(xmlstring(frag.str+1:frag.str+length(frag.end)+2),endmk) | ... 193 strcmp(strip(xmlstring(frag.str+1:frag.str+length(frag.end)+2)),endmk) 194 frag.str = frag.str + length(frag.end)+3; 195 return 196 else 197 frag = tag_element(frag); 198 end 199 end 200 end 201 end 202 end 203 end 204 205 %----------------------------------------------------------------------- 147 global xmlstring xtree Xparse_count; 148 149 while 1, 150 if length(xmlstring)<=frag.str || ... 151 (frag.str == length(xmlstring)-1 && strcmp(xmlstring(frag.str:end),' ')) 152 return 153 end 154 TagStart = xml_findstr(xmlstring,'<',frag.str,1); 155 if isempty(TagStart) 156 %- Character data 157 error('[XML] Unknown data at the end of the XML file.'); 158 Xparse_count = Xparse_count + 1; 159 xtree{Xparse_count} = chardata; 160 xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:end))); 161 xtree{Xparse_count}.parent = frag.parent; 162 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 163 frag.str = ''; 164 elseif TagStart > frag.str 165 if strcmp(xmlstring(frag.str:TagStart-1),' ') 166 %- A single white space before a tag (ignore) 167 frag.str = TagStart; 168 else 169 %- Character data 170 Xparse_count = Xparse_count + 1; 171 xtree{Xparse_count} = chardata; 172 xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:TagStart-1))); 173 xtree{Xparse_count}.parent = frag.parent; 174 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 175 frag.str = TagStart; 176 end 177 else 178 if strcmp(xmlstring(frag.str+1),'?') 179 %- Processing instruction 180 frag = tag_pi(frag); 181 else 182 if length(xmlstring)-frag.str>4 && strcmp(xmlstring(frag.str+1:frag.str+3),'!--') 183 %- Comment 184 frag = tag_comment(frag); 185 else 186 if length(xmlstring)-frag.str>9 && strcmp(xmlstring(frag.str+1:frag.str+8),'![CDATA[') 187 %- Litteral data 188 frag = tag_cdata(frag); 189 else 190 %- A tag element (empty (<.../>) or not) 191 if ~isempty(frag.end) 192 endmk = ['/' frag.end '>']; 193 else 194 endmk = '/>'; 195 end 196 if strcmp(xmlstring(frag.str+1:frag.str+length(frag.end)+2),endmk) || ... 197 strcmp(strip(xmlstring(frag.str+1:frag.str+length(frag.end)+2)),endmk) 198 frag.str = frag.str + length(frag.end)+3; 199 return 200 else 201 frag = tag_element(frag); 202 end 203 end 204 end 205 end 206 end 207 end 208 209 %-------------------------------------------------------------------------- 206 210 function frag = tag_element(frag) 207 global xmlstring xtree Xparse_count; 208 close = xml_findstr(xmlstring,'>',frag.str,1); 209 if isempty(close) 210 error('[XML] Tag < opened but not closed.'); 211 else 212 empty = strcmp(xmlstring(close-1:close),'/>'); 213 if empty 214 close = close - 1; 215 end 216 starttag = normalize(xmlstring(frag.str+1:close-1)); 217 nextspace = xml_findstr(starttag,' ',1,1); 218 attribs = ''; 219 if isempty(nextspace) 220 name = starttag; 221 else 222 name = starttag(1:nextspace-1); 223 attribs = starttag(nextspace+1:end); 224 end 225 xtree{Xparse_count} = element; 226 xtree{Xparse_count}.name = strip(name); 227 if frag.parent 228 xtree{Xparse_count}.parent = frag.parent; 229 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 230 end 231 if length(attribs) > 0 232 xtree{Xparse_count}.attributes = attribution(attribs); 233 end 234 if ~empty 235 contents = fragment; 236 contents.str = close+1; 237 contents.end = name; 238 contents.parent = Xparse_count; 239 contents = compile(contents); 240 frag.str = contents.str; 241 else 242 frag.str = close+2; 243 end 244 end 245 246 %----------------------------------------------------------------------- 211 global xmlstring xtree Xparse_count; 212 close = xml_findstr(xmlstring,'>',frag.str,1); 213 if isempty(close) 214 error('[XML] Tag < opened but not closed.'); 215 else 216 empty = strcmp(xmlstring(close-1:close),'/>'); 217 if empty 218 close = close - 1; 219 end 220 starttag = normalize(xmlstring(frag.str+1:close-1)); 221 nextspace = xml_findstr(starttag,' ',1,1); 222 attribs = ''; 223 if isempty(nextspace) 224 name = starttag; 225 else 226 name = starttag(1:nextspace-1); 227 attribs = starttag(nextspace+1:end); 228 end 229 Xparse_count = Xparse_count + 1; 230 xtree{Xparse_count} = element; 231 xtree{Xparse_count}.name = strip(name); 232 if frag.parent 233 xtree{Xparse_count}.parent = frag.parent; 234 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 235 end 236 if ~isempty(attribs) 237 xtree{Xparse_count}.attributes = attribution(attribs); 238 end 239 if ~empty 240 contents = fragment; 241 contents.str = close+1; 242 contents.end = name; 243 contents.parent = Xparse_count; 244 contents = compile(contents); 245 frag.str = contents.str; 246 else 247 frag.str = close+2; 248 end 249 end 250 251 %-------------------------------------------------------------------------- 247 252 function frag = tag_pi(frag) 248 global xmlstring xtree Xparse_count; 249 close = xml_findstr(xmlstring,'?>',frag.str,1); 250 if isempty(close) 251 warning('[XML] Tag <? opened but not closed.') 252 else 253 nextspace = xml_findstr(xmlstring,' ',frag.str,1); 254 xtree{Xparse_count} = pri; 255 if nextspace > close | nextspace == frag.str+2 256 xtree{Xparse_count}.value = erode(xmlstring(frag.str+2:close-1)); 257 else 258 xtree{Xparse_count}.value = erode(xmlstring(nextspace+1:close-1)); 259 xtree{Xparse_count}.target = erode(xmlstring(frag.str+2:nextspace)); 260 end 261 if frag.parent 262 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 263 xtree{Xparse_count}.parent = frag.parent; 264 end 265 frag.str = close+2; 266 end 267 268 %----------------------------------------------------------------------- 253 global xmlstring xtree Xparse_count; 254 close = xml_findstr(xmlstring,'?>',frag.str,1); 255 if isempty(close) 256 warning('[XML] Tag <? opened but not closed.') 257 else 258 nextspace = xml_findstr(xmlstring,' ',frag.str,1); 259 Xparse_count = Xparse_count + 1; 260 xtree{Xparse_count} = pri; 261 if nextspace > close || nextspace == frag.str+2 262 xtree{Xparse_count}.value = erode(xmlstring(frag.str+2:close-1)); 263 else 264 xtree{Xparse_count}.value = erode(xmlstring(nextspace+1:close-1)); 265 xtree{Xparse_count}.target = erode(xmlstring(frag.str+2:nextspace)); 266 end 267 if frag.parent 268 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 269 xtree{Xparse_count}.parent = frag.parent; 270 end 271 frag.str = close+2; 272 end 273 274 %-------------------------------------------------------------------------- 269 275 function frag = tag_comment(frag) 270 global xmlstring xtree Xparse_count; 271 close = xml_findstr(xmlstring,'-->',frag.str,1); 272 if isempty(close) 273 warning('[XML] Tag <!-- opened but not closed.') 274 else 275 xtree{Xparse_count} = comment; 276 xtree{Xparse_count}.value = erode(xmlstring(frag.str+4:close-1)); 277 if frag.parent 278 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 279 xtree{Xparse_count}.parent = frag.parent; 280 end 281 frag.str = close+3; 282 end 283 284 %----------------------------------------------------------------------- 276 global xmlstring xtree Xparse_count; 277 close = xml_findstr(xmlstring,'-->',frag.str,1); 278 if isempty(close) 279 warning('[XML] Tag <!-- opened but not closed.') 280 else 281 Xparse_count = Xparse_count + 1; 282 xtree{Xparse_count} = comment; 283 xtree{Xparse_count}.value = erode(xmlstring(frag.str+4:close-1)); 284 if frag.parent 285 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 286 xtree{Xparse_count}.parent = frag.parent; 287 end 288 frag.str = close+3; 289 end 290 291 %-------------------------------------------------------------------------- 285 292 function frag = tag_cdata(frag) 286 global xmlstring xtree Xparse_count; 287 close = xml_findstr(xmlstring,']]>',frag.str,1); 288 if isempty(close) 289 warning('[XML] Tag <![CDATA[ opened but not closed.') 290 else 291 xtree{Xparse_count} = cdata; 292 xtree{Xparse_count}.value = xmlstring(frag.str+9:close-1); 293 if frag.parent 294 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 295 xtree{Xparse_count}.parent = frag.parent; 296 end 297 frag.str = close+3; 298 end 299 300 %----------------------------------------------------------------------- 293 global xmlstring xtree Xparse_count; 294 close = xml_findstr(xmlstring,']]>',frag.str,1); 295 if isempty(close) 296 warning('[XML] Tag <![CDATA[ opened but not closed.') 297 else 298 Xparse_count = Xparse_count + 1; 299 xtree{Xparse_count} = cdata; 300 xtree{Xparse_count}.value = xmlstring(frag.str+9:close-1); 301 if frag.parent 302 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; 303 xtree{Xparse_count}.parent = frag.parent; 304 end 305 frag.str = close+3; 306 end 307 308 %-------------------------------------------------------------------------- 301 309 function all = attribution(str) 302 303 304 305 306 307 308 if isempty(str)| isempty(eq), return; end309 id = xml_findstr(str,'"',1,1); % should also look for '''' 310 nextid = xml_findstr(str,'"',id+1,1);% rather than only '"' 311 312 313 314 315 316 317 %----------------------------------------------------------------------- 310 %- Initialize attributs 311 nbattr = 0; 312 all = cell(nbattr); 313 %- Look for 'key="value"' substrings 314 while 1, 315 eq = xml_findstr(str,'=',1,1); 316 if isempty(str) || isempty(eq), return; end 317 id = sort([xml_findstr(str,'"',1,1),xml_findstr(str,'''',1,1)]); id=id(1); 318 nextid = sort([xml_findstr(str,'"',id+1,1),xml_findstr(str,'''',id+1,1)]);nextid=nextid(1); 319 nbattr = nbattr + 1; 320 all{nbattr}.key = strip(str(1:(eq-1))); 321 all{nbattr}.val = entity(str((id+1):(nextid-1))); 322 str = str((nextid+1):end); 323 end 324 325 %-------------------------------------------------------------------------- 318 326 function elm = element 319 global Xparse_count; 320 Xparse_count = Xparse_count + 1; 321 elm = struct('type','element','name','','attributes',[],'contents',[],'parent',[],'uid',Xparse_count); 327 global Xparse_count; 328 elm = struct('type','element','name','','attributes',[],'contents',[],'parent',[],'uid',Xparse_count); 322 329 323 %----------------------------------------------------------------------- 330 %-------------------------------------------------------------------------- 324 331 function cdat = chardata 325 global Xparse_count; 326 Xparse_count = Xparse_count + 1; 327 cdat = struct('type','chardata','value','','parent',[],'uid',Xparse_count); 332 global Xparse_count; 333 cdat = struct('type','chardata','value','','parent',[],'uid',Xparse_count); 328 334 329 %----------------------------------------------------------------------- 335 %-------------------------------------------------------------------------- 330 336 function cdat = cdata 331 global Xparse_count; 332 Xparse_count = Xparse_count + 1; 333 cdat = struct('type','cdata','value','','parent',[],'uid',Xparse_count); 337 global Xparse_count; 338 cdat = struct('type','cdata','value','','parent',[],'uid',Xparse_count); 334 339 335 %----------------------------------------------------------------------- 340 %-------------------------------------------------------------------------- 336 341 function proce = pri 337 global Xparse_count; 338 Xparse_count = Xparse_count + 1; 339 proce = struct('type','pi','value','','target','','parent',[],'uid',Xparse_count); 340 341 %----------------------------------------------------------------------- 342 global Xparse_count; 343 proce = struct('type','pi','value','','target','','parent',[],'uid',Xparse_count); 344 345 %-------------------------------------------------------------------------- 342 346 function commt = comment 343 global Xparse_count; 344 Xparse_count = Xparse_count + 1; 345 commt = struct('type','comment','value','','parent',[],'uid',Xparse_count); 346 347 %----------------------------------------------------------------------- 347 global Xparse_count; 348 commt = struct('type','comment','value','','parent',[],'uid',Xparse_count); 349 350 %-------------------------------------------------------------------------- 348 351 function frg = fragment 349 350 351 %----------------------------------------------------------------------- 352 frg = struct('str','','parent','','end',''); 353 354 %-------------------------------------------------------------------------- 352 355 function str = prolog(str) 353 354 355 356 357 358 359 360 361 if strcmp(lower(str(start:start+2)),'<?x')362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 if (~isempty(dp)& dp < b)377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 %----------------------------------------------------------------------- 356 %- Initialize beginning index of elements tree 357 b = 1; 358 %- Initial tag 359 start = xml_findstr(str,'<',1,1); 360 if isempty(start) 361 error('[XML] No tag found.') 362 end 363 %- Header (<?xml version="1.0" ... ?>) 364 if strcmpi(str(start:start+2),'<?x') 365 close = xml_findstr(str,'?>',1,1); 366 if ~isempty(close) 367 b = close + 2; 368 else 369 warning('[XML] Header tag incomplete.') 370 end 371 end 372 %- Doctype (<!DOCTYPE type ... [ declarations ]>) 373 start = xml_findstr(str,'<!DOCTYPE',b,1); % length('<!DOCTYPE') = 9 374 if ~isempty(start) 375 close = xml_findstr(str,'>',start+9,1); 376 if ~isempty(close) 377 b = close + 1; 378 dp = xml_findstr(str,'[',start+9,1); 379 if (~isempty(dp) && dp < b) 380 k = xml_findstr(str,']>',start+9,1); 381 if ~isempty(k) 382 b = k + 2; 383 else 384 warning('[XML] Tag [ in DOCTYPE opened but not closed.') 385 end 386 end 387 else 388 warning('[XML] Tag DOCTYPE opened but not closed.') 389 end 390 end 391 %- Skip prolog from the xml string 392 str = str(b:end); 393 394 %-------------------------------------------------------------------------- 392 395 function str = strip(str) 393 a = isspace(str); 394 a = find(a==1); 395 str(a) = ''; 396 397 %----------------------------------------------------------------------- 396 str(isspace(str)) = ''; 397 398 %-------------------------------------------------------------------------- 398 399 function str = normalize(str) 399 % Find white characters (space, newline, carriage return, tabs, ...) 400 i = isspace(str); 401 i = find(i == 1); 402 str(i) = ' '; 403 % replace several white characters by only one 404 if ~isempty(i) 405 j = i - [i(2:end) i(end)]; 406 k = find(j == -1); 407 str(i(k)) = []; 408 end 409 410 %----------------------------------------------------------------------- 400 % Find white characters (space, newline, carriage return, tabs, ...) 401 i = isspace(str); 402 i = find(i == 1); 403 str(i) = ' '; 404 % replace several white characters by only one 405 if ~isempty(i) 406 j = i - [i(2:end) i(end)]; 407 str(i(j == -1)) = []; 408 end 409 410 %-------------------------------------------------------------------------- 411 411 function str = entity(str) 412 413 414 415 416 412 str = strrep(str,'<','<'); 413 str = strrep(str,'>','>'); 414 str = strrep(str,'"','"'); 415 str = strrep(str,''',''''); 416 str = strrep(str,'&','&'); 417 417 418 %----------------------------------------------------------------------- 418 %-------------------------------------------------------------------------- 419 419 function str = erode(str) 420 if ~isempty(str) & str(1)==' 'str(1)=''; end;421 if ~isempty(str) & str(end)==' 'str(end)=''; end;420 if ~isempty(str) && str(1)==' ', str(1)=''; end; 421 if ~isempty(str) && str(end)==' ', str(end)=''; end; -
trunk/src/@xmltree/root.m
r723 r925 5 5 % tree - XMLTree object 6 6 % uid - UID of the root element of tree 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Return the uid of the root element of the tree. 10 %_______________________________________________________________________ 11 % @(#)root.m Guillaume Flandin 02/04/17 10 %__________________________________________________________________________ 11 % Copyright (C) 2002-2008 http://www.artefact.tk/ 12 13 % Guillaume Flandin 14 % $Id: root.m 4460 2011-09-05 14:52:16Z guillaume $ 12 15 13 16 % Actually root is necessarily the element whos UID is 1, by … … 27 30 % Look for the first element in the XML Tree 28 31 for i=1:length(tree) 29 30 31 32 32 if strcmp(get(tree,i,'type'),'element') 33 uid = i; 34 break 35 end 33 36 end -
trunk/src/@xmltree/save.m
r821 r925 6 6 % filename - XML output filename 7 7 % varargout - XML string 8 %_______________________________________________________________________ 8 %__________________________________________________________________________ 9 9 % 10 10 % Convert an XML tree into a well-formed XML string and write it into 11 11 % a file or return it as a string if no filename is provided. 12 %_______________________________________________________________________ 13 % @(#)save.m Guillaume Flandin 01/07/1112 %__________________________________________________________________________ 13 % Copyright (C) 2002-2011 http://www.artefact.tk/ 14 14 15 error(nargchk(1,2,nargin)); 15 % Guillaume Flandin 16 % $Id: save.m 4460 2011-09-05 14:52:16Z guillaume $ 17 18 19 %error(nargchk(1,2,nargin)); 16 20 17 21 prolog = '<?xml version="1.0" ?>\n'; … … 19 23 %- Return the XML tree as a string 20 24 if nargin == 1 21 22 25 varargout{1} = [sprintf(prolog) ... 26 print_subtree(tree,'',root(tree))]; 23 27 %- Output specified 24 28 else 25 26 if isstr(filename)27 28 29 30 31 elseif isnumeric(filename) & prod(size(filename)) == 132 33 34 35 36 37 38 39 if isstr(filename), fclose(fid); end40 41 42 29 %- Filename provided 30 if ischar(filename) 31 [fid, msg] = fopen(filename,'w'); 32 if fid==-1, error(msg); end 33 if isempty(tree.filename), tree.filename = filename; end 34 %- File identifier provided 35 elseif isnumeric(filename) && numel(filename) == 1 36 fid = filename; 37 prolog = ''; %- With this option, do not write any prolog 38 else 39 error('[XMLTree] Invalid argument.'); 40 end 41 fprintf(fid,prolog); 42 save_subtree(tree,fid,root(tree)); 43 if ischar(filename), fclose(fid); end 44 if nargout == 1 45 varargout{1} = print_subtree(tree,'',root(tree)); 46 end 43 47 end 44 48 45 %======================================================================= 49 %========================================================================== 46 50 function xmlstr = print_subtree(tree,xmlstr,uid,order) 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 51 if nargin < 4, order = 0; end 52 xmlstr = [xmlstr blanks(3*order)]; 53 switch tree.tree{uid}.type 54 case 'element' 55 xmlstr = sprintf('%s<%s',xmlstr,tree.tree{uid}.name); 56 for i=1:length(tree.tree{uid}.attributes) 57 xmlstr = sprintf('%s %s="%s"', xmlstr, ... 58 tree.tree{uid}.attributes{i}.key,... 59 tree.tree{uid}.attributes{i}.val); 60 end 61 if isempty(tree.tree{uid}.contents) 62 xmlstr = sprintf('%s/>\n',xmlstr); 63 else 64 xmlstr = sprintf('%s>\n',xmlstr); 65 for i=1:length(tree.tree{uid}.contents) 66 xmlstr = print_subtree(tree,xmlstr, ... 67 tree.tree{uid}.contents(i),order+1); 68 end 69 xmlstr = [xmlstr blanks(3*order)]; 70 xmlstr = sprintf('%s</%s>\n',xmlstr,... 71 tree.tree{uid}.name); 72 end 73 case 'chardata' 74 xmlstr = sprintf('%s%s\n',xmlstr, ... 75 entity(tree.tree{uid}.value)); 76 case 'cdata' 77 xmlstr = sprintf('%s<![CDATA[%s]]>\n',xmlstr, ... 78 tree.tree{uid}.value); 79 case 'pi' 80 xmlstr = sprintf('%s<?%s %s?>\n',xmlstr, ... 81 tree.tree{uid}.target, tree.tree{uid}.value); 82 case 'comment' 83 xmlstr = sprintf('%s<!-- %s -->\n',xmlstr,... 84 tree.tree{uid}.value); 85 otherwise 86 warning(sprintf('Type %s unknown: not saved', ... 87 tree.tree{uid}.type)); 88 end 85 89 86 %======================================================================= 90 %========================================================================== 87 91 function save_subtree(tree,fid,uid,order) 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 92 if nargin < 4, order = 0; end 93 fprintf(fid,blanks(3*order)); 94 switch tree.tree{uid}.type 95 case 'element' 96 fprintf(fid,'<%s',tree.tree{uid}.name); 97 for i=1:length(tree.tree{uid}.attributes) 98 fprintf(fid,' %s="%s"',... 99 tree.tree{uid}.attributes{i}.key, ... 100 tree.tree{uid}.attributes{i}.val); 101 end 102 if isempty(tree.tree{uid}.contents) 103 fprintf(fid,'/>\n'); 104 else 105 fprintf(fid,'>\n'); 106 for i=1:length(tree.tree{uid}.contents) 107 save_subtree(tree,fid,... 108 tree.tree{uid}.contents(i),order+1) 109 end 110 fprintf(fid,blanks(3*order)); 111 fprintf(fid,'</%s>\n',tree.tree{uid}.name); 112 end 113 case 'chardata' 114 fprintf(fid,'%s\n',entity(tree.tree{uid}.value)); 115 case 'cdata' 116 fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value); 117 case 'pi' 118 fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ... 119 tree.tree{uid}.value); 120 case 'comment' 121 fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value); 122 otherwise 123 warning(sprintf('[XMLTree] Type %s unknown: not saved', ... 124 tree.tree{uid}.type)); 125 end 122 126 123 127 124 %======================================================================= 128 %========================================================================== 125 129 function str = entity(str) 126 str = strrep(str,'&','&'); 127 str = strrep(str,'<','<'); 128 str = strrep(str,'>','>'); 129 str = strrep(str,'"','"'); 130 str = strrep(str,'''','''); 130 % This has the side effect of strtrim'ming the char array. 131 str = char(strrep(cellstr(str), '&', '&' )); 132 str = char(strrep(cellstr(str), '<', '<' )); 133 str = char(strrep(cellstr(str), '>', '>' )); 134 str = char(strrep(cellstr(str), '"', '"')); 135 str = char(strrep(cellstr(str), '''', ''')); -
trunk/src/@xmltree/set.m
r723 r925 7 7 % parameter - property name 8 8 % value - property value 9 %_______________________________________________________________________ 9 %__________________________________________________________________________ 10 10 % 11 11 % Set object properties given its uid and pairs parameter/value 12 12 % The tree parameter must be in input AND in output 13 %_______________________________________________________________________ 14 % @(#)set.m Guillaume Flandin 02/03/2713 %__________________________________________________________________________ 14 % Copyright (C) 2002-2011 http://www.artefact.tk/ 15 15 16 error(nargchk(4,4,nargin)); 16 % Guillaume Flandin 17 % $Id: set.m 4460 2011-09-05 14:52:16Z guillaume $ 18 19 20 %error(nargchk(4,4,nargin)); 17 21 18 22 if iscell(uid), uid = [uid{:}]; else uid = uid(:); end 19 23 20 24 for i=1:length(uid) 21 22 25 tree.tree{uid(i)} = builtin('subsasgn', tree.tree{uid(i)}, struct('type','.','subs',parameter), value); 26 %tree.tree{uid(i)} = setfield(tree.tree{uid(i)},parameter,value); 23 27 end -
trunk/src/@xmltree/setfilename.m
r723 r925 5 5 % tree - XMLTree object 6 6 % filename - XML filename 7 %_______________________________________________________________________ 7 %__________________________________________________________________________ 8 8 % 9 9 % Set the filename linked to the XML tree as filename. 10 %_______________________________________________________________________ 11 % @(#)setfilename.m Guillaume Flandin 02/03/27 10 %__________________________________________________________________________ 11 % Copyright (C) 2002-2011 http://www.artefact.tk/ 12 13 % Guillaume Flandin 14 % $Id: setfilename.m 4460 2011-09-05 14:52:16Z guillaume $ 12 15 13 16 tree.filename = filename; -
trunk/src/@xmltree/xmltree.m
r821 r925 9 9 % tree = xmltree('foo.xml'); % creates a tree from XML file 'foo.xml' 10 10 % tree = xmltree('<tag>content</tag>') % creates a tree from string 11 %_______________________________________________________________________ 11 %__________________________________________________________________________ 12 12 % 13 13 % This is the constructor of the XMLTree class. … … 16 16 % See http://www.w3.org/TR/REC-xml for details about XML 1.0. 17 17 % See http://www.w3.org/DOM/ for details about DOM platform. 18 %_______________________________________________________________________ 19 % @(#)xmltree.m Guillaume Flandin 02/03/27 18 %__________________________________________________________________________ 19 % Copyright (C) 2002-2011 http://www.artefact.tk/ 20 21 % Guillaume Flandin 22 % $Id: xmltree.m 4460 2011-09-05 14:52:16Z guillaume $ 20 23 21 24 switch(nargin) 22 case 0 23 tree.tree{1} = struct('type','element',... 24 'name','tag',... 25 'attributes',[],... 26 'contents',[],... 27 'parent',[],... 28 'uid',1); 29 tree.filename = ''; 30 tree = class(tree,'xmltree'); 31 case 1 32 if isa(varargin{1},'xmltree') 33 tree = varargin{1}; 34 elseif ischar(varargin{1}) 35 % Input argument is an XML string 36 if (exist(varargin{1}) ~= 2 & ... 37 ~isempty(xml_findstr(varargin{1},'<',1,1))) 38 tree.tree = xml_parser(varargin{1}); 39 tree.filename = ''; 40 % Input argument is an XML filename 41 else 42 fid = fopen(varargin{1},'rt'); 43 if (fid == -1) 44 error(['[XMLTree] Cannot open ' varargin{1}]); 45 end 46 xmlstr = fscanf(fid,'%c'); 47 fclose(fid); 48 tree.tree = xml_parser(xmlstr); 49 tree.filename = varargin{1}; 50 end 51 tree = class(tree,'xmltree'); 52 else 53 error('[XMLTree] Bad input argument'); 54 end 55 otherwise 56 error('[XMLTree] Too many input arguments'); 25 case 0 26 tree.tree{1} = struct('type','element',... 27 'name','tag',... 28 'attributes',[],... 29 'contents',[],... 30 'parent',[],... 31 'uid',1); 32 tree.filename = ''; 33 tree = class(tree,'xmltree'); 34 case 1 35 if isa(varargin{1},'xmltree') 36 tree = varargin{1}; 37 elseif ischar(varargin{1}) 38 % Input argument is an XML string 39 if (~exist(varargin{1},'file') && ... 40 ~isempty(xml_findstr(varargin{1},'<',1,1))) 41 tree.tree = xml_parser(varargin{1}); 42 tree.filename = ''; 43 % Input argument is an XML filename 44 else 45 fid = fopen(varargin{1},'rt'); 46 if (fid == -1) 47 error(['[XMLTree] Cannot open ' varargin{1}]); 48 end 49 xmlstr = fread(fid,'*char')'; 50 %xmlstr = fscanf(fid,'%c'); 51 fclose(fid); 52 tree.tree = xml_parser(xmlstr); 53 tree.filename = varargin{1}; 54 end 55 tree = class(tree,'xmltree'); 56 else 57 error('[XMLTree] Bad input argument'); 58 end 59 otherwise 60 error('[XMLTree] Too many input arguments'); 57 61 end
Note: See TracChangeset
for help on using the changeset viewer.