tinyxml使用

现在越来越多的数据和配置采用了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;  
      
}  

bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::map<std::string,std::string> &AttMap)  
{  
    // 定义一个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;  
        }  
        // 设置节点的属性值,然后插入节点  
        std::map<std::string,std::string>::iterator iter;  
        for (iter=AttMap.begin();iter!=AttMap.end();iter++)  
        {  
             pNewNode->SetAttribute(iter->first,iter->second);  
        }  
        pNode->InsertEndChild(*pNewNode);  
        pDoc->SaveFile(XmlFile);  
        return true;  
    }  
    else  
        return false;  
}  

如果你想了解更多的操作以及细化,请参看参考文档了。

  • 0
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
TinyXML是一个小型的、简单易用的XML解析器,适用于C++环境。它的主要特点是简单易用、轻量级、跨平台、可移植性强。本文将介绍TinyXML使用方法。 ## TinyXML安装 TinyXML安装非常简单,只需要下载TinyXML的源代码,并将其包含到你的项目中即可。TinyXML的源代码可以在其官方网站(http://www.grinninglizard.com/tinyxml/)上下载。 ## TinyXML的基本用法 TinyXML使用非常简单,只需要包含TinyXML头文件,并使用其提供的API即可完成XML文件的解析和生成。 ### 解析XML文件 以下是一个简单的XML文件example.xml: ``` <?xml version="1.0" encoding="UTF-8"?> <root> <person> <name>John</name> <age>20</age> </person> <person> <name>Jane</name> <age>21</age> </person> </root> ``` 我们可以使用如下代码来解析该XML文件: ```c++ #include "tinyxml.h" #include <iostream> using namespace std; int main() { TiXmlDocument doc("example.xml"); bool loadOkay = doc.LoadFile(); if (loadOkay) { TiXmlNode* root = doc.FirstChild("root"); if (root) { for (TiXmlNode* node = root->FirstChild(); node; node = node->NextSibling()) { if (node->Type() == TiXmlNode::TINYXML_ELEMENT && strcmp(node->Value(), "person") == 0) { TiXmlNode* nameNode = node->FirstChild("name"); TiXmlNode* ageNode = node->FirstChild("age"); if (nameNode && ageNode) { cout << "name:" << nameNode->FirstChild()->Value() << endl; cout << "age:" << ageNode->FirstChild()->Value() << endl; } } } } } return 0; } ``` 上述代码首先创建了一个TiXmlDocument对象,并调用其LoadFile方法加载example.xml文件。如果加载成功,则获取XML文档的根节点,并循环遍历根节点的所有子节点,查找名称为“person”的节点,并获取其下的“name”和“age”子节点的值。 ### 生成XML文件 我们可以使用TinyXML来生成XML文件。以下是一个简单的示例: ```c++ #include "tinyxml.h" #include <iostream> using namespace std; int main() { TiXmlDocument doc; TiXmlElement* root = new TiXmlElement("root"); doc.LinkEndChild(root); for (int i = 0; i < 3; i++) { TiXmlElement* person = new TiXmlElement("person"); root->LinkEndChild(person); TiXmlElement* name = new TiXmlElement("name"); person->LinkEndChild(name); TiXmlText* nameText = new TiXmlText("John"); name->LinkEndChild(nameText); TiXmlElement* age = new TiXmlElement("age"); person->LinkEndChild(age); TiXmlText* ageText = new TiXmlText("20"); age->LinkEndChild(ageText); } doc.SaveFile("example.xml"); return 0; } ``` 上述代码创建了一个TiXmlDocument对象,并创建了一个名为“root”的根节点。然后循环生成三个名为“person”的节点,并为每个节点添加名为“name”和“age”的子节点,并设置其值。最后,调用TiXmlDocument的SaveFile方法将生成的XML文件保存到example.xml中。 ## 小结 TinyXML是一个简单易用的XML解析器,适用于C++环境,其主要特点是简单易用、轻量级、跨平台、可移植性强。使用TinyXML可以轻松地解析和生成XML文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值