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