xmltree.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function 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. % Guillaume Flandin
  21. % $Id: xmltree.m 4460 2011-09-05 14:52:16Z guillaume $
  22. switch(nargin)
  23. case 0
  24. tree.tree{1} = struct('type','element',...
  25. 'name','tag',...
  26. 'attributes',[],...
  27. 'contents',[],...
  28. 'parent',[],...
  29. 'uid',1);
  30. tree.filename = '';
  31. tree = class(tree,'xmltree');
  32. case 1
  33. if isa(varargin{1},'xmltree')
  34. tree = varargin{1};
  35. elseif ischar(varargin{1})
  36. % Input argument is an XML string
  37. if (~exist(varargin{1},'file') && ...
  38. ~isempty(xml_findstr(varargin{1},'<',1,1)))
  39. tree.tree = xml_parser(varargin{1});
  40. tree.filename = '';
  41. % Input argument is an XML filename
  42. else
  43. fid = fopen(varargin{1},'rt');
  44. if (fid == -1)
  45. error(['[XMLTree] Cannot open ' varargin{1}]);
  46. end
  47. xmlstr = fread(fid,'*char')';
  48. %xmlstr = fscanf(fid,'%c');
  49. fclose(fid);
  50. tree.tree = xml_parser(xmlstr);
  51. tree.filename = varargin{1};
  52. end
  53. tree = class(tree,'xmltree');
  54. else
  55. error('[XMLTree] Bad input argument');
  56. end
  57. otherwise
  58. error('[XMLTree] Too many input arguments');
  59. end