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>