Index: trunk/src/@xmltree/save.m
===================================================================
--- trunk/src/@xmltree/save.m	(revision 805)
+++ trunk/src/@xmltree/save.m	(revision 821)
@@ -1,27 +1,13 @@
-function save(tree, filename)
+function varargout = save(tree, filename)
 % XMLTREE/SAVE Save an XML tree in an XML file
-% FORMAT save(tree,filename)
+% FORMAT varargout = save(tree,filename)
 %
-% tree     - XMLTree
-% filename - XML output 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 display it (send it to standard output) if no filename is 
-% given.
-%
-%-----------------------------------------------------------------------
-% SUBFUNCTIONS:
-%
-% FORMAT print_subtree(tree,fid,order)
-% Send tree entity XML formatted string to fid using 'order' blanks
-% tree  - XML tree
-% fid   - file identifier
-% uid   - uid of the current element
-% order - order of recursivity
-%
-% FORMAT str = entity(str)
-% Tool to replace input string in internal entities
-% str  - string to modify
+% a file or return it as a string if no filename is provided.
 %_______________________________________________________________________
 % @(#)save.m                 Guillaume Flandin                 01/07/11
@@ -31,34 +17,73 @@
 prolog = '<?xml version="1.0" ?>\n';
 
-%- Standard output
-if nargin==1
-	fid = 1;
+%- Return the XML tree as a string
+if nargin == 1
+	varargout{1} = [sprintf(prolog) ...
+		print_subtree(tree,'',root(tree))];
 %- Output specified
-elseif nargin==2
+else
 	%- Filename provided
-	if ischar(filename)
+	if isstr(filename)
 		[fid, msg] = fopen(filename,'w');
-		if fid==-1, error(msg);end
+		if fid==-1, error(msg); end
 		if isempty(tree.filename), tree.filename = filename; end
 	%- File identifier provided
 	elseif isnumeric(filename) & prod(size(filename)) == 1
 		fid = filename;
-		prolog = ''; %- In this option, do not write any prolog
-	%- Rubbish provided
+		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 isstr(filename), fclose(fid); end
+	if nargout == 1
+		varargout{1} = print_subtree(tree,'',root(tree));
+	end
 end
 
-fprintf(fid,prolog);
-print_subtree(tree,fid);
-
-if nargin==2 && ischar(filename), fclose(fid); end
-
-if nargout==1, varargout{1} = tree; 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 print_subtree(tree,fid,uid,order)
-	if nargin <3, uid = root(tree); end
+function save_subtree(tree,fid,uid,order)
 	if nargin < 4, order = 0; end
 	fprintf(fid,blanks(3*order));
@@ -68,13 +93,18 @@
 			for i=1:length(tree.tree{uid}.attributes)
 				fprintf(fid,' %s="%s"',...
-				tree.tree{uid}.attributes{i}.key,...
+				tree.tree{uid}.attributes{i}.key, ...
 				tree.tree{uid}.attributes{i}.val);
 			end
-			fprintf(fid,'>\n');
-			for i=1:length(tree.tree{uid}.contents)
-				print_subtree(tree,fid,tree.tree{uid}.contents(i),order+1)
-  			end
-			for i=1:order, fprintf(fid,'   '); end
-			fprintf(fid,'</%s>\n',tree.tree{uid}.name);
+			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));
@@ -82,11 +112,14 @@
 				fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
 		case 'pi'
-			fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target,tree.tree{uid}.value);
+			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('Type %s unknown : not saved',tree.tree{uid}.type));
+			warning(sprintf('[XMLTree] Type %s unknown: not saved', ...
+				tree.tree{uid}.type));
 	end
-   
+
+
 %=======================================================================
 function str = entity(str)
@@ -95,3 +128,3 @@
 	str = strrep(str,'>','&gt;');
 	str = strrep(str,'"','&quot;');
-	str = strrep(str,'\','&apos;');
+	str = strrep(str,'''','&apos;');
