我想知道如何为某个属性更新DOM上的数据?我搜过了,但什么也找不到。基本上,我有一个名为Hour的属性(例如,它是"11:03"),我希望将该特定属性中的文本更改为类似于"11:04“或任何其他不同的文本。
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
}稍后编辑:这是我尝试过的,但它告诉我FindAttribute()是私有的。
发布于 2018-03-01 08:50:25
确实,您可以使用SetAttribute,它接受属性、名称、和值作为参数。
但是,TinyXml2确实有一种使用FindAttribute的方法,因为我的应用程序中有以下代码:
// 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
}如您所见,我使用FindAttribute方法,没有编译错误。如果您仔细观察,您会发现我正在使用const,这是关键。
类公开了两种方法:

其中一个设置为private,正如您已经发现的那样。但是const重载被设置为public

https://stackoverflow.com/questions/41765249
复制相似问题