Attribute Parser

  • + 0 comments

    Code:

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    void handleAttributes(vector<pair<string, string>>& attributes)
    {
      std::string attributeName;
      std::string temp;
      std::string attributeValue;
    
      while (1)
      {
        cin >> attributeName;
        cin >> temp;
        cin >> temp;
        attributeValue = temp.substr(1, temp.find_last_of('"') -1);
    
        attributes.push_back(make_pair(attributeName, attributeValue));
    
        if (temp.back() == '>')
          break;
      }
    }
    
    
    
    int main() {
      /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    
    
      int n, q;
    
      cin >> n;
      cin >> q;
    
      vector<string> tags;
      vector<vector<pair<string, string>>> attributes;
    
      string temp;
      string tag;
      string prevTag;
    
    
      for (int i = 0; i < n; i++)
      {
    
        // parsing one line into the data
    
        //Searching for tag name
    
        cin >> tag;
    
        tag = tag.substr(1);
    
        //Checking if starting or ending
    
        if (tag[0] == '/') // ending
        {
          if (std::string::npos != prevTag.find('.'))
            prevTag = prevTag.substr(0, prevTag.find_last_of('.'));
          else
            prevTag = "";
          continue;
        }
        else //starting tag
        {
          vector<pair<string, string>> attributesOfTag;
          if (tag.back() == '>')
          {
            tag.pop_back();
          }
          else
          {
            handleAttributes(attributesOfTag);
          }
    
          if (prevTag != "")
          {
            tag = prevTag + "." + tag;
          }
          tags.push_back(tag);
          prevTag = tag;
    
    
          attributes.push_back(attributesOfTag);
        }
      }
    
    
      string tagName;
      string attributeName;
    
      vector<string> results;
    
      for (int i = 0; i < q; i++)
      {
        cin >> tagName;
        attributeName = tagName.substr(tagName.find_last_of('~') + 1);
        tagName = tagName.substr(0, tagName.find_last_of('~'));
    
        int j = 0;
        bool foundTag = false;
        bool foundAttribute = false;
        for (const auto& tagIt : tags)
        {
          if (tagIt == tagName)
          {
            foundTag = true;
            for (const auto& attributeIt : attributes[j])
            {
              if (attributeIt.first == attributeName)
              {
                results.push_back(attributeIt.second);
                foundAttribute = true;
                break;
              }
    
            }
            if (foundAttribute == false)
              results.push_back("Not Found!");
            break;
          }
          j++;
        }
        if (!foundTag)
          results.push_back("Not Found!");
      }
    
    
    
      for (const auto& result : results)
      {
        cout << result << endl;
      }
    
      return 0;
    }