网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何用tinyxml库创建并读写xml代码截取

本篇文章给大家分享的是有关如何用tinyxml 库创建并读写xml代码截取,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站主营耒阳网站建设的网络公司,主营网站建设方案,app开发定制,耒阳h5小程序定制开发搭建,耒阳网站营销推广欢迎耒阳等地区企业咨询

因为自己每次写完之后都忘记了  然后又要从别的地方学习再重新写  还不如记录再这里

//创建
        TiXmlDocument *pXmlDocument = new TiXmlDocument(m_strFilePath.c_str());//
        TiXmlDeclaration *pDeclaretion = new TiXmlDeclaration("1.0", "UTF-8","");//创建xml声明
        pXmlDocument->LinkEndChild(pDeclaretion);
        TiXmlElement *pXmlElement = new TiXmlElement("Symbol");
        pXmlDocument->LinkEndChild(pXmlElement);    
        pXmlDocument->SaveFile(m_strFilePath.c_str());

//读
TiXmlDocument *pXmlDoc = new TiXmlDocument();
    pXmlDoc->LoadFile(strFilePath.c_str());

    TiXmlElement *pRootElement = pXmlDoc->RootElement(); 
    if (pRootElement == NULL)
    {
        return;
    }
    // 第一个子节点  
    TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign"); 
    if (!pSignNode)
    {
        SAFE_DELETE(pSignNode);
        return;
    }
    while (pSignNode != NULL)
    {
        CMapSingleParameter *pMapSingleParameter = new CMapSingleParameter();
        if (!pMapSingleParameter)
        {
            SAFE_DELETE(pMapSingleParameter);
            return ;
        }
        // text
        TCHAR tcharValue[MAX_PATH * 4] = {_T('\0')};

        // name
        const CHAR *pszName = pSignNode->Attribute("name");
        if (pszName != NULL)
        {
            CommonUtil::UTF8ToTCHAR(pszName,tcharValue);
            pMapSingleParameter->AppendParameter(_T("name"), tcharValue);
        }
        
        // height
        const CHAR *pszHeight = pSignNode->Attribute("height");
        if (pszHeight != NULL)
        {
            CommonUtil::UTF8ToTCHAR(pszHeight,tcharValue);
            pMapSingleParameter->AppendParameter(_T("height"), tcharValue);
        }
        //Content
        const CHAR *pszContent = pSignNode->GetText();
        if (pszContent != NULL)
        {
            CommonUtil::UTF8ToTCHAR(pszContent,tcharValue);
            pMapSingleParameter->AppendParameter(_T("content"), tcharValue);
        }

        m_pLatelyVectorSymbol->push_back(pMapSingleParameter);        
        pSignNode = pSignNode->NextSiblingElement();            
    }    



//写
string strFile = m_strFilePath;
    
    //创建文档对象
    TiXmlDocument myXmlDocument;
    //加载文件数据 
    myXmlDocument.LoadFile(strFile.c_str());

    TiXmlElement *pRootElement = myXmlDocument.RootElement(); 
    if (pRootElement == NULL)
    {
        return ;
    }

    if (pRootElement != NULL)
    {
        TiXmlElement *pFirstNode = pRootElement->FirstChildElement("sign");
        if (pFirstNode == NULL)
        {    
            int nSignPos = 0;

            for (TVectorSymbol::iterator ite = m_pLatelyVectorSymbol->begin(); ite != m_pLatelyVectorSymbol->end(); ++ite)
            {
                CMapSingleParameter *pMapSingleParameter = *ite;
                TiXmlElement *insertElement = new TiXmlElement("sign");
                pRootElement->LinkEndChild(insertElement);
                xstring strUnicode = pMapSingleParameter->GetParameterValue(_T("name"));
                TCHAR tcharValue[MAX_PATH*4]= {_T('\0')};
                _tcscpy(tcharValue, strUnicode.c_str());
                char chUtf8[MAX_PATH*4] = {'\0'};
                CommonUtil::TCHARToUTF8(tcharValue, chUtf8);
                insertElement->SetAttribute("name",chUtf8);


                strUnicode = pMapSingleParameter->GetParameterValue(_T("height"));
                _tcscpy(tcharValue, strUnicode.c_str());
                CommonUtil::TCHARToUTF8(tcharValue, chUtf8);
                insertElement->SetAttribute("height",chUtf8);

                strUnicode = pMapSingleParameter->GetParameterValue(_T("content"));
                _tcscpy(tcharValue, strUnicode.c_str());
                CommonUtil::TCHARToUTF8(tcharValue, chUtf8);
                
                TiXmlText *pXmlText = new TiXmlText(chUtf8);
                pXmlText->SetCDATA(true);//添加格式化
                
                insertElement->LinkEndChild(pXmlText);

                m_nLatelyTotal ++;    
            }        
        }            
    }    
    myXmlDocument.SaveFile(strFile.c_str());
}


//删除节点

    string strFile = m_strFilePath;

    //创建文档对象
    TiXmlDocument myXmlDocument;
    //加载文件数据 
    myXmlDocument.LoadFile(strFile.c_str());

    TiXmlElement *pRootElement = myXmlDocument.RootElement(); 
    if (pRootElement == NULL)
    {
        return ;
    }

    TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign");
    while (pSignNode != NULL)
    {
        m_nLatelyTotal --;
        TiXmlElement *pSignRemoveNode = pSignNode;
        pSignNode= pSignNode->NextSiblingElement("sign");
        pRootElement->RemoveChild(pSignRemoveNode);        
    }

    myXmlDocument.SaveFile(strFile.c_str());

以上就是如何用tinyxml 库创建并读写xml代码截取,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章题目:如何用tinyxml库创建并读写xml代码截取
浏览地址:http://bjjierui.cn/article/ppssid.html

其他资讯