您的位置:首页 > 其它

tinyxml使用:创建XML,遍历打印xml文件 select操作,获取单个节点值,删除节点操作,修改节点操作,增加节点操作

2015-03-08 09:01 941 查看
本文转载于:http://blog.csdn.net/A056313/article/details/6663378 感谢作者对我的帮助。

现在越来越多的数据和配置采用了xml格式来存放和进行传输解析了。在c++方面,没有本地支持的库,所以需要我们自己去找一下。微软的msxml说实话,确实不咋地,尤其是com的类型变量名字一直指针,让众人看上去就比较反感。开源的tinyxml在这方便做的还不错。简单介绍下使用过程的一点小经验。

在这里发下牢骚,VC6.0以后的各个版本的VS环境对于C++的智能感知都是那么的SB,不管你怎么配置,怎么google都让你非常抓狂,就是不出来。算了不说了。

从网站上下载tinyxml,下载之后解压打开文件夹,里面有一些测试例子,tinyxml.sln支持vs2010了都,不管这些,怎么需要的是那个xml类库。

使用tinyxml我们只需要

tinyxml.cpp,

tinyxml.h,

tinyxmlerror.cpp,

tinyxmlparser.cpp,

tinystr.cpp,

tinystr.h

6个文件即可。注意一旦少拷贝了其中tinyxmlerror.cpp, tinyxmlparser.cpp, 其中一个或者两个,就会报告link错误,呵呵

在目标源文件的头部,添加 #include"tinyxml.h"和#include "tinystr.h"

我们使用xml文件,无外乎这几种操作,

1. 遍历整个xml返回一棵树select

2. 查找特定节点的属性/文本值select

3.插入特定位置一个节点insert

4.更新特定节点的属性/文本值 update

5.创建xml文件 create

6.who knows

据一位网友的博文里面提到,基本应该涵盖数据库的所有操作,xml操作应该像操作数据库一样。我觉得甚是有道理啊。

那么我来列举下,我搜集以及测试成功的相关的操作代码吧,希望能节省一些大家学习的时间。

1.create xml操作

// 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument;  
    if (NULL==pDoc)  
    {  
        return false;  
    }  

    // 定义一个xml文件头部声明
    TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));  
    if (NULL==pDeclaration)  
    {  
        return false;  
    }
  
    pDoc->LinkEndChild(pDeclaration);  

    // 生成一个根节点:MyApp  
    TiXmlElement *pRootEle = new TiXmlElement(("MyApp"));  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
    pDoc->LinkEndChild(pRootEle);
  
    // 生成子节点:Messages  
    TiXmlElement *pMsg = new TiXmlElement(("Messages"));  
    if (NULL==pMsg)  
    {  
        return false;  
    }  
    pRootEle->LinkEndChild(pMsg);  

    // 生成子节点:Welcome  
    TiXmlElement *pWelcome = new TiXmlElement(("Welcome"));  
    if (NULL==pWelcome)  
    {  
        return false;  
    }  
    pMsg->LinkEndChild(pWelcome);  

    // 设置Welcome节点的值  
    const char* strValue = ("Welcome to MyApp");  
    TiXmlText *pWelcomeValue = new TiXmlText(strValue);  
    pWelcome->LinkEndChild(pWelcomeValue);  

    // 生成子节点:Farewell  
    TiXmlElement *pFarewell = new TiXmlElement(("Farewell"));  
    if (NULL==pFarewell)  
    {  
        return false;  
    }  
    pMsg->LinkEndChild(pFarewell); 
 
    // 设置Farewell节点的值  
    strValue = ("Thank you for using MyApp");  
    TiXmlText *pFarewellValue = new TiXmlText(strValue);  
    pFarewell->LinkEndChild(pFarewellValue);  

    // 生成子节点:Windows  
    TiXmlElement *pWindows = new TiXmlElement(("Windows"));  
    if (NULL==pWindows)  
    {  
        return false;  
    }  
    pRootEle->LinkEndChild(pWindows);  

    // 生成子节点:Window  
    TiXmlElement *pWindow = new TiXmlElement(("Window"));  
    if (NULL==pWindow)  
    {  
        return false;  
    }  
    pWindows->LinkEndChild(pWindow); 
 
    // 设置节点Window的值  
    pWindow->SetAttribute(("name"),("MainFrame"));  
    pWindow->SetAttribute(("x"),("5"));  
    pWindow->SetAttribute(("y"),("15"));  
    pWindow->SetAttribute(("w"),("400"));  
    pWindow->SetAttribute(("h"),("250"));
  
    // 生成子节点:Window  
    TiXmlElement *pConnection  = new TiXmlElement(("Connection"));  
    if (NULL==pConnection)  
    {  
        return false;  
    }  
    pRootEle->LinkEndChild(pConnection);  

    // 设置节点Connection的值  
    pConnection->SetAttribute(("ip"),("192.168.0.1"));  
    pConnection->SetAttribute(("timeout"),("123.456000"));  
    pDoc->SaveFile("1.xml");


2.遍历打印xml文件 select操作

//TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile("1.xml");  
    pDoc->Print();


3.获取单个节点值

bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)  
{  
     // 假如等于根节点名,就退出  
     if (strNodeName==pRootEle->Value())  
     {  
         Node = pRootEle;  
         return true;  
     }  
      TiXmlElement* pEle = pRootEle;    
      for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())    
    {    
          //递归处理子节点,获取节点指针  
          if(GetNodePointerByName(pEle,strNodeName,Node))  
              return true;  
     }    
     return false;  
}


bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText)  
{  
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
   TiXmlElement *pNode = NULL;  
   GetNodePointerByName(pRootEle,strNodeName,pNode);  
   if (NULL!=pNode)  
   {  
        strText = pNode->GetText();   
        return true;  
   }  
   else  
   {  
        return false;  
   }  
      
}


bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap)  
{  
    // 定义一个TiXmlDocument类指针  
    typedef std::pair <std::string,std::string> String_Pair;  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
    TiXmlElement *pNode = NULL;  
    GetNodePointerByName(pRootEle,strNodeName,pNode);  
    if (NULL!=pNode)  
    {  
        TiXmlAttribute* pAttr = NULL;   
        for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())    
        {    
            std::string strAttName = pAttr->Name();  
            std::string strAttValue = pAttr->Value();  
            AttMap.insert(String_Pair(strAttName,strAttValue));  
        }    
        return true;  
    }  
    else  
    {  
        return false;  
    }  
    return true;  
}


4.删除节点操作

bool DelNode(std::string XmlFile,std::string strNodeName)  
{  
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
    TiXmlElement *pNode = NULL;  
    GetNodePointerByName(pRootEle,strNodeName,pNode);  
    // 假如是根节点  
    if (pRootEle==pNode)  
    {  
          if(pDoc->RemoveChild(pRootEle))  
          {  
               pDoc->SaveFile(XmlFile);  
               return true;  
          }  
          else   
              return false;  
    }  
    // 假如是其它节点  
    if (NULL!=pNode)  
    {  
        TiXmlNode *pParNode =  pNode->Parent();  
        if (NULL==pParNode)  
        {  
               return false;  
        }  
              
        TiXmlElement* pParentEle = pParNode->ToElement();  
        if (NULL!=pParentEle)  
        {  
            if(pParentEle->RemoveChild(pNode))  
                 pDoc->SaveFile(XmlFile);  
            else  
                return false;  
        }  
    }  
    else  
    {  
          return false;  
    }  
     return false;  
}


5.修改节点操作
bool ModifyNode_Text(std::string XmlFile,std::string strNodeName,std::string strText)  
{  
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
    TiXmlElement *pNode = NULL;  
    GetNodePointerByName(pRootEle,strNodeName,pNode);  
    if (NULL!=pNode)  
    {  
        pNode->Clear();  // 首先清除所有文本  
        // 然后插入文本,保存文件  
        TiXmlText *pValue = new TiXmlText(strText);  
        pNode->LinkEndChild(pValue);  
        pDoc->SaveFile(XmlFile);  
        return true;  
    }  
    else  
        return false;  
}


bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,  
                 std::map<std::string,std::string> &AttMap)  
{  
    typedef std::pair <std::string,std::string> String_Pair;  
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
   
    TiXmlElement *pNode = NULL;  
    GetNodePointerByName(pRootEle,strNodeName,pNode);  
    if (NULL!=pNode)  
    {  
        TiXmlAttribute* pAttr = NULL;   
        std::string strAttName = _T("");  
        std::string strAttValue = _T("");  
        for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())    
        {    
            strAttName = pAttr->Name();  
            std::map<std::string,std::string>::iterator iter;  
            for (iter=AttMap.begin();iter!=AttMap.end();iter++)  
            {  
                if (strAttName==iter->first)  
                {  
                    pAttr->SetValue(iter->second);  
                }  
            }  
        }    
        pDoc->SaveFile(XmlFile);  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}


6增加节点操作

bool AddNode_Text(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::string strText)  
{  
    // 定义一个TiXmlDocument类指针  
    TiXmlDocument *pDoc = new TiXmlDocument();  
    if (NULL==pDoc)  
    {  
        return false;  
    }  
    pDoc->LoadFile(XmlFile);  
    TiXmlElement *pRootEle = pDoc->RootElement();  
    if (NULL==pRootEle)  
    {  
        return false;  
    }  
    TiXmlElement *pNode = NULL;  
    GetNodePointerByName(pRootEle,strParNodeName,pNode);  
    if (NULL!=pNode)  
    {  
        // 生成子节点:pNewNode  
        TiXmlElement *pNewNode = new TiXmlElement(strNodeName);  
        if (NULL==pNewNode)  
        {  
            return false;  
        }  
        // 设置节点文本,然后插入节点  
        TiXmlText *pNewValue = new TiXmlText(strText);  
        pNewNode->LinkEndChild(pNewValue);  
        pNode->InsertEndChild(*pNewNode);  
        pDoc->SaveFile(XmlFile);  
        return true;  
    }  
    else  
         return false;  
      
}


如果你想了解更多的操作以及细化,请参看参考文档了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐