<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Immernoch Probleme mit TinyXML]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mehrere Einträge in meiner XML Datei in der Form:</p>
<pre><code>&lt;root&gt;
    &lt;Icon Name=&quot;1.txt&quot; X=&quot;-2.35&quot; Y=&quot;1.56&quot; Z=&quot;-5.5&quot; /&gt;
    &lt;Icon Name=&quot;1717_arbeitsdateien&quot; X=&quot;-2.35&quot; Y=&quot;1.26&quot; Z=&quot;-5.5&quot; /&gt;
...
&lt;/root&gt;
</code></pre>
<p>Eine Methode soll jetzt über alle Elemente iterieren, falls ein Element mit dem übergebenen Namen übereinstimmt sollen die X,Y,Z Werte aktualisiert werden.</p>
<p>Die Methode:</p>
<pre><code class="language-cpp">BOOL updateElement(TiXmlElement element)
{

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

	// Try loading config file
	if ( !doc.LoadFile() )	
		cout &lt;&lt; &quot;could not read file&quot;;

	BOOL result = false;

	TiXmlNode * iNode = doc.FirstChild( &quot;root&quot; );
	TiXmlNode * itemNode = iNode-&gt;FirstChild( &quot;Icon&quot; );

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

			string temp1 = itemNode-&gt;ToElement()-&gt;Attribute(&quot;Name&quot;);
			string temp2 = element.ToElement()-&gt;Attribute(&quot;Name&quot;);

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

				cout &lt;&lt; &quot;Setting Z of element: &quot; &lt;&lt; temp2 &lt;&lt; &quot;to: &quot; &lt;&lt; element.ToElement()-&gt;Attribute(&quot;Z&quot;) &lt;&lt; &quot;\n&quot;;

				result = true;
				break;
			}

			itemNode = iNode-&gt;IterateChildren( &quot;Icon&quot;, itemNode );
		}
	}

	doc.SaveFile();
	return result;
}
</code></pre>
<p>Der Aufruf:</p>
<pre><code class="language-cpp">// Set attributes for an Element. 
	wert.SetAttribute(&quot;Name&quot;, name);
	wert.SetAttribute(&quot;X&quot;, xString.c_str());
	wert.SetAttribute(&quot;Y&quot;, yString.c_str());
	wert.SetAttribute(&quot;Z&quot;, zString.c_str());

	// Get the root Element 
	root = doc.FirstChildElement(&quot;root&quot;);
	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-&gt;InsertEndChild(wert);
}
</code></pre>
<p>Das Problem: das Aktualisieren klappt nicht. Dh die <strong>updateElement</strong> Methode hat wohl noch einen Bug, den ich aber nicht finde. Hat jemand eine Idee?</p>
<p>Gruß<br />
Alex</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/226474/immernoch-probleme-mit-tinyxml</link><generator>RSS for Node</generator><lastBuildDate>Wed, 30 Sep 2026 18:59:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/226474.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Nov 2008 19:21:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sat, 01 Nov 2008 19:23:58 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mehrere Einträge in meiner XML Datei in der Form:</p>
<pre><code>&lt;root&gt;
    &lt;Icon Name=&quot;1.txt&quot; X=&quot;-2.35&quot; Y=&quot;1.56&quot; Z=&quot;-5.5&quot; /&gt;
    &lt;Icon Name=&quot;1717_arbeitsdateien&quot; X=&quot;-2.35&quot; Y=&quot;1.26&quot; Z=&quot;-5.5&quot; /&gt;
...
&lt;/root&gt;
</code></pre>
<p>Eine Methode soll jetzt über alle Elemente iterieren, falls ein Element mit dem übergebenen Namen übereinstimmt sollen die X,Y,Z Werte aktualisiert werden.</p>
<p>Die Methode:</p>
<pre><code class="language-cpp">BOOL updateElement(TiXmlElement element)
{

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

	// Try loading config file
	if ( !doc.LoadFile() )	
		cout &lt;&lt; &quot;could not read file&quot;;

	BOOL result = false;

	TiXmlNode * iNode = doc.FirstChild( &quot;root&quot; );
	TiXmlNode * itemNode = iNode-&gt;FirstChild( &quot;Icon&quot; );

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

			string temp1 = itemNode-&gt;ToElement()-&gt;Attribute(&quot;Name&quot;);
			string temp2 = element.ToElement()-&gt;Attribute(&quot;Name&quot;);

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

				cout &lt;&lt; &quot;Setting Z of element: &quot; &lt;&lt; temp2 &lt;&lt; &quot;to: &quot; &lt;&lt; element.ToElement()-&gt;Attribute(&quot;Z&quot;) &lt;&lt; &quot;\n&quot;;

				result = true;
				break;
			}

			itemNode = iNode-&gt;IterateChildren( &quot;Icon&quot;, itemNode );
		}
	}

	doc.SaveFile();
	return result;
}
</code></pre>
<p>Der Aufruf:</p>
<pre><code class="language-cpp">// Set attributes for an Element. 
	wert.SetAttribute(&quot;Name&quot;, name);
	wert.SetAttribute(&quot;X&quot;, xString.c_str());
	wert.SetAttribute(&quot;Y&quot;, yString.c_str());
	wert.SetAttribute(&quot;Z&quot;, zString.c_str());

	// Get the root Element 
	root = doc.FirstChildElement(&quot;root&quot;);
	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-&gt;InsertEndChild(wert);
}
</code></pre>
<p>Das Problem: das Aktualisieren klappt nicht. Dh die <strong>updateElement</strong> Methode hat wohl noch einen Bug, den ich aber nicht finde. Hat jemand eine Idee?</p>
<p>Gruß<br />
Alex</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608716</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608716</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Sat, 01 Nov 2008 19:23:58 GMT</pubDate></item><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sun, 02 Nov 2008 08:46:32 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">if(strcmp(temp1.c_str(), temp2.c_str()) == 0)
</code></pre>
<p>Wieso nicht einfach</p>
<pre><code class="language-cpp">if(temp1 == temp2)
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608836</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608836</guid><dc:creator><![CDATA[The-Kenny]]></dc:creator><pubDate>Sun, 02 Nov 2008 08:46:32 GMT</pubDate></item><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sun, 02 Nov 2008 11:34:46 GMT]]></title><description><![CDATA[<p>filth schrieb:</p>
<blockquote>
<p>Das Problem: das Aktualisieren klappt nicht. Dh die <strong>updateElement</strong> Methode hat wohl noch einen Bug, den ich aber nicht finde. Hat jemand eine Idee?</p>
</blockquote>
<p>Tausende ...</p>
<p>Aber zuerst einmal:</p>
<pre><code class="language-cpp">TiXmlDocument doc( (char*)positionDataFile.c_str() );
</code></pre>
<p>Was hatte ich gesagt? Böse! Aus! :p</p>
<p>Zu deinem Problem:<br />
Hast du schon mal mit C++ gearbeitet und/oder mal die TinyXML Dokumentation durchgelesen? Dein Vorgehen ist zum Scheitern verdammt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Ich stelle wohl am einfachsten ein paar Fragen:<br />
- Wieso öffnest du in der Funktion das Dokument erneut und speicherst es am Ende wieder? Wieso übergibst du nicht das Dokument, welches du bereits geöffnet hast per Referenz? Da wird am Ende sicher das falsche Dokument gespeichert.<br />
- Wieso verwendest du <code>BOOL</code> , statt <code>bool</code> ? Zu viel Windowsprogrammierung gehabt?<br />
- Wieso übergibst du ein <code>TiXmlElement</code> Objekt und nimmst es intern dann wieder auseinander. Wieso nicht gleich die Parameter übergeben, am besten per const-Referenz?<br />
- Wieso vergleichst du die <code>std::string</code> nicht direkt miteinander, sondern mühsam über C-Funktionen?<br />
- Wieso iterierst du so kompliziert durch deine Nodes und zum Beispiel nicht gleich über die Elemente und ihren Geschwistern?<br />
- Wieso fügst du das Element nicht gleich in der Funktion ein? Dann kannst du dir den Rückgabewert ersparen!<br />
- Wieso verwendest du eine <code>BOOL result</code> Variable? Wieso da mal wieder <code>BOOL</code> statt <code>bool</code> und wieso nicht in der Funktion statt des <code>break</code> lieber gleich ein <code>return true</code> ?<br />
- Rufst du diese updateElement Funktion mehrmals hintereinander auf? Dann wäre das noch extrem ineffizient!</p>
<p>Das wäre es soweit einmal von mir. Mehr fällt mir jetzt grad nicht ein an Fragen. Ich persönlich würde alles in deinem Code umschreiben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608895</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 02 Nov 2008 11:34:46 GMT</pubDate></item><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sun, 02 Nov 2008 14:50:29 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>danke für die Tipps. Nun funktioniert es.</p>
<p>Habe es so gelöst:</p>
<pre><code class="language-cpp">bool updateElement(TiXmlElement element, TiXmlDocument &amp;doc)
{

	bool result = false;

	TiXmlNode * iNode = doc.FirstChild( &quot;root&quot; );
	TiXmlNode * itemNode = iNode-&gt;FirstChild( &quot;Icon&quot; );

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

			string temp1 = itemNode-&gt;ToElement()-&gt;Attribute(&quot;Name&quot;);
			string temp2 = element.ToElement()-&gt;Attribute(&quot;Name&quot;);

			// If the names are equal set the values to the passed values
			if(strcmp(temp1.c_str(), temp2.c_str()) == 0)
			{

				itemNode-&gt;ToElement()-&gt;SetAttribute(&quot;X&quot;, element.ToElement()-&gt;Attribute(&quot;X&quot;));
				itemNode-&gt;ToElement()-&gt;SetAttribute(&quot;Y&quot;, element.ToElement()-&gt;Attribute(&quot;Y&quot;));
				itemNode-&gt;ToElement()-&gt;SetAttribute(&quot;Z&quot;, element.ToElement()-&gt;Attribute(&quot;Z&quot;));

				result = true;
			}

			itemNode = iNode-&gt;IterateChildren( &quot;Icon&quot;, itemNode );
		}
	}

	return result;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1608964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608964</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Sun, 02 Nov 2008 14:50:29 GMT</pubDate></item><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sun, 02 Nov 2008 15:24:46 GMT]]></title><description><![CDATA[<p>Du hast sicher nochmals die Hälfe der Tipps ausgelassen, deshalb konnte ich jetzt nicht anders ...</p>
<pre><code class="language-cpp">bool updateElement(TiXmlDocument&amp; doc,
                   std::string const&amp; name,
                   std::string const&amp; valueX,
                   std::string const&amp; valueY,
                   std::string const&amp; valueZ)
{
  TiXmlElement* element = doc.FirstChildElement(&quot;root&quot;);

  if(element)
  { element = element-&gt;FirstChildElement(&quot;Icon&quot;); }

  char const* attrName;

  while(0 != element) 
  {
    attrName = element-&gt;Attribute(&quot;Name&quot;);

    // If the names are equal set the values to the passed values 
    if(attrName != 0 &amp;&amp; name == attrName) 
    {
      element-&gt;SetAttribute(&quot;X&quot;, valueX);
      element-&gt;SetAttribute(&quot;Y&quot;, valueY);
      element-&gt;SetAttribute(&quot;Z&quot;, valueZ);

      return true;
    }

    element = element-&gt;NextSiblingElement(&quot;Icon&quot;);
  }

  return false;
}
</code></pre>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608984</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 02 Nov 2008 15:24:46 GMT</pubDate></item><item><title><![CDATA[Reply to Immernoch Probleme mit TinyXML on Sun, 02 Nov 2008 15:29:13 GMT]]></title><description><![CDATA[<p>Naa gut, Danke! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1608987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1608987</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Sun, 02 Nov 2008 15:29:13 GMT</pubDate></item></channel></rss>