VC使用tinyxml 遍历xml文件

#include "tinystr.h"
#include "tinyxml.h"
#ifdef _DEBUG
#pragma comment(lib,"tinyxmld.lib")
#pragma comment(lib,"tinyxmld_STL.lib")
#else
#pragma comment(lib,"tinyxml.lib")
#pragma comment(lib,"tinyxml_STL.lib")
#endif
  
  
string dumpNode(TiXmlNode * pNode, int flag);
  
int _tmain( int argc, _TCHAR* argv[])
{
  
  
     TiXmlDocument *myDocument = new TiXmlDocument( "config_export.xml" );
     myDocument->LoadFile();
  
     //获得xml的头,即声明部分
     TiXmlDeclaration* decl = myDocument->FirstChild()->ToDeclaration();
     cout << "xml文件的版本是:" << decl->Version() << endl;
     cout << "xml的编码格式是:" << decl->Encoding() << endl;
      
     //获得根元素
     TiXmlElement *RootElement = myDocument->RootElement();
  
     //输出根元素名称
     cout << RootElement->Value() << endl;
  
     TiXmlNode* pNode  = NULL;
     string msg = "" ;
  
     for (pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())
     {
         msg += dumpNode(pNode,0);
     }
     cout << msg << endl;
     system ( "pause" );
     return 0;
}
  
  
string dumpNode(TiXmlNode * pNode, int flag)
{   
     string msg = "" ;
  
     if (pNode == NULL)
     {
         return "" ;
     }
     TiXmlText * pText = NULL;
     TiXmlNode * pChildNode = NULL;
     int t = pNode->Type();
     if (t == TiXmlText::TEXT)  //节点类型是text节点
     {
         pText = pNode->ToText();
         string text = pText->Value();
         if (!text.empty())
         {
             msg = msg + "=" + text;
         }
  
  
     }
     else if (t == TiXmlText::ELEMENT) //节点类型是Element
     {
         msg = msg + "rn" ;
         int num = flag;
         while (num >= 1)
         {
             msg = msg + "t" ;
             num--;
         }
  
         msg = msg + pNode->Value();
  
         //输出属性
         TiXmlElement * pElement = pNode->ToElement();
  
         TiXmlAttribute * pAttr = pElement->FirstAttribute();
         TiXmlAttribute * pNextAttr =NULL;
         string tmpAttrMsg = "[" ;
         if (pAttr != NULL)
         {   
             string tmpAttrVal = "" ;
             string tmpAttrName = "" ;
             do
             {                           
                 tmpAttrVal = pAttr->Value();
                 tmpAttrName = pAttr->Name();
                 tmpAttrMsg += tmpAttrName+ "=" + tmpAttrVal+ "," ; //各个属性之间用逗号分隔
             } while (pAttr = pAttr->Next());
             tmpAttrMsg = tmpAttrMsg.erase(tmpAttrMsg.find_last_of( "," ));
         }
         tmpAttrMsg += "]" ;
         msg += tmpAttrMsg;
  
     }
     //循环访问它的每一个元素
     for (pChildNode=pNode->FirstChild();pChildNode!=0;pChildNode = pChildNode->NextSibling())
     {
  
         msg = msg + dumpNode(pChildNode,flag+1);
  
     }
     return msg;
  
}
#include "tinystr.h"
#include "tinyxml.h"
#ifdef _DEBUG
#pragma comment(lib,"tinyxmld.lib")
#pragma comment(lib,"tinyxmld_STL.lib")
#else
#pragma comment(lib,"tinyxml.lib")
#pragma comment(lib,"tinyxml_STL.lib")
#endif
  
  
string dumpNode(TiXmlNode * pNode, int flag);
  
int _tmain( int argc, _TCHAR* argv[])
{
  
  
     TiXmlDocument *myDocument = new TiXmlDocument( "config_export.xml" );
     myDocument->LoadFile();
  
     //获得xml的头,即声明部分
     TiXmlDeclaration* decl = myDocument->FirstChild()->ToDeclaration();
     cout << "xml文件的版本是:" << decl->Version() << endl;
     cout << "xml的编码格式是:" << decl->Encoding() << endl;
      
     //获得根元素
     TiXmlElement *RootElement = myDocument->RootElement();
  
     //输出根元素名称
     cout << RootElement->Value() << endl;
  
     TiXmlNode* pNode  = NULL;
     string msg = "" ;
  
     for (pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())
     {
         msg += dumpNode(pNode,0);
     }
     cout << msg << endl;
     system ( "pause" );
     return 0;
}
  
  
string dumpNode(TiXmlNode * pNode, int flag)
{   
     string msg = "" ;
  
     if (pNode == NULL)
     {
         return "" ;
     }
     TiXmlText * pText = NULL;
     TiXmlNode * pChildNode = NULL;
     int t = pNode->Type();
     if (t == TiXmlText::TEXT)  //节点类型是text节点
     {
         pText = pNode->ToText();
         string text = pText->Value();
         if (!text.empty())
         {
             msg = msg + "=" + text;
         }
  
  
     }
     else if (t == TiXmlText::ELEMENT) //节点类型是Element
     {
         msg = msg + "rn" ;
         int num = flag;
         while (num >= 1)
         {
             msg = msg + "t" ;
             num--;
         }
  
         msg = msg + pNode->Value();
  
         //输出属性
         TiXmlElement * pElement = pNode->ToElement();
  
         TiXmlAttribute * pAttr = pElement->FirstAttribute();
         TiXmlAttribute * pNextAttr =NULL;
         string tmpAttrMsg = "[" ;
         if (pAttr != NULL)
         {   
             string tmpAttrVal = "" ;
             string tmpAttrName = "" ;
             do
             {                           
                 tmpAttrVal = pAttr->Value();
                 tmpAttrName = pAttr->Name();
                 tmpAttrMsg += tmpAttrName+ "=" + tmpAttrVal+ "," ; //各个属性之间用逗号分隔
             } while (pAttr = pAttr->Next());
             tmpAttrMsg = tmpAttrMsg.erase(tmpAttrMsg.find_last_of( "," ));
         }
         tmpAttrMsg += "]" ;
         msg += tmpAttrMsg;
  
     }
     //循环访问它的每一个元素
     for (pChildNode=pNode->FirstChild();pChildNode!=0;pChildNode = pChildNode->NextSibling())
     {
  
         msg = msg + dumpNode(pChildNode,flag+1);
  
     }
     return msg;
  
}
 
我使用的tinyxml是2.6版本的 在官网下载的
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值