-2

I am parsing a .xml file, for educational reasons. I know that the correct and safe way is to use a third party library, but I wish to extend my knowledge in C++ by using its libraries and methods.

I have read the file and load its contents to a string. I have removed the spaces and now I am trying to capture the value between the tag low. I am using the .find() method in order to match the tag and then I am trying to save it to a new string. My problem is that my code removes everything before the first match and what begins and ends with the tag I provide. Is there a way to, maybe, add to a new string or a vector the value between the tag using my approach?

Edit: I edited the code to include all everything I wrote.

Here is the code I am using:

 int main(){
    std::ifstream inFile("demo.xml");
    std::string contents;


    bool found_tag = false;

    std::cout << "line 17" << std::endl;
    while (getline(inFile, contents))
    {
        std::string temp;
        std::string low;
        for (int i = 0; i < contents.length(); ++i)
        {
            if (contents[i] == ' ' && temp.size() == 0) {}
            else
            {
                temp += contents[i];
            }
        }


        unsigned int begin_tag = temp.find("<low>"); //when printed shows a random number or 0
        unsigned int end_tag = temp.find("</low>"); // same as the above.
        if (begin_tag != std::string::npos)
        {
            found_tag = true;
            continue;
        }
        else if (end_tag != std::string::npos)
        {
            found_tag = false;
        }

        if (found_tag)
        {
            low += temp;
            std::cout << low << std::endl;
        }
      return 0;
    }

Output:

    <high>200</high>
    </interval>
    <interval>
    <high>400</high>
    </interval>
    <interval>
    <high>700</high>
    </interval>
    <interval>
    <high>800</high>
    </interval>
    </intervals>
    </root>     
  • The presented code does not make sense. – Vlad from Moscow Sep 27 '19 at 11:18
  • You can use `.insert()` to insert new text at a particular character position in a string, if that is what you want to do? – Gem Taylor Sep 27 '19 at 11:32
  • @Savvas Savvidis Provide a minimal complete compiled program that reproduces the problem. Fro example this if statement with the enclosed continue if (begin_tag != std::string::npos) { found_tag = true; continue; } is totally unclear. – Vlad from Moscow Sep 27 '19 at 11:35
  • 1
    I want to retrieve the value between specific tags. The problem is that I have created a huge string and when I am printing to the console the position of the .find() method I get all zeros or a random number. I tried the approach suggested below but it did not work. – Savvas Savvidis Sep 27 '19 at 12:29
  • @VladfromMoscow to you it doesn't make sense. – fonZ Sep 27 '19 at 13:46
  • @fonZ It does not make sense for any more or less qualified programmer. It is a code of a beginner. For example the return statement is within the while loop. – Vlad from Moscow Sep 27 '19 at 13:51
  • @VladfromMoscow Why would a return statement within a while loop be bad? https://stackoverflow.com/questions/10800195/return-in-for-loop-or-outside-loop – fonZ Jan 05 '20 at 13:07

1 Answers1

3

When you do low += temp;, you are adding the whole input.

You probably want the text between begin_tag and end_tag (perhaps not the first 5 characters, which will always be "<low>")

Try low += temp.substr(begin_tag + 5, end_tag - begin_tag - 5);

Caleth
  • 52,200
  • 2
  • 44
  • 75