1

I'm using tinyxml2 and I want to parse some elements from XML in C++. For example

<root>
     <First x="1" y="2">
     <Second x = "1">
     <Second y = "2">
</root>

I can parse only x in "Second" element.

#include <stdio.h>
#include "tinyxml2.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace tinyxml2;
using namespace std;
int main(){
     tinyxml2::XMLError eResult = xml_doc.LoadFile("test.xml");
     if (eResult != tinyxml2::XML_SUCCESS) return false;

     tinyxml2::XMLNode* root = xml_doc.FirstChildElement("root");
     if (root == nullptr) return false;

     tinyxml2::XMLElement* First = root->FirstChildElement("First");
     if (First == nullptr) return false;

     double x1 = std::stod(First->Attribute("x"));
     double y1 = std::stod(First->Attribute("y"));

     tinyxml2::XMLElement* Second = root->FirstChildElement("Second");
     if (Second == nullptr) return false;

     double x2 = std::stod(Second->Attribute("x"));
     double y2 = std::stod(Second->Attribute("y"));

     system("pause");
}

when I'm trying same method for "First" element or for "Second y", it just shows me errors. What should I do?

1 Answers1

0

You are defining "double x" twice. Try

double first_x = std::stod(First->Attribute("x"));
double first_y = std::stod(First->Attribute("y"));

and

double second_x = std::stod(Second->Attribute("x"));

Your compiler should have stopped you though, always look out for warnings!

CppChris
  • 1,226
  • 9
  • 14