% cleaning_Monday.m
clc
clear

% define variables
m_id = [];      % vehicle's id
m_status = [];  % status
m_time = {};    % timestamp - using cell array
m_int = [];     % sample rate
m_ttd = [];     % time to destination
m_meter = [];   % meters from the last position
m_speed = [];   % velocity
m_x = [];       % GPS x-coordinate
m_y = [];       % GPS y-coordinate
m_sektor = [];  % sektor in Vienna
m_area = [];    % area in Vienna
m_sat = [];     % number of visible satellites
m_pdop = [];    % dilution of precision

% loop through all of the xml files in directory 2014-03-24
for i = 1:53
    fileNumber = sprintf('%02d',i);
    fileName = strcat('20140324GPS_',fileNumber,'.xml');
     
    xDoc = xmlread(fullfile('2014-03-24',fileName));
    
    allListItems = xDoc.getElementsByTagName('FAHRT');
    ListLength = allListItems.getLength - 1;

    % loop through to end of the file
    for k = 0:ListLength
      % get first node
      node = allListItems.item(k).getFirstChild;
      
      % get length of root node FAHRT and subnode ABFAHRT
      fahrtLength = allListItems.item(k).getLength;
      abfahrtLength = allListItems.item(k).getElementsByTagName('ABFAHRT').item(0).getLength;
      
      % check whether the FAHRT (13 nodes) and ABFAHRT (10 nodes) record is complete
      if fahrtLength == 13 && abfahrtLength == 10
          % loop through individual FAHRT record and select some of its nodes
          while ~isempty(node)
              % distinguish between different nodes and store them in variables
              if strcmpi(node.getNodeName,'ID')
                  m_id(end+1) = str2double(node.getTextContent);
              elseif strcmpi(node.getNodeName,'STATUS')
                  m_status(end+1) = str2double(node.getTextContent);
              elseif strcmpi(node.getNodeName,'ZEITPUNKT')
                  m_time{end+1} = node.getTextContent;
              elseif strcmpi(node.getNodeName,'SEKUNDEN')
                  m_int(end+1) = str2double(node.getTextContent);
              elseif strcmpi(node.getNodeName,'SOLLZEIT')
                  m_ttd(end+1) = str2double(node.getTextContent);
              elseif strcmpi(node.getNodeName,'METER')
                  m_meter(end+1) = str2double(node.getTextContent);
              elseif strcmpi(node.getNodeName,'KMH')
                  m_speed(end+1) = str2double(node.getTextContent);
              % ABFAHRT node contains more subnodes
              elseif strcmpi(node.getNodeName,'ABFAHRT')
                  subnode = node.getFirstChild;
             
                  % loop through nodes of ABFAHRT
                  while ~isempty(subnode)
                      subname = subnode.getNodeName;
                      
                      % distinguish between different subnodes and store them in variables
                      if strcmpi(subnode.getNodeName,'X')
                          m_x(end+1) = str2double(subnode.getTextContent);
                      elseif strcmpi(subnode.getNodeName,'Y')
                          m_y(end+1) = str2double(subnode.getTextContent);
                      elseif strcmpi(subnode.getNodeName,'SEKTOR')
                          m_sektor(end+1) = str2double(subnode.getTextContent);
                      elseif strcmpi(subnode.getNodeName,'GEBIET')
                          m_area(end+1) = str2double(subnode.getTextContent);
                      elseif strcmpi(subnode.getNodeName,'SATELLITEN')
                          m_sat(end+1) = str2double(subnode.getTextContent);
                      elseif strcmpi(subnode.getNodeName,'PDOP')
                          m_pdop(end+1) = str2double(subnode.getTextContent);
                      
                          break
                      end
                 
                      % move to the next subnode
                      subnode = subnode.getNextSibling;

                  end
             
                  break
         
              end
          
              % move to the next node
              node = node.getNextSibling;
          end
       end
    end
end

% saves the variables in a file
save monday_24_03.mat m_id m_status m_time m_int m_ttd m_meter m_speed m_x m_y m_sektor m_area m_sat m_pdop

