1
## Copyright (C) 2004 Laurent Mazet
3
## This program is free software; you can redistribute it and/or modify
4
## it under the terms of the GNU General Public License as published by
5
## the Free Software Foundation; either version 2 of the License, or
6
## (at your option) any later version.
8
## This program is distributed in the hope that it will be useful,
9
## but WITHOUT ANY WARRANTY; without even the implied warranty of
10
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
## GNU General Public License for more details.
13
## You should have received a copy of the GNU General Public License
14
## along with this program; If not, see <http://www.gnu.org/licenses/>.
17
## @deftypefn {Function File} {@var{nb} =} xmlwrite (@var{filename}, @var{value})
18
## @deftypefnx {Function File} {@var{nb} =} xmlwrite (@var{fd}, @var{value}, [@var{name}])
20
## Write a @var{value} into @var{filename} (@var{fd}) as a XML file.
22
##The number of elements (@var{nb}) or 0 is returned.
28
function nb = xmlwrite (filename, value, name)
29
persistent indent = "";
30
persistent separator = "\n";
32
## Check argument number
34
if (nargin < 2) || (nargin > 3)
35
usage("xmlwrite (filename, value, [name])");
39
## Get the file identificator
44
sn = split(filename, ".");
45
if !strcmp(tolower(deblank(sn(end,:))), "xml")
46
filename = [filename, ".xml"];
50
fd = fopen (filename, "w");
52
error("error opening file \"%s\"\n", filename);
56
fprintf (fd, "<?xml version=\"1.0\"?>\n");
57
fprintf (fd, "<!DOCTYPE octave SYSTEM \"octave.dtd\">\n");
58
fprintf (fd, "<octave>\n");
65
## Store name in optional argument
68
opt = sprintf(" name=\"%s\"", name);
73
if ischar(value) && (rows(value) <= 1)
76
fprintf (fd, "%s<string%s length=\"%d\">%s</string>%s",
77
indent, opt, length(value), value, separator);
82
fprintf (fd, "%s<array%s rows=\"%d\">\n", indent, opt, rows(value));
83
_indent = indent; indent = [indent, " "];
85
nb += xmlwrite (fd, deblank(value(k, :)));
88
fprintf (fd, "%s</array>\n", indent);
90
elseif isscalar(value)
96
fprintf (fd, "%s<complex%s>", indent, opt);
97
_indent = indent; indent = ""; _separator = separator; separator = "";
98
nb += xmlwrite (fd, real(value));
99
nb += xmlwrite (fd, imag(value));
100
indent = _indent; separator = _separator;
101
fprintf (fd, "</complex>%s", separator);
107
fprintf (fd, "%s<scalar%s value=\"true\"/>%s", indent, opt, separator);
109
fprintf (fd, "%s<scalar%s value=\"false\"/>%s", indent, opt, separator);
116
fprintf (fd, "%s<scalar%s value=\"inf\"/>%s",
117
indent, opt, separator);
119
fprintf (fd, "%s<scalar%s value=\"neginf\"/>%s",
120
indent, opt, separator);
126
fprintf (fd, "%s<scalar%s value=\"nan\"/>%s", indent, opt, separator);
131
fprintf (fd, "%s<scalar%s value=\"na\"/>%s", indent, opt, separator);
134
sc = sprintf(sprintf("%%.%dg", save_precision), value);
135
fprintf (fd, "%s<scalar%s>%s</scalar>%s", indent, opt, sc, \
139
elseif ismatrix(value) && isnumeric(value) && (length(size(value)) <= 2)
142
fprintf (fd, "%s<matrix%s rows=\"%d\" columns=\"%d\">\n",
143
indent, opt, rows(value), columns(value));
144
_indent = indent; indent = ""; separator = "";
146
fprintf (fd, "%s ", _indent);
147
for l=1:columns(value)-1,
148
nb += xmlwrite (fd, value(k, l));
151
nb += xmlwrite (fd, value(k, end));
154
indent = _indent; separator = "\n";
155
fprintf (fd, "%s</matrix>\n", indent);
157
elseif isstruct(value)
160
st = fieldnames(value);
161
fprintf (fd, "%s<structure%s>\n", indent, opt);
162
_indent = indent; indent = [indent, " "];
164
eval(sprintf("nb += xmlwrite (fd, value.%s, \"%s\");", st{k}, st{k}));
167
fprintf (fd, "%s</structure>\n", indent);
172
fprintf (fd, "%s<cell%s rows=\"%d\" columns=\"%d\">\n",
173
indent, opt, rows(value), columns(value));
174
_indent = indent; indent = [indent, " "];
176
for l=1:columns(value),
177
nb += xmlwrite (fd, value{k, l});
181
fprintf (fd, "%s</cell>\n", indent);
186
fprintf (fd, "%s<list%s length=\"%d\">\n", indent, opt, length(value));
187
_indent = indent; indent = [indent, " "];
188
for k=1:length(value),
189
nb += xmlwrite (fd, value{k});
192
fprintf (fd, "%s</list>\n", indent);
196
error("unknown type\n");
201
fprintf (fd, "</octave>\n");