1.下载地址:http://www.grinninglizard.com/tinyxml/ (使用TinyXML只需要将其中的6个文件拷贝到项目中就可以直接使用了,这六个文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。)
2. 解析XML文件:
文件示例:
文件:test.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<setting>
<startTime>2018-7-5 10:00:00</startTime>
<cycle>24</cycle>
</setting>
<FrequentAlarmStatistics>
<Tconfig>180</Tconfig>
<Lconfig>3</Lconfig>
</FrequentAlarmStatistics>
<config>
<Tconfig>60</Tconfig>
<Lconfig>2</Lconfig>
</config>
</config>
读取XML代码:
bool loadXML ()
{
TiXmlDocument doc;
doc.LoadFile ("./test.xml");
if ( doc.Error() )
{
printf ("./test.xml open error\n");
return false;
}
TiXmlElement* eleConfig = doc.FirstChildElement ("config");
if ( !eleConfig )
return false;
TiXmlElement* pConfig = eleConfig->FirstChildElement("config");
if (pConfig)
{
TiXmlElement* pTimeLimit = pConfig->FirstChildElement("Tconfig");
if (pTimeLimit)
{
m_nTconfig = atoi(pTimeLimit->GetText());
}
TiXmlElement* pThreshold = pConfig->FirstChildElement("Lconfig");
if (pThreshold)
{
std::string strValue = pThreshold->GetText();
m_nLconfig = atoi(strValue.c_str());
}
}
}
小提示:一开始用这个tinyxml解析遍历xml的时候不知道啥快捷方法,然后就用了for循环去查找自己想找的字段,后来学习到了可以使用FirstChildElement这个方法,该方法快捷、方便。在使用的过程中要注意判断指针是否为空。
3.保存信息到XML
/**
* @func: WriteCablesInfoToXML
* @brief: 将cable信息输入到xml文件中
* @author: xxx 2020.05.11
* @param: void
* @return: void
*/
void XXX::WriteCablesInfoToXML(std::vector<CableInfo> cablesInfo)
{
TiXmlDocument doc;
//声明XML
TiXmlDeclaration* m_pDeclaration = new TiXmlDeclaration("1.0", "UTF-8", "");
doc.LinkEndChild(m_pDeclaration);
//创建根节点
TiXmlElement* cables = new TiXmlElement("cables");
cables->SetAttribute("cables count", cablesInfo.size());
//关联XML文档,成为XML文档的根节点
doc.LinkEndChild(cables);
for (int i = 0; i < cablesInfo.size(); i++)
{
std::string cableNum = "cable ";
cableNum.append(std::to_string(i));
TiXmlElement* cable = new TiXmlElement(cableNum.c_str());
cable->SetAttribute("cableName", cablesInfo[i].cable.c_str());
cable->SetAttribute("cableState", cablesInfo[i].cableState);
cable->SetAttribute("sendIed", cablesInfo[i].sendIedName.c_str());
cable->SetAttribute("sendport", cablesInfo[i].sendPort.c_str());
cable->SetAttribute("sendPortState", cablesInfo[i].sendPortState);
cable->SetAttribute("recvIedName", cablesInfo[i].recvIedName.c_str());
cable->SetAttribute("recvPort", cablesInfo[i].recvPort.c_str());
cable->SetAttribute("recvPortState", cablesInfo[i].recvPortState);
std::string appids = "";
for (int j = 0; j < cablesInfo[i].virtualAppidVec.size(); j++)
{
appids.append(" ").append(std::to_string(cablesInfo[i].virtualAppidVec[j]));
}
cable->SetAttribute("virtualAppid", appids.c_str());
cables->LinkEndChild(cable);
TiXmlElement* links = new TiXmlElement("links");
cable->LinkEndChild(links);
for (int j = 0; j < cablesInfo[i].links.size(); j++)
{
TiXmlElement* link = new TiXmlElement("link");
links->LinkEndChild(link);
link->SetAttribute("SendIed", cablesInfo[i].links[j].szSendIed);
link->SetAttribute("RecvIed", cablesInfo[i].links[j].szRecvIed);
link->SetAttribute("Appid", std::to_string(cablesInfo[i].links[j].nAppid).c_str());
link->SetAttribute("linkState", cablesInfo[i].links[j].linkState);
link->SetAttribute("DoName", cablesInfo[i].links[j].szDoName);
//link->SetAttribute("DoDesc", cablesInfo[i].links[j].szDoDesc);
}
}
doc.SaveFile("log/cablesInfo.xml");
}
小提示:在保存的时候要注意声明XML,这样有利于其他人解析。在组建树的时候使用LinkEndChild即可,最后记得使用SaveFile进行保存数据。
3. 修改XML节点
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<setting>
<startTime>2018-7-5 10:00:00</startTime>
<cycle>24</cycle>
</setting>
<runningInspection>
<startTime>2018-7-5 11:25:00</startTime>
<cycle>48.00</cycle>
<checkStartTime>1588150915</checkStartTime>
<checkStartMilliSecondTime>340</checkStartMilliSecondTime>
</runningInspection>
</config>
TiXmlDocument doc;
doc.LoadFile ("./test.xml");
if ( doc.Error() )
{
printf ("./test.xml open error\n");
return false;
}
TiXmlElement* eleConfig = doc.FirstChildElement ("config");
if ( !eleConfig )
return false;
TiXmlElement* eleIntelligentStatistic = eleConfig->FirstChildElement ("intelligentStatistic");
if ( !eleIntelligentStatistic )
return false;
TiXmlElement* eleStartTime = eleIntelligentStatistic->FirstChildElement ("startTime");
if ( !eleStartTime )
return false;
TiXmlElement* eleCycle = eleIntelligentStatistic->FirstChildElement ("cycle");
if ( !eleCycle )
return false;
if ( eleStartTime->FirstChild() )
eleStartTime->RemoveChild(eleStartTime->FirstChild());
if ( eleCycle->FirstChild() )
eleCycle->RemoveChild(eleCycle->FirstChild());
eleStartTime->LinkEndChild((new TiXmlText(startTime)));
eleCycle->LinkEndChild(new TiXmlText(cycle));
doc.SaveFile();