source: trunk/src/@xmltree/xmltree.m @ 967

Last change on this file since 967 was 925, checked in by sommeria, 8 years ago

xmltree updated

File size: 2.4 KB
Line 
1function tree = xmltree(varargin)
2% XMLTREE/XMLTREE Constructor of the XMLTree class
3% FORMAT tree = xmltree(varargin)
4%
5% varargin - XML filename or XML string
6% tree     - XMLTree Object
7%
8%     tree = xmltree;             % creates a minimal XML tree: '<tag/>'
9%     tree = xmltree('foo.xml');  % creates a tree from XML file 'foo.xml'
10%     tree = xmltree('<tag>content</tag>') % creates a tree from string
11%__________________________________________________________________________
12%
13% This is the constructor of the XMLTree class.
14% It creates a tree of an XML 1.0 file (after parsing) that is stored
15% using a Document Object Model (DOM) representation.
16% See http://www.w3.org/TR/REC-xml for details about XML 1.0.
17% See http://www.w3.org/DOM/ for details about DOM platform.
18%__________________________________________________________________________
19% Copyright (C) 2002-2011  http://www.artefact.tk/
20
21% Guillaume Flandin
22% $Id: xmltree.m 4460 2011-09-05 14:52:16Z guillaume $
23
24switch(nargin)
25    case 0
26        tree.tree{1} = struct('type','element',...
27                              'name','tag',...
28                              'attributes',[],...
29                              'contents',[],...
30                              'parent',[],...
31                              'uid',1);
32        tree.filename = '';
33        tree = class(tree,'xmltree');
34    case 1
35        if isa(varargin{1},'xmltree')
36            tree = varargin{1};
37        elseif ischar(varargin{1})
38            % Input argument is an XML string
39            if (~exist(varargin{1},'file') && ...
40                ~isempty(xml_findstr(varargin{1},'<',1,1)))
41                tree.tree = xml_parser(varargin{1});
42                tree.filename = '';
43            % Input argument is an XML filename
44            else
45                fid = fopen(varargin{1},'rt');
46                if (fid == -1)
47                    error(['[XMLTree] Cannot open ' varargin{1}]);
48                end
49                xmlstr = fread(fid,'*char')';
50                %xmlstr = fscanf(fid,'%c');
51                fclose(fid);
52                tree.tree = xml_parser(xmlstr);
53                tree.filename = varargin{1};
54            end
55            tree = class(tree,'xmltree');
56        else
57            error('[XMLTree] Bad input argument');
58        end
59    otherwise
60        error('[XMLTree] Too many input arguments');
61end
Note: See TracBrowser for help on using the repository browser.