1 year ago

#386325

test-img

Nells

How to navigate through XML and return a tag from a multidimensional map that was written to XML

I am currently trying to implement a very simple (primitive) way to read tags from a multidimensional map inserted into an xml file.

I am able to find a word and its line (not line number!) but as soon as I want to navigate through that map and return for example a key from the most inner map then this method does not really serve me well.

I know i could use xpath and so on but I am required for learning reasons to implement it manually with if statements or somehting like that.

My understanding: I could use this method below but I have to find a way to get the line number, store that and then continue scanning (reading) further. My concern is, wouldnt that be a problem if the file constantly changes because map values get added and removed?

Pseudo code:

  • if tag1 in Level1 found, look for tag4 in Level2 and so on...

My code: (not yet finished -stuck-)

string search, search2;
    string line, line2;
    std::size_t offset, offset2;
   

    ifstream Myfile;
    Myfile.open("D:\\Projects\\Internal Projects\\Learning C++\\demoFile.xml");
    cout << "which word?" << endl;
    cin >> search;
    //cout << "my file: " << ;

    if (Myfile.is_open())
    {
        while (!Myfile.eof())
        {
            getline(Myfile, line);
            if (offset = (line.find(search, 0)) != string::npos)
            {
                cout << "The word was found --->   " << search << endl;
                cout << " Line = " << line << endl;
                
                cout << "next word?" << endl;
                cin >> search2;
            }
            //When found continue with this
            
            if (offset2 = (line2.find(search2, 0)) != string::npos)
            {
                cout << "2nd word was found --->   " << search2 << endl;
                cout << " Line = " << line2 << endl;



                //When found continue with this

            }
        }
        Myfile.close();
    }
    else
        cout << "Couldn't open file" << endl;
   
    
<rootTag>
  <Level1>
    <tag id = 1>
      <Level2>
        <tag id = 1>
          <Level3>
            <tag id = 1>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
            <tag id = 2>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
          </Level3>
        </tag>
        <tag id = 2>
          <Level3>
            <tag id = 1>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
            <tag id = 2>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
          </Level3>
        </tag>
      </Level2>
    </tag>
    <tag id = 2>
      <Level2>
        <tag id = 1>
          <Level3>
            <tag id = 1>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
            <tag id = 2>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
          </Level3>
        </tag>
        <tag id = 2>
          <Level3>
            <tag id = 1>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
            <tag id = 2>
              <Level4>
                <tag id = 1>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
                <tag id = 2>
                  <measurement id = 1649323440341>   7575.000000   
                  </measurement>
                </tag>
              </Level4>
            </tag>
          </Level3>
        </tag>
      </Level2>
    </tag>
  </Level1>
</rootTag>

xml

dictionary

parsing

txt

0 Answers

Your Answer

Accepted video resources