使用tinyxml-2更新XML DOM c ++上的数据

问题描述 投票:2回答:1

我想知道如何更新某个属性的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()是私有的..

c++ dom tinyxml2
1个回答
1
投票

确实,您可以使用接受属性名称和值作为参数的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,这是关键。

该类公开了两种方法:

FindAttribute

你已经发现其中一个被设置为private。但const重载设置为public

Access

© www.soinside.com 2019 - 2024. All rights reserved.