Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Blindly Parse in TinyXML
    text
    copied!<p>my XML knowledge is enough to get by. </p> <p>What I want to do is blindly parse an XML string and get all of the elements and attributes starting at the root. I can't use recursion like the example in the documentation and I can't seem to grasp how to do it with a loop. I know enough to get the root from the XML string and can loop through the string, but can't seem to get the attributes or text values.</p> <p>I do not know any names of any tags and need to figure them out as I parse. Anyone have any idea how I start or an example?</p> <p>Thanks</p> <p>Here is the code I have so far for a XML string like this, works unless its nested elements like the location and country:</p> <pre><code> string strData = "&lt;MyStuff mystring1=""111"" mystring2=""223""&gt;\ &lt;MYTAG1&gt;0&lt;/MYTAG1&gt;\ &lt;MYTAG2&gt;0&lt;/MYTAG3&gt;\ &lt;MYTAG4&gt;0&lt;/MYTAG4&gt;\ &lt;location&gt;&lt;country&gt;GreatBritain&lt;/country&gt;&lt;/location&gt;\ &lt;/MyStuff&gt;"; void parseXmlString2( TiXmlNode* root ) { TiXmlNode* child; TiXmlElement* elem = (TiXmlElement*)root; TiXmlAttribute* pAttrib = elem-&gt;FirstAttribute(); //This gets the root and its attributes. while(pAttrib) { cout &lt;&lt; "Value: " &lt;&lt; pAttrib-&gt;Name() &lt;&lt; ":" &lt;&lt; pAttrib-&gt;Value() &lt;&lt; endl; pAttrib=pAttrib-&gt;Next(); } //Now I want to parse up the rest. //Does not work if nested such as &lt;location&gt;&lt;country&gt;GreatBritain&lt;/country&gt;&lt;/location&gt; for( child = root-&gt;FirstChild(); child; child = child-&gt;NextSibling() ) { //Test For child. if(child-&gt;FirstChild()) { //My helper fuct to tell type //getType(child-&gt;FirstChild()-&gt;Type()); TiXmlNode* myChild; for( myChild = child-&gt;FirstChild(); myChild; myChild = child-&gt;IterateChildren(child-&gt;FirstChild())) { cout &lt;&lt; " " &lt;&lt; myChild-&gt;Value() &lt;&lt; endl; } } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload