- Timestamp:
- Oct 8, 2014, 9:10:00 PM (10 years ago)
- Location:
- trunk/src/@xmltree
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/@xmltree/Contents.m
r723 r821 1 1 % XMLTree: XML Toolbox for Matlab. 2 % Version 1. 0 12-Apr-20022 % Version 1.2 17-Nov-2004 3 3 % 4 4 % XML file I/O. … … 27 27 % 28 28 % Graphical user interface methods (work in progress). 29 % editor - Reimplementation of <view> for Matlab 6+ 29 30 % view - Graphical display of a tree. 30 31 % view_ui - Useful function for view method. … … 38 39 % xml_findstr - Find one string within another (mexfile) 39 40 % 41 % Conversions Matlab <=> XML 42 % loadxml - 43 % savexml - 44 % mat2xml - 45 % xml2mat - 46 % struct2xml - 47 % 40 48 % Demos. 41 49 % xmldemo1 - Create an XML tree from scratch and save it. … … 43 51 % xmldemo3 - Read an XML file, modify some fields and save it. 44 52 45 % Copyright 2002 Guillaume Flandin - INRIA Sophia Antipolis46 % $Revision: 1. 0$53 % Copyright 2002-2004 Guillaume Flandin <Guillaume@artefact.tk> 54 % $Revision: 1.2 $ -
trunk/src/@xmltree/attributes.m
r723 r821 54 54 error(nargchk(3,4,nargin)); 55 55 if nargin == 4 56 if ~isa(varargin{4},'double') | ... 56 if ischar(varargin{4}) 57 for i=1:length(tree.tree{uid}.attributes) 58 if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key) 59 varargout{1} = tree.tree{uid}.attributes{i}.val; 60 return; 61 end 62 end 63 varargout{1} = []; 64 elseif ~isa(varargin{4},'double') | ... 57 65 any(varargin{4}>length(tree.tree{uid}.attributes)) | ... 58 66 any(varargin{4}<1) 59 67 error('[XMLTree] Invalid attribute indice.'); 60 end61 if length(varargin{4}) == 162 varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};63 68 else 64 varargout{1} = {}; 65 for i=1:length(varargin{4}) 66 varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)}; 69 if length(varargin{4}) == 1 70 varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)}; 71 else 72 varargout{1} = {}; 73 for i=1:length(varargin{4}) 74 varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)}; 75 end 67 76 end 68 77 end -
trunk/src/@xmltree/branch.m
r723 r821 22 22 subtree = xmltree; 23 23 subtree = set(subtree,root(subtree),'name',tree.tree{uid}.name); 24 %- fix by Piotr Dollar to copy attributes for the root node: 25 subtree = set(subtree,root(subtree),'attributes',tree.tree{uid}.attributes); 24 26 25 27 child = children(tree,uid); -
trunk/src/@xmltree/convert.m
r805 r821 75 75 s = sub_setfield(s,arg{:},''); 76 76 end 77 %- saving attributes : does not work with <a t='q'>b</a> 78 %- but ok with <a t='q'><c>b</c></a> 79 % attrb = attributes(tree,'get',uid); %- 80 % if ~isempty(attrb) %- 81 % arg2 = {arg{:} 'attributes'}; %- 82 % s = sub_setfield(s,arg2{:},attrb); %- 83 % end %- 77 84 case 'chardata' 78 85 s = sub_setfield(s,arg{:},get(tree,uid,'value')); 86 %- convert strings into their Matlab equivalent when possible 87 %- e.g. string '3.14159' becomes double scalar 3.14159 88 % v = get(tree,uid,'value'); %- 89 % cv = str2num(v); %- 90 % if isempty(cv) %- 91 % s = sub_setfield(s,arg{:},v); %- 92 % else %- 93 % s = sub_setfield(s,arg{:},cv); %- 94 % end %- 79 95 case 'cdata' 80 96 s = sub_setfield(s,arg{:},get(tree,uid,'value')); … … 95 111 s = sub_setfield(s,arg{:},feval(app,get(tree,uid,'value'))); 96 112 catch, 97 warning('[X mltree/convert] Unknown target application');113 warning('[XMLTREE] Unknown target application'); 98 114 end 99 115 end … … 115 131 if (isa(varargin{i}, 'cell')) 116 132 types{i} = '{}'; 117 elseif is char(varargin{i})133 elseif isstr(varargin{i}) 118 134 types{i} = '.'; 119 135 subs{i} = varargin{i}; %strrep(varargin{i},' ',''); % deblank field name -
trunk/src/@xmltree/save.m
r805 r821 1 function save(tree, filename)1 function varargout = save(tree, filename) 2 2 % XMLTREE/SAVE Save an XML tree in an XML file 3 % FORMAT save(tree,filename)3 % FORMAT varargout = save(tree,filename) 4 4 % 5 % tree - XMLTree 6 % filename - XML output filename 5 % tree - XMLTree 6 % filename - XML output filename 7 % varargout - XML string 7 8 %_______________________________________________________________________ 8 9 % 9 10 % Convert an XML tree into a well-formed XML string and write it into 10 % a file or display it (send it to standard output) if no filename is 11 % given. 12 % 13 %----------------------------------------------------------------------- 14 % SUBFUNCTIONS: 15 % 16 % FORMAT print_subtree(tree,fid,order) 17 % Send tree entity XML formatted string to fid using 'order' blanks 18 % tree - XML tree 19 % fid - file identifier 20 % uid - uid of the current element 21 % order - order of recursivity 22 % 23 % FORMAT str = entity(str) 24 % Tool to replace input string in internal entities 25 % str - string to modify 11 % a file or return it as a string if no filename is provided. 26 12 %_______________________________________________________________________ 27 13 % @(#)save.m Guillaume Flandin 01/07/11 … … 31 17 prolog = '<?xml version="1.0" ?>\n'; 32 18 33 %- Standard output 34 if nargin==1 35 fid = 1; 19 %- Return the XML tree as a string 20 if nargin == 1 21 varargout{1} = [sprintf(prolog) ... 22 print_subtree(tree,'',root(tree))]; 36 23 %- Output specified 37 else if nargin==224 else 38 25 %- Filename provided 39 if is char(filename)26 if isstr(filename) 40 27 [fid, msg] = fopen(filename,'w'); 41 if fid==-1, error(msg); end28 if fid==-1, error(msg); end 42 29 if isempty(tree.filename), tree.filename = filename; end 43 30 %- File identifier provided 44 31 elseif isnumeric(filename) & prod(size(filename)) == 1 45 32 fid = filename; 46 prolog = ''; %- In this option, do not write any prolog 47 %- Rubbish provided 33 prolog = ''; %- With this option, do not write any prolog 48 34 else 49 35 error('[XMLTree] Invalid argument.'); 50 36 end 37 fprintf(fid,prolog); 38 save_subtree(tree,fid,root(tree)); 39 if isstr(filename), fclose(fid); end 40 if nargout == 1 41 varargout{1} = print_subtree(tree,'',root(tree)); 42 end 51 43 end 52 44 53 fprintf(fid,prolog); 54 print_subtree(tree,fid); 55 56 if nargin==2 && ischar(filename), fclose(fid); end 57 58 if nargout==1, varargout{1} = tree; end 45 %======================================================================= 46 function xmlstr = print_subtree(tree,xmlstr,uid,order) 47 if nargin < 4, order = 0; end 48 xmlstr = [xmlstr blanks(3*order)]; 49 switch tree.tree{uid}.type 50 case 'element' 51 xmlstr = sprintf('%s<%s',xmlstr,tree.tree{uid}.name); 52 for i=1:length(tree.tree{uid}.attributes) 53 xmlstr = sprintf('%s %s="%s"', xmlstr, ... 54 tree.tree{uid}.attributes{i}.key,... 55 tree.tree{uid}.attributes{i}.val); 56 end 57 if isempty(tree.tree{uid}.contents) 58 xmlstr = sprintf('%s/>\n',xmlstr); 59 else 60 xmlstr = sprintf('%s>\n',xmlstr); 61 for i=1:length(tree.tree{uid}.contents) 62 xmlstr = print_subtree(tree,xmlstr, ... 63 tree.tree{uid}.contents(i),order+1); 64 end 65 xmlstr = [xmlstr blanks(3*order)]; 66 xmlstr = sprintf('%s</%s>\n',xmlstr,... 67 tree.tree{uid}.name); 68 end 69 case 'chardata' 70 xmlstr = sprintf('%s%s\n',xmlstr, ... 71 entity(tree.tree{uid}.value)); 72 case 'cdata' 73 xmlstr = sprintf('%s<![CDATA[%s]]>\n',xmlstr, ... 74 tree.tree{uid}.value); 75 case 'pi' 76 xmlstr = sprintf('%s<?%s %s?>\n',xmlstr, ... 77 tree.tree{uid}.target, tree.tree{uid}.value); 78 case 'comment' 79 xmlstr = sprintf('%s<!-- %s -->\n',xmlstr,... 80 tree.tree{uid}.value); 81 otherwise 82 warning(sprintf('Type %s unknown: not saved', ... 83 tree.tree{uid}.type)); 84 end 59 85 60 86 %======================================================================= 61 function print_subtree(tree,fid,uid,order) 62 if nargin <3, uid = root(tree); end 87 function save_subtree(tree,fid,uid,order) 63 88 if nargin < 4, order = 0; end 64 89 fprintf(fid,blanks(3*order)); … … 68 93 for i=1:length(tree.tree{uid}.attributes) 69 94 fprintf(fid,' %s="%s"',... 70 tree.tree{uid}.attributes{i}.key, ...95 tree.tree{uid}.attributes{i}.key, ... 71 96 tree.tree{uid}.attributes{i}.val); 72 97 end 73 fprintf(fid,'>\n'); 74 for i=1:length(tree.tree{uid}.contents) 75 print_subtree(tree,fid,tree.tree{uid}.contents(i),order+1) 76 end 77 for i=1:order, fprintf(fid,' '); end 78 fprintf(fid,'</%s>\n',tree.tree{uid}.name); 98 if isempty(tree.tree{uid}.contents) 99 fprintf(fid,'/>\n'); 100 else 101 fprintf(fid,'>\n'); 102 for i=1:length(tree.tree{uid}.contents) 103 save_subtree(tree,fid,... 104 tree.tree{uid}.contents(i),order+1) 105 end 106 fprintf(fid,blanks(3*order)); 107 fprintf(fid,'</%s>\n',tree.tree{uid}.name); 108 end 79 109 case 'chardata' 80 110 fprintf(fid,'%s\n',entity(tree.tree{uid}.value)); … … 82 112 fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value); 83 113 case 'pi' 84 fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target,tree.tree{uid}.value); 114 fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ... 115 tree.tree{uid}.value); 85 116 case 'comment' 86 117 fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value); 87 118 otherwise 88 warning(sprintf('Type %s unknown : not saved',tree.tree{uid}.type)); 119 warning(sprintf('[XMLTree] Type %s unknown: not saved', ... 120 tree.tree{uid}.type)); 89 121 end 90 122 123 91 124 %======================================================================= 92 125 function str = entity(str) … … 95 128 str = strrep(str,'>','>'); 96 129 str = strrep(str,'"','"'); 97 str = strrep(str,' \',''');130 str = strrep(str,'''','''); -
trunk/src/@xmltree/xmltree.m
r723 r821 3 3 % FORMAT tree = xmltree(varargin) 4 4 % 5 % filename - XML filename5 % varargin - XML filename or XML string 6 6 % tree - XMLTree Object 7 7 % 8 % tree = xmltree; % creates a minimal XML tree: <tag/> 9 % tree = xmltree(filename); % creates a tree from an XML file 8 % tree = xmltree; % creates a minimal XML tree: '<tag/>' 9 % tree = xmltree('foo.xml'); % creates a tree from XML file 'foo.xml' 10 % tree = xmltree('<tag>content</tag>') % creates a tree from string 10 11 %_______________________________________________________________________ 11 12 % … … 32 33 tree = varargin{1}; 33 34 elseif ischar(varargin{1}) 34 tree.tree = xml_parser(varargin{1}); 35 tree.filename = 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 36 51 tree = class(tree,'xmltree'); 37 52 else … … 39 54 end 40 55 otherwise 41 error('[XMLTree] Bad number ofarguments');56 error('[XMLTree] Too many input arguments'); 42 57 end
Note: See TracChangeset
for help on using the changeset viewer.