Changeset 925 for trunk/src/@xmltree/save.m
- Timestamp:
- Feb 17, 2016, 12:52:48 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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), '''', '''));
Note: See TracChangeset
for help on using the changeset viewer.