如何在加载较大的文件时使用Tinyxml中的Loadfile()来加速

How to speed up when load a larger file use Loadfile() in Tinyxml

本文关键字:中的 Tinyxml Loadfile 加速 加载 文件      更新时间:2023-10-16

在我的项目中,我需要追加新的数据到xml,所以我这样做如下:问题是LoadFile()函数很慢,当xml文件很大,高cpu时,SaveFile()也有同样的问题。所以,我应该如何加快我的项目。谢谢你的帮助:)

        TiXmlDocument doc(m_filePath.c_str()); 
    (void)doc.LoadFile(); //here is slowly
    if (doc.Error() && (doc.ErrorId()==TiXmlBase::TIXML_ERROR_OPENING_FILE))
    {
        ATS_LOG(ERROR, "Can not open the file:%s", m_filePath.c_str());
        result = false; 
    }
    else
    {
        const TiXmlHandle docH(&doc); 
        TiXmlElement* const element = docH.FirstChildElement("HistoryMsgs").Element();
        TiXmlNode* const pNode=element->ToElement();
        if (pNode!=NULL)
        {
                            //do something that insert new node;
            (void)doc.SaveFile(m_filePath.c_str());//here is slowly too
        }
    }

TinyXML有一些性能问题。RapidXML和PugiXML是首选。我不确定将代码移植到新的解析器是否容易,但是我曾经在使用TinyXML时遇到性能问题,然后切换到PugiXML。您可以查看有关c++解析器的讨论:c++最好的开放XML解析器是什么?