Home > atmlab > arts > xml > xmlReadTag.m

xmlReadTag

PURPOSE ^

Reads a tag from an XML file.

SYNOPSIS ^

function [result,attrlist] = xmlReadTag(fid, filename, itype, ftype, binary, fid2, expected_tag)

DESCRIPTION ^

 Reads a tag from an XML file.

    Internal function. Parses the tag and calls the appropriate reading
    function.

 FORMAT   result = xmlReadTag(fid, filename, binary, ftype, fid2)

 OUT   result     Data read from file
 IN    fid        File descriptor of XML file
 IN    filename   Name of XML file
 IN    itype      Only used if called recursively
 IN    ftype      Only used if called recursively
 IN    binary     Only used if called recursively
 IN    fid2       Only used if called recursively

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

xmlReadTag.m

SOURCE CODE ^

0001 % Reads a tag from an XML file.
0002 %
0003 %    Internal function. Parses the tag and calls the appropriate reading
0004 %    function.
0005 %
0006 % FORMAT   result = xmlReadTag(fid, filename, binary, ftype, fid2)
0007 %
0008 % OUT   result     Data read from file
0009 % IN    fid        File descriptor of XML file
0010 % IN    filename   Name of XML file
0011 % IN    itype      Only used if called recursively
0012 % IN    ftype      Only used if called recursively
0013 % IN    binary     Only used if called recursively
0014 % IN    fid2       Only used if called recursively
0015 
0016 % 2002-11-28   Created by Oliver Lemke.
0017 
0018 function [result,attrlist] = xmlReadTag(fid, filename, itype, ftype, binary, fid2, expected_tag)
0019 
0020 toplevel = 0;
0021 exit_loop = 0;
0022 result = 'error';
0023 
0024 %=== Parsing tags
0025 while ~feof (fid) && ~exit_loop
0026 
0027   c = fgets (fid, 1);
0028   while ~feof (fid) && (c == 10 || c == 13 || c == 32)
0029     c = fgets (fid, 1);
0030   end
0031 
0032   if feof (fid), break, end
0033 
0034   %=== Tag has to start with bracket
0035   if c == '<'
0036     c = fgets (fid, 1);
0037 
0038     %=== Do we have an opening tag here?
0039     if c ~= '/'
0040       tag = c;
0041 
0042       c = fgets (fid, 1);
0043       while ~feof (fid) && c ~= ' ' && c ~= '>'
0044         tag = [tag c];
0045         c = fgets (fid, 1);
0046       end
0047       
0048       tag = strtrim(tag);
0049 
0050       attrlist = {};
0051       if feof (fid)
0052         error ('Unexpected end of file');
0053       elseif c ~= '>'
0054         % Tag with attributes
0055         attrlist = xmlReadAttributes (fid);
0056       end
0057       
0058       switch tag
0059        case 'arts'
0060          toplevel = 1;
0061          format = xmlGetAttrValue (attrlist, 'format');
0062          if strcmp (format, 'binary')
0063              binary = 1;
0064              fid2 = fopen ([filename '.bin'], 'rb');
0065          end
0066          fsize = xmlGetAttrValue (attrlist, 'fsize');
0067          if isempty(fsize)
0068            ftype = 'double';
0069          end
0070          isize = xmlGetAttrValue (attrlist, 'isize');
0071          if isempty(isize)
0072            itype = 'long';
0073          end
0074        case 'comment'
0075          buf = '';
0076          c = fgets (fid, 1);
0077          while ~feof (fid) && c ~= '>'
0078            c = fgets (fid, 1);
0079            buf = [buf c];
0080          end
0081          valid = 0;
0082          if (length(buf) > 9)
0083            p1 = length(buf)-8;
0084            p2 = length(buf)-1;
0085            if strcmp(buf(p1:p2), '/comment')
0086              valid = 1;
0087            end
0088          end
0089 
0090          if valid == 0
0091            error ('Cannot find closing tag for comment');
0092          end
0093         otherwise
0094            if exist('expected_tag', 'var') && ~isempty(expected_tag) && ~strcmp(expected_tag, tag)
0095                error ('Wrong tag, expected "%s", but got "%s"', ...
0096                       expected_tag, tag);
0097            end
0098          func = str2func (['xmlRead', tag]);
0099          result = feval (func, fid, attrlist, itype, ftype, binary, fid2);
0100       end
0101     else %=== or is it a closing tag
0102       c = fgets (fid, 1);
0103       s = c;
0104       while ~feof (fid) && c ~= '>'
0105         c = fgets (fid, 1);
0106         s = [s c];
0107       end
0108       if (~toplevel)
0109         exit_loop = 1;
0110       else
0111         if (strcmp (s, 'arts>') && (fid2))
0112           fclose (fid2);
0113         end
0114       end
0115     end
0116   else
0117     exit_loop = 1;
0118   end
0119 end
0120

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005