C++ 试图在C++;解析kml文件的步骤

C++ 试图在C++;解析kml文件的步骤,c++,C++,我使用getline()将kml文件中的坐标线解析为向量 kml文件的相关部分如下所示(将数字替换为x) 你知道这是怎么回事吗? 谢谢大家! 更新: #include <iostream> #include <vector> #include <fstream> #include <string> using namespace std; vector <string> coordinates; int main() { fs

我使用getline()将kml文件中的坐标线解析为向量

kml文件的相关部分如下所示(将数字替换为x)

你知道这是怎么回事吗? 谢谢大家!

更新:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

vector <string> coordinates;

int main() {
   fstream inputFile("Fish.kml", fstream::in);
   string str;

   bool running = true;
   int counter = 0;

   while (getline(inputFile, str, '\0')) {

      if (str == "        <coordinates>") {
         counter++;
      }
      if (counter > 0 && str != "        </coordinates>") {
         coordinates.push_back(str);

      } 
      if (counter > 0 && str == "        </coordinates>") {
         counter = -1;
         inputFile.close();
      }
   }


   for (int i = 0; i < coordinates.size(); i++) {
      cout << coordinates[i] << "\n";
   }

   return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
矢量坐标;
int main(){
fstream输入文件(“Fish.kml”,fstream::in);
字符串str;
bool running=true;
int计数器=0;
while(getline(inputFile,str,'\0')){
如果(str==“”){
计数器++;
}
如果(计数器>0&&str!=“”){
坐标。推回(str);
} 
如果(计数器>0&&str==“”){
计数器=-1;
inputFile.close();
}
}
对于(int i=0;iwhile(running)
替换为
while(getline(inputFile,str)
并删除
running
变量,因为不再需要它

而且

  if (str == "<coordinates>") {
     counter++;
  }
  if (counter > 0 && str != "</coordinates>") {
     coordinates.push_back(str);

  } 
  if (counter > 0 && str == "</coordinates>") {
     counter = -1;
  }
if(str==“”){
计数器++;
}
如果(计数器>0&&str!=“”){
坐标。推回(str);
} 
如果(计数器>0&&str==“”){
计数器=-1;
}

谢谢,这解决了它被卡住的问题。它似乎仍然没有将坐标记录为字符串。程序执行良好,但不会打印回向量。小心与固定数量的空格进行比较。我不知道KML,但一般来说XML空格没有严格的规定。一个空格,十个空格,加上一个空格几乎没有额外的行和制表符或其他内容,这一切都是一样的。与使用字符串比较相比,您可能会使用“”。感谢您的建议,我认为您是对的。在清理完其余代码后,if语句似乎无法识别“”。我从未使用过您之前建议的方法,因此我将做一些研究,尝试并更新。另一种方法是使用XML库来完成查找标记的繁重工作,然后将标记的内容放入一个XML库中,并使用
getline
解析标记中的行。这似乎是一种可行的方法。我尝试使用查找(“字符串”)但我无法在原始代码中创建布尔参数。我将尝试您的第二个建议。不幸的是,我对XML或需要解析的库没有什么经验,但我进行了一些快速搜索,Tinyxml2库似乎是一个很好的起点。
find
返回一个不可能的索引,
 npos
当它找不到搜索字符串时,因此您需要
str.find(“”!=std::string::npos
irectory>test.exe
^C
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

vector <string> coordinates;

int main() {
   fstream inputFile("Fish.kml", fstream::in);
   string str;

   bool running = true;
   int counter = 0;

   while (getline(inputFile, str, '\0')) {

      if (str == "        <coordinates>") {
         counter++;
      }
      if (counter > 0 && str != "        </coordinates>") {
         coordinates.push_back(str);

      } 
      if (counter > 0 && str == "        </coordinates>") {
         counter = -1;
         inputFile.close();
      }
   }


   for (int i = 0; i < coordinates.size(); i++) {
      cout << coordinates[i] << "\n";
   }

   return 0;
}
  if (str == "<coordinates>") {
     counter++;
  }
  if (counter > 0 && str != "</coordinates>") {
     coordinates.push_back(str);

  } 
  if (counter > 0 && str == "</coordinates>") {
     counter = -1;
  }