我想知道如何为某个属性更新 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() 是私有(private)的..
最佳答案
的确,您可以使用 SetAttribute
,它接受属性 name 和 value 作为参数。
但是,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
:
关于c++ - 使用 tinyxml-2 在 XML DOM c++ 上更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41765249/