MFC+D3D+ToolKitPro

MFC+D3D+ToolKitPro

C++博客 首页 新随笔 联系 聚合 管理
  3 Posts :: 0 Stories :: 9 Comments :: 0 Trackbacks

由于最近写工具要使用xml来存储脚本,所以看了一些xml的c++相关内容
网上好多关于tinyxml的文档看了好多总是觉得比较麻烦,所以决定自己写一个类来封装它
这个类封装的不是很全面,但已经基本够我使用了,有兴趣的朋友可以再继续完善他,
让不懂xml内部原理的朋友们也可以方便使用xml格式文件存储数据

这是测试项目,vc71版本,我喜欢用2003,哈哈
/Files/hwawai/CXML_vc71.7z
如果大家有什么更好的代码来处理xml希望踊跃交流

头文件
#pragma once
#include<string>
#include "tinyxml.h"
using namespace std;
class CXML
{
public:
 CXML(void);
 ~CXML(void);
 
 bool ParseXmlFile(const char* xmlFile);
 TiXmlElement* GetElement(const char* parentTitle,const char* title);//此函数需一层一层递进
 bool getElementAttributeValue(TiXmlElement* Element,const char* AttributeName,string& reslut);
 bool getFirstElementValue(const char* title,string& result);
 bool getNextElementValue(const char* title,string& result);
 TiXmlElement* getRootElement();
 void Clear();
 //////////////////////////////////////////////////////////////////////////
 TiXmlElement* addXmlRootElement(const char* title);
 TiXmlElement* addXmlChildElement(TiXmlElement* pElement,const char* title);
 void addXmlAttribute(TiXmlElement* pElement,const char* name,const char* value);
 void addXmlDeclaration(const char* vesion="1.0",const char* encoding="gb2312",const char* standalone="");
 void addElementValue(TiXmlElement* pElement,const char* value);
 void addXmlComment(TiXmlElement* pElement,const char* Comment);
 void saveFile(const char* file);
protected:
 TiXmlDocument m_xml;
 TiXmlElement* pElement;   // 获取NextElementValue使用,属临时变量
 TiXmlElement* getFirstElement(const char* ElementMark,TiXmlElement* pcrElement);
};

源文件
#include "StdAfx.h"
#include ".\xml.h"

CXML::CXML(void)
{
}

CXML::~CXML(void)
{
}

bool CXML::ParseXmlFile(const char* xmlFile)
{
 return m_xml.LoadFile(xmlFile)?1:0;
}

TiXmlElement* CXML::GetElement(const char* parentTitle,const char* title)
{
 TiXmlNode* _=m_xml.FirstChildElement(parentTitle);
 for(_=_->FirstChild();_;_=_->NextSibling())
 {
  if (!strcmp(title,_->Value()))
  {
   return _->ToElement();
  }
 }
 return 0;
}

bool CXML::getElementAttributeValue(TiXmlElement* Element,const char* AttributeName,string& reslut)
{
 if(Element->Attribute(AttributeName))
 {
  reslut=Element->Attribute(AttributeName);
  return 1;
 }
 return 0;
}

bool CXML::getFirstElementValue(const char* title,string& result)
{
 if(!title)
  return 0;
 TiXmlElement* _(0);
 _=m_xml.RootElement();
 _=getFirstElement(title,_);
 if(_)
 {
  pElement=_;
  result=pElement->GetText();
  return 1;
 }
 return 0;
}

bool CXML::getNextElementValue(const char* title,string& result)
{
 result="";
 pElement=pElement->NextSiblingElement(title);
 if(pElement)
 {
  result=pElement->GetText();
  return 1;
 }
 return 0;
}

TiXmlElement* CXML::getRootElement()
{
 return m_xml.RootElement();
}

void CXML::Clear()
{
 m_xml.Clear();
}

//////////////////////////////////////////////////////////////////////////
TiXmlElement* CXML::addXmlRootElement(const char* title)
{
 TiXmlElement* _=new TiXmlElement(title);
 m_xml.LinkEndChild(_);
 return _;
}

TiXmlElement* CXML::addXmlChildElement(TiXmlElement* pElement,const char* title)
{
 if(pElement)
 {
  TiXmlElement* _=new TiXmlElement(title);
  pElement->LinkEndChild(_);
  return _;
 }
 return 0;
}

void CXML::addXmlAttribute(TiXmlElement* pElement,const char* name,const char* value)
{
 if(pElement)
 {
  pElement->SetAttribute(name,value);
 }
}

void CXML::addXmlDeclaration(const char* vesion,const char* encoding,const char* standalone)
{
 TiXmlDeclaration *_=new TiXmlDeclaration(vesion,encoding,standalone);
 m_xml.LinkEndChild(_);
}

void CXML::addElementValue(TiXmlElement *pElement,const char* value)
{
 if(pElement)
 {
  TiXmlText *_=new TiXmlText(value);
  pElement->LinkEndChild(_);
 }
}
 
void CXML::addXmlComment(TiXmlElement* pElement,const char* Comment)
{
 if(pElement)
 {
  TiXmlComment *_=new TiXmlComment(Comment);
  pElement->LinkEndChild(_);
 }
}

void CXML::saveFile(const char* file)
{
 m_xml.SaveFile(file);
}

//////////////////////////////////////////////////////////////////////////
TiXmlElement* CXML::getFirstElement(const char* ElementMark,TiXmlElement* pcrElement)
{
 TiXmlElement* _=pcrElement; 
 while(_)
 {
  if(strcmp(_->Value(),ElementMark)==0)
  {
   //printf("%s\r\n",pElementtmp->Value());
   return _;
  }
  else
  {
   TiXmlElement* nextElement=_->FirstChildElement();
   while(nextElement)
   {
    //printf("%s\r\n",nextElement->Value());
    if(strcmp(nextElement->Value(),ElementMark)==0)
    {
     return nextElement;
    }
    else
    {
     TiXmlElement* reElement=NULL;
     reElement=getFirstElement(ElementMark,nextElement);
     if(reElement)
     {
      return reElement;
     }
    }
    nextElement=nextElement->NextSiblingElement();
   }
  }
  _=_->NextSiblingElement();
 }
 return NULL;
}


stdafx文件
#pragma once
#include <iostream>
#include <tchar.h>

用来测试的主cpp文件
#include "stdafx.h"
#include "tinyxml//XML.h"
#include <iostream>

void createXML()
{
 CXML xml;
 xml.addXmlDeclaration("1.0","gb2312","");
 TiXmlElement* root=xml.addXmlRootElement("fields");
 TiXmlElement* pElement=xml.addXmlChildElement(root,"pos");
 xml.addXmlAttribute(pElement,"x","100");
 xml.addXmlAttribute(pElement,"y","200.1");
 xml.addXmlAttribute(pElement,"z","0.123");

 TiXmlElement* pElement2=xml.addXmlChildElement(root,"dest");
 xml.addXmlAttribute(pElement2,"x","一二三");
 xml.addXmlAttribute(pElement2,"y","一二");
 xml.addXmlAttribute(pElement2,"z","一");
 xml.saveFile("1.xml");
}

void CreateXML1()
{
 CXML xml;
 xml.addXmlDeclaration();
    TiXmlElement* root=xml.addXmlRootElement("fields");
 xml.addXmlComment(root,"AAAAAAA");
 TiXmlElement* pElement=xml.addXmlChildElement(root,"pos_x");
 xml.addElementValue(pElement,"1.3");
 pElement=xml.addXmlChildElement(root,"pos_x");
 xml.addElementValue(pElement,"30.1");
 pElement=xml.addXmlChildElement(root,"pos_x");
 xml.addElementValue(pElement,"30ssss.1");
 xml.saveFile("2.xml");
}


void LoadXML()
{
 CXML xml;
 xml.ParseXmlFile("1.xml");
 string a;
 TiXmlElement* pElement=xml.GetElement("fields","dest");
   xml.getElementAttributeValue(pElement,"x",a);
  cout<<a<<endl;
  xml.getElementAttributeValue(pElement,"y",a);
  cout<<a<<endl;
  xml.getElementAttributeValue(pElement,"z",a);
  cout<<a<<endl;
}

void LoadXML1()
{
 CXML xml;
 xml.ParseXmlFile("2.xml");
 string a;
 xml.getFirstElementValue("pos_x",a);
 cout<<a<<endl;
 xml.getNextElementValue("pos_x",a);
  cout<<a<<endl;
 xml.getNextElementValue("pos_x",a);
 cout<<a<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
//  createXML();
//  LoadXML(); 
  CreateXML1();
  LoadXML1(); 
 getchar();
 return 0;
}


生成的xml文件
1.xml
<?xml version="1.0" encoding="gb2312" ?>
<fields>
    <pos x="100" y="200.1" z="0.123" />
    <dest x="一二三" y="一二" z="一" />
</fields>

2.xml
<?xml version="1.0" encoding="gb2312" ?>
<fields>
    <!--AAAAAAA-->
    <pos_x>1.3</pos_x>
    <pos_x>30.1</pos_x>
    <pos_x>30ssss.1</pos_x>
</fields>

posted on 2008-08-21 15:45 hwawai 阅读(3016) 评论(6)  编辑 收藏 引用

评论

# re: 关于tinyXML库的封装---我写的CXML类 2008-08-21 17:56 Condor
晕,不是有老外写过tinyxml for C++的了嘛?  回复  更多评论
  

# re: 关于tinyXML库的封装---我写的CXML类 2008-08-21 19:25 沈臻豪(foxtail)
这年头大家都喜欢自己封装 呵呵  回复  更多评论
  

# re: 关于tinyXML库的封装---我写的CXML类 2008-08-21 20:10 cexer
tinyxml对中文的支持太别扭了。  回复  更多评论
  

# re: 关于tinyXML库的封装---我写的CXML类 2008-08-22 00:33 theanswerzju
... 你封装的越多 可控的粒度就越粗了 像你这样为tinyXML再次封装明显没有什么意义  回复  更多评论
  

# re: 关于tinyXML库的封装---我写的CXML类 2008-08-24 15:46 戴尔笔记本
xml格式文件存储数据倒没问题,但代码读起来还是挺费劲。
  回复  更多评论
  

# re: 关于tinyXML库的封装---我写的CXML类 2009-06-05 11:41 kroody
您好!
在使用tinyxml的时候碰到一个问题想请教下您, 像这种嵌套形式的 怎么读? 比如我要读第二组中的 index Value怎么实现呢?
假如我事先不知道有多少层呢? 递归来做么?
hedybest#163.com

非常感谢!
<?xml version="1.0" ?>
<xmlRoot>
<aaa>
<Count>50</Count>
<Desc>dog</Desc>
<Item>
<Index>1111</Index>
<Value>222</Value>
</Item>
<Items0>
<Index>-858993460</Index>
<Value>-107374176.000000</Value>
</Items0>
<Items1>
<Index>-858993460</Index>
<Value>-107374176.000000</Value>
</Items1>
<Items2>
<Index>-858993460</Index>
<Value>-107374176.000000</Value>
</Items2>
<Items3>
<Index>-858993460</Index>
<Value>-107374176.000000</Value>
</Items3>
</aaa>
</xmlRoot>  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理