1 ## Copyright (C) 2004 Laurent Mazet <mazet@crm.mot.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## 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 an XML file.
22 ##The number of elements (@var{nb}) or 0 is returned.
25 function nb = xmlwrite (filename, value, name)
26 persistent indent = "";
27 persistent separator = "\n";
29 ## Check argument number
31 if (nargin < 2) || (nargin > 3)
35 ## Get the file identificator
40 sn = char (strsplit (filename, "."));
41 if !strcmp(tolower(deblank(sn(end,:))), "xml")
42 filename = [filename, ".xml"];
46 fd = fopen (filename, "w");
48 error("xmlwrite: error opening file \"%s\"\n", filename);
52 fprintf (fd, "<?xml version=\"1.0\"?>\n");
53 fprintf (fd, "<!DOCTYPE octave SYSTEM \"octave.dtd\">\n");
54 fprintf (fd, "<octave>\n");
61 ## Store name in optional argument
64 opt = sprintf(" name=\"%s\"", name);
69 if ischar(value) && (rows(value) <= 1)
72 fprintf (fd, "%s<string%s length=\"%d\">%s</string>%s",
73 indent, opt, length(value), value, separator);
78 fprintf (fd, "%s<array%s rows=\"%d\">\n", indent, opt, rows(value));
79 _indent = indent; indent = [indent, " "];
81 nb += xmlwrite (fd, deblank(value(k, :)));
84 fprintf (fd, "%s</array>\n", indent);
86 elseif isscalar(value)
92 fprintf (fd, "%s<complex%s>", indent, opt);
93 _indent = indent; indent = ""; _separator = separator; separator = "";
94 nb += xmlwrite (fd, real(value));
95 nb += xmlwrite (fd, imag(value));
96 indent = _indent; separator = _separator;
97 fprintf (fd, "</complex>%s", separator);
103 fprintf (fd, "%s<scalar%s value=\"true\"/>%s", indent, opt, separator);
105 fprintf (fd, "%s<scalar%s value=\"false\"/>%s", indent, opt, separator);
112 fprintf (fd, "%s<scalar%s value=\"inf\"/>%s",
113 indent, opt, separator);
115 fprintf (fd, "%s<scalar%s value=\"neginf\"/>%s",
116 indent, opt, separator);
122 fprintf (fd, "%s<scalar%s value=\"nan\"/>%s", indent, opt, separator);
127 fprintf (fd, "%s<scalar%s value=\"na\"/>%s", indent, opt, separator);
130 sc = sprintf(sprintf("%%.%dg", save_precision), value);
131 fprintf (fd, "%s<scalar%s>%s</scalar>%s", indent, opt, sc, ...
135 elseif ismatrix(value) && isnumeric(value) && (length(size(value)) <= 2)
138 fprintf (fd, "%s<matrix%s rows=\"%d\" columns=\"%d\">\n",
139 indent, opt, rows(value), columns(value));
140 _indent = indent; indent = ""; separator = "";
142 fprintf (fd, "%s ", _indent);
143 for l=1:columns(value)-1,
144 nb += xmlwrite (fd, value(k, l));
147 nb += xmlwrite (fd, value(k, end));
150 indent = _indent; separator = "\n";
151 fprintf (fd, "%s</matrix>\n", indent);
153 elseif isstruct(value)
156 st = fieldnames(value);
157 fprintf (fd, "%s<structure%s>\n", indent, opt);
158 _indent = indent; indent = [indent, " "];
160 eval(sprintf("nb += xmlwrite (fd, value.%s, \"%s\");", st{k}, st{k}));
163 fprintf (fd, "%s</structure>\n", indent);
168 fprintf (fd, "%s<cell%s rows=\"%d\" columns=\"%d\">\n",
169 indent, opt, rows(value), columns(value));
170 _indent = indent; indent = [indent, " "];
172 for l=1:columns(value),
173 nb += xmlwrite (fd, value{k, l});
177 fprintf (fd, "%s</cell>\n", indent);
182 fprintf (fd, "%s<list%s length=\"%d\">\n", indent, opt, length(value));
183 _indent = indent; indent = [indent, " "];
184 for k=1:length(value),
185 nb += xmlwrite (fd, value{k});
188 fprintf (fd, "%s</list>\n", indent);
192 error("xmlwrite: unknown type\n");
197 fprintf (fd, "</octave>\n");