-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsave.m
133 lines (125 loc) · 4.85 KB
/
save.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
85
86
87
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
122
123
124
125
126
127
128
129
130
131
132
133
function varargout = save(tree, filename)
% XMLTREE/SAVE Save an XML tree in an XML file
% FORMAT varargout = save(tree,filename)
%
% tree - XMLTree
% filename - XML output filename
% varargout - XML string
%_______________________________________________________________________
%
% Convert an XML tree into a well-formed XML string and write it into
% a file or return it as a string if no filename is provided.
%_______________________________________________________________________
% Copyright (C) 2002-2008 http://www.artefact.tk/
% Guillaume Flandin <guillaume@artefact.tk>
% $Id: save.m 3756 2010-03-05 18:43:37Z guillaume $
error(nargchk(1,2,nargin));
prolog = '<?xml version="1.0" ?>\n';
%- Return the XML tree as a string
if nargin == 1
varargout{1} = [sprintf(prolog) ...
print_subtree(tree,'',root(tree))];
%- Output specified
else
%- Filename provided
if ischar(filename)
[fid, msg] = fopen(filename,'w');
if fid==-1, error(msg); end
if isempty(tree.filename), tree.filename = filename; end
%- File identifier provided
elseif isnumeric(filename) && numel(filename) == 1
fid = filename;
prolog = ''; %- With this option, do not write any prolog
else
error('[XMLTree] Invalid argument.');
end
fprintf(fid,prolog);
save_subtree(tree,fid,root(tree));
if ischar(filename), fclose(fid); end
if nargout == 1
varargout{1} = print_subtree(tree,'',root(tree));
end
end
%=======================================================================
function xmlstr = print_subtree(tree,xmlstr,uid,order)
if nargin < 4, order = 0; end
xmlstr = [xmlstr blanks(3*order)];
switch tree.tree{uid}.type
case 'element'
xmlstr = sprintf('%s<%s',xmlstr,tree.tree{uid}.name);
for i=1:length(tree.tree{uid}.attributes)
xmlstr = sprintf('%s %s="%s"', xmlstr, ...
tree.tree{uid}.attributes{i}.key,...
tree.tree{uid}.attributes{i}.val);
end
if isempty(tree.tree{uid}.contents)
xmlstr = sprintf('%s/>\n',xmlstr);
else
xmlstr = sprintf('%s>\n',xmlstr);
for i=1:length(tree.tree{uid}.contents)
xmlstr = print_subtree(tree,xmlstr, ...
tree.tree{uid}.contents(i),order+1);
end
xmlstr = [xmlstr blanks(3*order)];
xmlstr = sprintf('%s</%s>\n',xmlstr,...
tree.tree{uid}.name);
end
case 'chardata'
xmlstr = sprintf('%s%s\n',xmlstr, ...
entity(tree.tree{uid}.value));
case 'cdata'
xmlstr = sprintf('%s<![CDATA[%s]]>\n',xmlstr, ...
tree.tree{uid}.value);
case 'pi'
xmlstr = sprintf('%s<?%s %s?>\n',xmlstr, ...
tree.tree{uid}.target, tree.tree{uid}.value);
case 'comment'
xmlstr = sprintf('%s<!-- %s -->\n',xmlstr,...
tree.tree{uid}.value);
otherwise
warning(sprintf('Type %s unknown: not saved', ...
tree.tree{uid}.type));
end
%=======================================================================
function save_subtree(tree,fid,uid,order)
if nargin < 4, order = 0; end
fprintf(fid,blanks(3*order));
switch tree.tree{uid}.type
case 'element'
fprintf(fid,'<%s',tree.tree{uid}.name);
for i=1:length(tree.tree{uid}.attributes)
fprintf(fid,' %s="%s"',...
tree.tree{uid}.attributes{i}.key, ...
tree.tree{uid}.attributes{i}.val);
end
if isempty(tree.tree{uid}.contents)
fprintf(fid,'/>\n');
else
fprintf(fid,'>\n');
for i=1:length(tree.tree{uid}.contents)
save_subtree(tree,fid,...
tree.tree{uid}.contents(i),order+1)
end
fprintf(fid,blanks(3*order));
fprintf(fid,'</%s>\n',tree.tree{uid}.name);
end
case 'chardata'
fprintf(fid,'%s\n',entity(tree.tree{uid}.value));
case 'cdata'
fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
case 'pi'
fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ...
tree.tree{uid}.value);
case 'comment'
fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value);
otherwise
warning(sprintf('[XMLTree] Type %s unknown: not saved', ...
tree.tree{uid}.type));
end
%=======================================================================
function str = entity(str)
str = strrep(str,'&','&');
str = strrep(str,'<','<');
str = strrep(str,'>','>');
str = strrep(str,'"','"');
str = strrep(str,'''',''');