[821] | 1 | function varargout = save(tree, filename) |
---|
[723] | 2 | % XMLTREE/SAVE Save an XML tree in an XML file |
---|
[821] | 3 | % FORMAT varargout = save(tree,filename) |
---|
[723] | 4 | % |
---|
[821] | 5 | % tree - XMLTree |
---|
| 6 | % filename - XML output filename |
---|
| 7 | % varargout - XML string |
---|
[723] | 8 | %_______________________________________________________________________ |
---|
| 9 | % |
---|
| 10 | % Convert an XML tree into a well-formed XML string and write it into |
---|
[821] | 11 | % a file or return it as a string if no filename is provided. |
---|
[723] | 12 | %_______________________________________________________________________ |
---|
| 13 | % @(#)save.m Guillaume Flandin 01/07/11 |
---|
| 14 | |
---|
| 15 | error(nargchk(1,2,nargin)); |
---|
| 16 | |
---|
| 17 | prolog = '<?xml version="1.0" ?>\n'; |
---|
| 18 | |
---|
[821] | 19 | %- Return the XML tree as a string |
---|
| 20 | if nargin == 1 |
---|
| 21 | varargout{1} = [sprintf(prolog) ... |
---|
| 22 | print_subtree(tree,'',root(tree))]; |
---|
[723] | 23 | %- Output specified |
---|
[821] | 24 | else |
---|
[723] | 25 | %- Filename provided |
---|
[821] | 26 | if isstr(filename) |
---|
[723] | 27 | [fid, msg] = fopen(filename,'w'); |
---|
[821] | 28 | if fid==-1, error(msg); end |
---|
[723] | 29 | if isempty(tree.filename), tree.filename = filename; end |
---|
| 30 | %- File identifier provided |
---|
| 31 | elseif isnumeric(filename) & prod(size(filename)) == 1 |
---|
| 32 | fid = filename; |
---|
[821] | 33 | prolog = ''; %- With this option, do not write any prolog |
---|
[723] | 34 | else |
---|
| 35 | error('[XMLTree] Invalid argument.'); |
---|
| 36 | end |
---|
[821] | 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 |
---|
[723] | 43 | end |
---|
| 44 | |
---|
[821] | 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 |
---|
[723] | 85 | |
---|
| 86 | %======================================================================= |
---|
[821] | 87 | function save_subtree(tree,fid,uid,order) |
---|
[723] | 88 | if nargin < 4, order = 0; end |
---|
| 89 | fprintf(fid,blanks(3*order)); |
---|
| 90 | switch tree.tree{uid}.type |
---|
| 91 | case 'element' |
---|
| 92 | fprintf(fid,'<%s',tree.tree{uid}.name); |
---|
| 93 | for i=1:length(tree.tree{uid}.attributes) |
---|
| 94 | fprintf(fid,' %s="%s"',... |
---|
[821] | 95 | tree.tree{uid}.attributes{i}.key, ... |
---|
[723] | 96 | tree.tree{uid}.attributes{i}.val); |
---|
| 97 | end |
---|
[821] | 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 |
---|
[723] | 109 | case 'chardata' |
---|
| 110 | fprintf(fid,'%s\n',entity(tree.tree{uid}.value)); |
---|
| 111 | case 'cdata' |
---|
| 112 | fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value); |
---|
| 113 | case 'pi' |
---|
[821] | 114 | fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ... |
---|
| 115 | tree.tree{uid}.value); |
---|
[723] | 116 | case 'comment' |
---|
| 117 | fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value); |
---|
| 118 | otherwise |
---|
[821] | 119 | warning(sprintf('[XMLTree] Type %s unknown: not saved', ... |
---|
| 120 | tree.tree{uid}.type)); |
---|
[723] | 121 | end |
---|
[821] | 122 | |
---|
| 123 | |
---|
[723] | 124 | %======================================================================= |
---|
| 125 | function str = entity(str) |
---|
| 126 | str = strrep(str,'&','&'); |
---|
| 127 | str = strrep(str,'<','<'); |
---|
| 128 | str = strrep(str,'>','>'); |
---|
| 129 | str = strrep(str,'"','"'); |
---|
[821] | 130 | str = strrep(str,'''','''); |
---|