c++tinyxmltinyxml2

What is the equivalent of TiXmlAttribute for tinyxml2 and how to use it?


I've encountered a problem that I'm not being able to solve using tinyxml2.

I have a function that receives as a parameter a XMLElement and I need to iterate over its attributes. With tinyxml, this worked:

void xmlreadLight(TiXmlElement* light){
    for (TiXmlAttribute* a = light->FirstAttribute(); a ; a = a->Next()) {
         //Do stuff
    }
}

Using the same with tinyxml2, like in the example below, I get the following error:

a value of type const tinyxml2::XMLAttribute * cannot be used to initialize an entity of type tinyxml2::XMLAttribute *

void xmlreadLight(XMLElement* light){
    for (XMLAttribute* a = light->FirstAttribute(); a ; a = a->Next()) {
         //Do stuff
    }
}

The XML code in question is:

<lights>
   <light type="POINT" posX=-1.0 posY=1.0 posZ=-1.0 ambtR=1.0 ambtG=1.0 ambtB=1.0 />
</lights>

where light is the XMLElement passed into the function xmlreadLight. Not sure if my question is properly set up, so if theres some info missing, let me know.


Solution

  • Going by the error message, it looks like you need to do:

    for (const XMLAttribute* a = light->FirstAttribute(); a ; a = a->Next()) { ...
         ^^^^^
    

    Presumbably, the return type of FirstAttribute has been made const in tinyxml2.


    If you check the Github repository for the tinyxml2.h file on line 1513 you will see this:

    /// Return the first attribute in the list.
    const XMLAttribute* FirstAttribute() const {
        return _rootAttribute;
    }