我正在尝试在我的应用程序中创建一个函数,它可以通过xml文件中的属性加载到对象中。我想使用TinyXML2,因为我听说它在游戏中非常简单和快速。
目前我有以下xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Level>
<Pulsator starttime="0" type="0" higherradius="100" lowerradius="10" time="60" y="500" x="300" bpm="60"/>
</Level>Pulsator的每个属性都是我的Pulsator类中的一个变量。我使用下面的函数来导入我的Pulsator,并将它们添加到对象的向量中。
void Game::LoadLevel(string filename)
{
tinyxml2::XMLDocument level;
level.LoadFile(filename.c_str());
tinyxml2::XMLNode* root = level.FirstChild();
tinyxml2::XMLNode* childNode = root->FirstChild();
while (childNode)
{
Pulsator* tempPulse = new Pulsator();
float bpm;
float type;
std::string::size_type sz;
tinyxml2::XMLElement* data = childNode->ToElement();
string inputdata = data->Attribute("bpm");
bpm = std::stof(inputdata, &sz);
if (type == 0)
{
tempPulse->type = Obstacle;
tempPulse->SetColor(D2D1::ColorF(D2D1::ColorF::Black));
}
if (type == 1)
{
tempPulse->type = Enemy;
tempPulse->SetColor(D2D1::ColorF(D2D1::ColorF::Red));
}
if (type == 2)
{
tempPulse->type = Score;
tempPulse->SetColor(D2D1::ColorF(D2D1::ColorF::Green));
}
else
{
tempPulse->type = No_Type;
}
objects.push_back(tempPulse);
}
}每次我到达根节点时,它都不能正确地加载,并且子节点变为空。我是不是用错了,还是我的XML文件有问题?
发布于 2014-09-20 02:36:40
代码没有正确地指定它想要的子级。您想要的是第一个XMLElement,而不是第一个孩子。要做到这一点,在获取childNode时使用以下代码:
tinyxml2::XMLElement* childNode = root->FirstChildElement();这样就省去了以后的演员阵容。(您不需要也不应该使用ToElement())。
https://stackoverflow.com/questions/25901530
复制相似问题