| 1 | function varargout = save(tree, filename) |
|---|
| 2 | % XMLTREE/SAVE Save an XML tree in an XML file |
|---|
| 3 | % FORMAT varargout = save(tree,filename) |
|---|
| 4 | % |
|---|
| 5 | % tree - XMLTree |
|---|
| 6 | % filename - XML output filename |
|---|
| 7 | % varargout - XML string |
|---|
| 8 | %_______________________________________________________________________ |
|---|
| 9 | % |
|---|
| 10 | % Convert an XML tree into a well-formed XML string and write it into |
|---|
| 11 | % a file or return it as a string if no filename is provided. |
|---|
| 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 | |
|---|
| 19 | %- Return the XML tree as a string |
|---|
| 20 | if nargin == 1 |
|---|
| 21 | varargout{1} = [sprintf(prolog) ... |
|---|
| 22 | print_subtree(tree,'',root(tree))]; |
|---|
| 23 | %- Output specified |
|---|
| 24 | else |
|---|
| 25 | %- Filename provided |
|---|
| 26 | if isstr(filename) |
|---|
| 27 | [fid, msg] = fopen(filename,'w'); |
|---|
| 28 | if fid==-1, error(msg); end |
|---|
| 29 | if isempty(tree.filename), tree.filename = filename; end |
|---|
| 30 | %- File identifier provided |
|---|
| 31 | elseif isnumeric(filename) & prod(size(filename)) == 1 |
|---|
| 32 | fid = filename; |
|---|
| 33 | prolog = ''; %- With this option, do not write any prolog |
|---|
| 34 | else |
|---|
| 35 | error('[XMLTree] Invalid argument.'); |
|---|
| 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 |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 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 |
|---|
| 85 | |
|---|
| 86 | %======================================================================= |
|---|
| 87 | function save_subtree(tree,fid,uid,order) |
|---|
| 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"',... |
|---|
| 95 | tree.tree{uid}.attributes{i}.key, ... |
|---|
| 96 | tree.tree{uid}.attributes{i}.val); |
|---|
| 97 | end |
|---|
| 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 |
|---|
| 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' |
|---|
| 114 | fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ... |
|---|
| 115 | tree.tree{uid}.value); |
|---|
| 116 | case 'comment' |
|---|
| 117 | fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value); |
|---|
| 118 | otherwise |
|---|
| 119 | warning(sprintf('[XMLTree] Type %s unknown: not saved', ... |
|---|
| 120 | tree.tree{uid}.type)); |
|---|
| 121 | end |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | %======================================================================= |
|---|
| 125 | function str = entity(str) |
|---|
| 126 | str = strrep(str,'&','&'); |
|---|
| 127 | str = strrep(str,'<','<'); |
|---|
| 128 | str = strrep(str,'>','>'); |
|---|
| 129 | str = strrep(str,'"','"'); |
|---|
| 130 | str = strrep(str,'''','''); |
|---|