我正在使用 tinyxml2,我知道如何获取属性字符串,但我也想获取整数、浮点数和布尔值。所以,我有这个代码:
#include <iostream>
#include <fstream>
#include <tinyxml2.h>
using namespace std;
using namespace tinyxml2;
int main()
{
XMLDocument doc;
doc.LoadFile("sample.xml");
XMLElement *titleElement = doc.FirstChildElement("specimen");
int f = -1;
if (!titleElement->QueryIntAttribute("age", &f))
cerr << "Unable to get value!" << endl;
cout << f << endl;
return 0;
}
sample.xml 是:
<?xml version=1.0?>
<specimen>
<animal>
Dog
</animal>
<age>
12
</age>
</specimen>
不用担心,xml 文件只是一个假样本,不是真的!
无论如何,我仍然无法获得属性“年龄”内的整数值。如果这不起作用,那么我应该如何使用 tinyxml2 从 xml 文档中获取整数和浮点数?