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

时间:2017-01-20 13:51:42

标签: c++ dom tinyxml2

我想知道如何更新某个属性的DOM数据?我搜索过但我找不到任何东西。基本上,我有一个名为Hour的属性(例如它' 11:03")我希望将该特定属性的文本更改为" 11:04&# 34;或任何其他不同的文字。

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()是私有的..

1 个答案:

答案 0 :(得分: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