I'm unloading an array of data from с++ object to a XML file. How can I get data from their attribute (in my case the attribute name is "Data"), and save result to some buffer for further data manipulation?
I'm using the TinyXml library.
Function saving an object (see the result in XML below)
TiXmlDocument doc("text.xml");
if (!doc.LoadFile())
return false;
TiXmlHandle hDoc(&doc);
TiXmlElement* elem;
TiXmlHandle hRoot(0);
elem = hDoc.FirstChildElement().Element();
if (!elem) return false;
std::string name = elem->Value();
hRoot = TiXmlHandle(elem);
TiXmlElement* chunkbranch = new TiXmlElement("ChunkBranch");
root->LinkEndChild(chunkbranch);
for (int x = 0; x < this->mapChunk.size(); x++)
{
for (int y = 0; y < this->mapChunk[x].size(); y++)
{
TiXmlElement* chunk;
chunk = new TiXmlElement("Chunk");
chunkbranch->LinkEndChild(chunk);
chunk->SetAttribute("posX", x);
chunk->SetAttribute("posY", y);
TiXmlElement* chunkdata;
chunkdata = new TiXmlElement("ChunkData");
chunk->LinkEndChild(chunkdata);
chunkdata->SetAttribute("data", this->mapChunk[x][y]->getChunkAsString().c_str());
//maybe can be use some other method for saving data?
}
}
Here I'm calling attribute "ChunkData". Here trying to get array values "ChunkBranch" >> "ChunkData" >> data
elem = hRoot.FirstChild("ChunkBranch").FirstChild().Element();
for (elem; elem; elem = elem->NextSiblingElement())
{
std::string buff("");
buff = elem->Value();
if (buff == "Chunk")
{
int buffX;
int xKey = elem->QueryIntAttribute("posX", &buffX);
int yKey = elem->QueryIntAttribute("posY", &buffX);
this->mapChunk[xKey][yKey] = &Chunk(sf::Vector2f(xKey,yKey));
std::cout << "In chunk:\t" << xKey << " x " << yKey<< "\n";
TiXmlElement* cData;
cData = elem->FirstChildElement("ChunkData");
int test;
const char* pKey = cData->Value();
const char* xke = cData->GetText();
cData->QueryIntAttribute("data", &test);//<< somewhere here the
//program doesn't return array
//and pKey = 0 (not NULL, just 0), after loading it will stop
XML data
<?xml version="1.0" ?>
<base>
<ChunkBranch>
<Chunk posX="0" posY="0">
<ChunkData data="0 19 0 0 0 0 0 19 0 0 0 0 0 0 19 19 0 0 0 0 0 " />
</Chunk>
<Chunk posX="0" posY="1">
<ChunkData data="0 0 0 0 0 0 0 0 19 0 0 0 0 0 19 0 0 0 0 0 0 19 19 0 0 0 0 0 "/>
</ChunkBranch>
</base>