source: trunk/src/@xmltree/save.m @ 731

Last change on this file since 731 was 723, checked in by sommeria, 11 years ago

xmltree and toolbox_calib added to svn

File size: 2.9 KB
RevLine 
[723]1function save(tree, filename)
2% XMLTREE/SAVE Save an XML tree in an XML file
3% FORMAT save(tree,filename)
4%
5% tree     - XMLTree
6% filename - XML output filename
7%_______________________________________________________________________
8%
9% Convert an XML tree into a well-formed XML string and write it into
10% a file or display it (send it to standard output) if no filename is
11% given.
12%
13%-----------------------------------------------------------------------
14% SUBFUNCTIONS:
15%
16% FORMAT print_subtree(tree,fid,order)
17% Send tree entity XML formatted string to fid using 'order' blanks
18% tree  - XML tree
19% fid   - file identifier
20% uid   - uid of the current element
21% order - order of recursivity
22%
23% FORMAT str = entity(str)
24% Tool to replace input string in internal entities
25% str  - string to modify
26%_______________________________________________________________________
27% @(#)save.m                 Guillaume Flandin                 01/07/11
28
29error(nargchk(1,2,nargin));
30
31prolog = '<?xml version="1.0" ?>\n';
32
33%- Standard output
34if nargin==1
35        fid = 1;
36%- Output specified
37elseif nargin==2
38        %- Filename provided
39        if isstr(filename)
40                [fid, msg] = fopen(filename,'w');
41                if fid==-1, error(msg);end
42                if isempty(tree.filename), tree.filename = filename; end
43        %- File identifier provided
44        elseif isnumeric(filename) & prod(size(filename)) == 1
45                fid = filename;
46                prolog = ''; %- In this option, do not write any prolog
47        %- Rubbish provided
48        else
49                error('[XMLTree] Invalid argument.');
50        end
51end
52
53fprintf(fid,prolog);
54print_subtree(tree,fid);
55
56if nargin==2 & isstr(filename), fclose(fid); end
57
58if nargout==1, varargout{1} = tree; end
59
60%=======================================================================
61function print_subtree(tree,fid,uid,order)
62        if nargin <3, uid = root(tree); end
63        if nargin < 4, order = 0; end
64        fprintf(fid,blanks(3*order));
65        switch tree.tree{uid}.type
66                case 'element'
67                        fprintf(fid,'<%s',tree.tree{uid}.name);
68                        for i=1:length(tree.tree{uid}.attributes)
69                                fprintf(fid,' %s="%s"',...
70                                tree.tree{uid}.attributes{i}.key,...
71                                tree.tree{uid}.attributes{i}.val);
72                        end
73                        fprintf(fid,'>\n');
74                        for i=1:length(tree.tree{uid}.contents)
75                                print_subtree(tree,fid,tree.tree{uid}.contents(i),order+1)
76                        end
77                        for i=1:order, fprintf(fid,'   '); end
78                        fprintf(fid,'</%s>\n',tree.tree{uid}.name);
79                case 'chardata'
80                        fprintf(fid,'%s\n',entity(tree.tree{uid}.value));
81                case 'cdata'
82                                fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
83                case 'pi'
84                        fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target,tree.tree{uid}.value);
85                case 'comment'
86                        fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value);
87                otherwise
88                        warning(sprintf('Type %s unknown : not saved',tree.tree{uid}.type));
89        end
90   
91%=======================================================================
92function str = entity(str)
93        str = strrep(str,'&','&amp;');
94        str = strrep(str,'<','&lt;');
95        str = strrep(str,'>','&gt;');
96        str = strrep(str,'"','&quot;');
97        str = strrep(str,'\','&apos;');
Note: See TracBrowser for help on using the repository browser.