EDIT: I figured it out, look towards the bottom half of this
Before I ask my question, I will state I have successfully used TBXML and RapidXML. Specifically Rapid has parsed this very same document successfully. I'm familiar with XML reading and assigning values.
That said, TinyXML and Cocos seems to be a good combination, as it compiles to android. I can go down the nodes ONCE, print out values, see where I am... the problem is this:
node = node->nextSibling();
Always returns false. I've learned that I can do this:
node->Parent()->NextSibling(); and this will give me the next sibling instead. But here's a problem:
ParentNode->FirstChild() will return the first element of that tree. But if I call ParentNode->NextSibling(), for some reason it returns a pointer to the sibling, except it's now one level higher on the tree.
This is going to make looping a semi nightmare.
I'm scouring http://grinninglizard.com/tinyxml2docs/classtinyxml2_1_1_x_m_l_node.html to find differences between using node, or element styles (eg: NextSiblingElement() ) but at best, I get one loop through with a failure, or just a crash.
This parser doesn't work like the others, or I have the conventions wrong. I've placed cout's everywhere to see where I am, and the initial runs seem fine. Specifically NextSibling is causing issues, and I think I just don't know the tree structure that they "intend" to use.
This is my nested loop as of now:
for( node = node->FirstChild(); node; node = node->NextSibling() ) {
cout << endl << node->Parent()->Value();
for( tempElement = node->FirstChildElement(); tempNode; tempElement = tempElement->NextSibling() ) {
//assign inner values
}
//take the inner values and assign them to an object
}
So, while my code matches all of the suggestions for traversing sibling elements, it's failing. I could use some help from someone who's used this, as what I did in 30 minutes with every other system, is costing hours.
Also, I'm sure it'll make more sense once "their way" is understood, but I can't find one working example on the internet of doing this. Plenty of ones suggesting the sample code I have (not working).
For the most part, TinyXML does find nodes and values, and if you remember that to get the type, you have to use node->Parent()->Value(), then knowing where you are in a tree doesn't seem too hard.
Just for kicks, here's the load, which is working:
if (doc.LoadFile(xmlFilePath.c_str()) == XML_SUCCESS)
{
XMLNode *node = doc.RootElement();
node = node->FirstChild(); //root
//....
Thanks for any assistance! Would be greatly appreciated, and perhaps the only TinyXML2 question with a working answer for future askers, at least at this point in time.
EDIT: I can't answer yet, but I figured out:
For the love of... I hope I'm doing something wrong, if you're doing this, pay attention to this pattern, it works (Probably same for TinyXML2, I'm guessing, just replace TiXmlNode with XMLNode):
note: node refers to the root.
Essentially, think of the NextSibling as a function on the parent, where it's laying out "who's next".
Thanks for the visits so far. This traverses at least to 6 items (outer ones, yay!), then I'm sure the bug I'm having now is my own.
This should help somebody, if anyone else needs to traverse a tree dynamically.
TiXmlNode * subNode = node->FirstChild();
for(subNode = node ; subNode; subNode = subNode->Parent()->NextSibling())
{
subNode = subNode->FirstChild();
cout << endl << "Parent " << subNode->Parent()->Value() << endl;
for( tempNode = subNode; tempNode; tempNode = tempNode->Parent()->NextSibling() )
{
tempNode = tempNode->FirstChild();
The key is this line: Essentially, think of the NextSibling as a function on the parent, where it's laying out "who's next".
While every single XML Parser I've used before hand means "Sibling of current node", TinyXML says "Sibling of the child".
It seems completely natural once I get used to it, but it still throws me for a crashing loop.
Example code is in the bottom half of the post. This answer's existence is explained in the comments of such.
See you