c++domtinyxml2

Updating data on a XML DOM c++ using tinyxml-2


I was wondering how could I update the data on the DOM for a certain attribute? I've searched but I couldn't find anything. Basically, I have an attribute called Hour(for example it's "11:03") and I want the text from that specific attribute to be changed to something like "11:04" or any other different text.

if( strcmp(Code1,Code2) == 0 )
{
    strcpy(New,NewHour);
    Element->FindAttribute("Hour")->SetAttribute(New); // here I want it to be changed in the DOM but I dont know how to do it
}

Later edit: This is what I've tried, but it's telling me FindAttribute() is private..


Solution

  • It is true that you can use SetAttribute which accepts the attribute name and value as parameters.

    However, TinyXml2 does have a methodology for using FindAttribute because I have this code in my application:

    // We need to get the assistant
    const XMLAttribute *pAttrAssistant = const_cast<const XMLElement*>(pStudent)->FindAttribute("Assistant");
    if (pAttrAssistant != nullptr)
    {
        LPCTSTR szAssistant = CA2CT(pAttrAssistant->Value(), CP_UTF8);
        SetStudentInfo(eSchool, eAssign, strStudent, szAssistant, iStudyPoint);
    }
    else
    {
        // TODO: Throw exception if Assistant attribute missing
    }
    

    As you can see, I use the FindAttribute method and I have no compilation errors. If you look closely you will see that I am using const and that is the key.

    The class exposes two methods:

    FindAttribute

    One of them is set to private as you have already found out. But the const overload is set as public:

    Access