C++ xml库的选择

自从触及xml文件的读写,一直以来都是用的tinyxml2,接口简单,然而近期项目频繁出错,跟踪调试发现,问题出在了xml文件的读写上,当节点数超过百万级别的时候,内存暴增到G的当量,很显然程序会由于内存申请不足崩掉了。果断寻找替代品,百度搜索找到了pugixml开源库,将原有的调用tinyxml2接口的近千行代码替换之后,运行程序,一切顺利,速度感觉还较之前的有所提示。

2者调用区别(被注释屏蔽掉的代码是tinyxml2接口调用部分):

	// xml文件声明
    // tinyxml2::XMLDocument docCalKml;
	pugi::xml_document docCalKml;	

    // xml文件内容编码格式声明
	//tinyxml2::XMLDeclaration *declare = docCalKml.NewDeclaration(_T("xml version=\"1.0\" encoding=\"UTF-8\""));
	//docCalKml.InsertEndChild(declare);
	pugi::xml_node declare = docCalKml.prepend_child(pugi::node_declaration);
	declare.append_attribute("version").set_value("1.0");
	declare.append_attribute("encoding").set_value("UTF-8");

    // 节点值的写入
    //XMLElement* eleSRSId = docCalXml.NewElement(_T("Id"));
	//eleSRSId->SetText(0);
	//eleSRS->InsertEndChild(eleSRSId);
	pugi::xml_node eleSRSId = eleSRS.append_child("Id");
	eleSRSId.append_child(pugi::node_pcdata).set_value("0");

    // 节点值的读取
    //XMLElement* eleOpticalProperties = docOpt.RootElement();// ->FirstChildElement(_T("OpticalProperties"));
	//XMLElement* eleOpticalPropertiesId = eleOpticalProperties->FirstChildElement(_T("Id"));
	//int nOpticalPropertiesId = eleOpticalPropertiesId->IntText();
	pugi::xml_node eleOpticalProperties = docOpt.root().first_child();
	pugi::xml_node eleOpticalPropertiesId = eleOpticalProperties.child("Id");
	int nOpticalPropertiesId = eleOpticalPropertiesId.text().as_int();

    // 文件保存
    //if (tinyxml2::XML_SUCCESS != docCalKml.SaveFile(strCalKmlFile))
	if (!docCalKml.save_file(strCalKmlFile, PUGIXML_TEXT("\t"), pugi::format_default | pugi::format_no_declaration | pugi::format_no_escapes, pugi::encoding_utf8))
		return FALSE;
	else
		return TRUE;

Guess you like

Origin blog.csdn.net/u012156872/article/details/121396322
Recommended