Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When TinyXML visitor function returns false, why does it stop parsing siblings?

Tags:

c++

tinyxml

We have adopted Tiny XML as our XML parser. I am writing code to grab palettes out of an XML file, and wrote a visitor function like this:

PALETTE_PARSER::VisitEnter( const TiXmlElement& Element, const TiXmlAttribute* First Attribute)
{
    if( Element.ValueStr() == "palette" )
    {
        AddPalette( Element );
        return( true );
    }
    else
    {
        return( false );
    }
}

I found to my surprise that this parsed the first palette element, and then stopped. When I checked, the documentation said

If you return 'true' from a Visit method, recursive parsing will continue. If you return false, no children of this node or its sibilings will be Visited.

It makes sense to me not to parse the children, but the siblings seems weird to me. What is the reason for this behavior? Is there any way to get it to do what I want?

That is, I am interested only in the palette elements, but there may be more than one of them (as well as other elements). I wanted to return false to skip the other element types rather than having to process them recursively, while still finding all the palettes. So I guess I am looking for a way to visit only the palette elements, while completely ignoring everything else.

like image 385
andrewdski Avatar asked Sep 12 '26 13:09

andrewdski


1 Answers

What is the reason for this behavior?

The purpose is to allow you to search a tree of nodes recursively, and stop as soon as you have found whatever it is you are looking for.

I am interested only in the palette elements, but there may be more than one of them (as well as other elements)... So I guess I am looking for a way to visit only the palette elements, while completely ignoring everything else.

Further discussion implies that all the <palette> elements are children of some specific node in the tree.

Visit is for examining the entire (sub)tree, under the assumption that the nodes you're interested in (<palette> elements in this case) might be found at any depth.

If that's not the case, then it's not the tool for the job.

You can use FirstChild/LastChild/IterateChildren/etc. member functions to iterate over the children of whichever node it is that contains all the <palette> elements, and do whatever you need to do with the nodes that actually are (upon inspection) <palette> elements, and ignore the others.

like image 100
Karl Knechtel Avatar answered Sep 15 '26 02:09

Karl Knechtel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!