2016-06-29 18 views
1

私はtinyxml2を使用しており、C++でXMLからいくつかの要素を解析したいと考えています。たとえば、C++でXML要素を解析する

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

"Second"要素ではxのみを解析できます。

#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"); 
} 

"First"要素または "Second y"と同じ方法を試しているときは、エラーが表示されます。私は何をすべきか?

+2

コードを親切に表示してください。 – lorro

+0

tinyxml2チュートリアルを開き、そのまま使用してください。 – Arkady

答えて

0

"double x"を2回定義しています。

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

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

を試してみてくださいあなたのコンパイラは常に警告を探してください、しかし、あなたを停止している必要があります!

+0

これは実際には問題ありません。私は今すぐ構造体を使用しています。私は "First"と "Second"を例として使用しました。ちょうどそのコードは質問にはあまりにも大きくないでしょう –

関連する問題