Get child count from rapidxml:xmlnode to get random child node



I can turn my XML document file into object:



if(exists("generators.xml")) { //http://ift.tt/15Ycuzn
rapidxml::file<> xmlFile("generators.xml"); // Open file, default template is char

xml_document<> doc; // character type defaults to char
doc.parse<0>(xmlFile.data());; // 0 means default parse flags
xml_node<> *main = doc.first_node(); //Get the main node that contains everything
cout << "Name of my first node is: " << doc.first_node()->name() << "\n";

if(main!=NULL) {
//Get random child node?
}
}


I'd like to pick one random child node from the main object. My XML looks like this (version with comments):



<?xml version="1.0" encoding="windows-1250"?>
<Stripes>
<Generator>
<stripe h="0,360" s="255,255" l="50,80" width="10,20" />
</Generator>
<Generator>
<loop>
<stripe h="0,360" s="255,255" l="50,80" width="10,20" />
<stripe h="0,360" s="255,255" l="0,0" width="10,20" />
</loop>
</Generator>
</Stripes>


I want to pick random <Generator> entry. I think getting the child count would be a way to do it:



//Fictional code - **all** methods are fictional!
unsigned int count = node->child_count();
//In real code, `rand` is not a good way to get anything random
xmlnode<> *child = node->childAt(rand(0, count));


How can I gild count and child at offset from rapidxml node?


No comments:

Post a Comment