TinyXML Attributwerte ändern

hury

Erfahrenes Mitglied
Hallo,

ich habe mehrere Einträge in meiner XML Datei in der Form:


Code:
<root>
    <Icon Name="1.txt" X="-2.35" Y="1.56" Z="-5.5" />
    <Icon Name="1717_arbeitsdateien" X="-2.35" Y="1.26" Z="-5.5" />
...
</root>

Eine Methode soll jetzt über alle Elemente iterieren, falls ein Element mit dem übergebenen Namen übereinstimmt sollen die X,Y,Z Werte aktualisiert werden.

Die Methode:

C++:
BOOL updateElement(TiXmlElement element)
{

	// Get the config file
	TiXmlDocument doc( (char*)positionDataFile.c_str() );

	// Try loading config file
	if ( !doc.LoadFile() )	
		cout << "could not read file";

	BOOL result = false;

	TiXmlNode * iNode = doc.FirstChild( "root" );
	TiXmlNode * itemNode = iNode->FirstChild( "Icon" );

	if( iNode != NULL )
	{
		while( itemNode != NULL )
		{

			string temp1 = itemNode->ToElement()->Attribute("Name");
			string temp2 = element.ToElement()->Attribute("Name");
			


			if(strcmp(temp1.c_str(), temp2.c_str()) == 0)
			{
				itemNode->ToElement()->SetAttribute("X", element.ToElement()->Attribute("X"));
				itemNode->ToElement()->SetAttribute("Y", element.ToElement()->Attribute("Y"));
				itemNode->ToElement()->SetAttribute("Z", element.ToElement()->Attribute("Z"));

				cout << "Setting Z of element: " << temp2 << "to: " << element.ToElement()->Attribute("Z") << "\n";

				result = true;
				break;
			}
				
			itemNode = iNode->IterateChildren( "Icon", itemNode );
		}
	}

	doc.SaveFile();
	return result;
}

Der Aufruf:

C++:
// Set attributes for an Element. 
	wert.SetAttribute("Name", name);
	wert.SetAttribute("X", xString.c_str());
	wert.SetAttribute("Y", yString.c_str());
	wert.SetAttribute("Z", zString.c_str());

	// Get the root Element 
	root = doc.FirstChildElement("root");
	if (root)
	{

		// This checks if the node wert is already in the .xml file
		// If it is inside, the values will be updated.
		if(!updateElement(wert))
			root->InsertEndChild(wert);
}

Das Problem: das Aktualisieren klappt nicht. Dh die updateElement Methode hat wohl noch einen Bug, den ich aber nicht finde. Hat jemand eine Idee?

Gruß
Alex
 
Zurück