tinyxml的封装与使用
2008-12-25 10:33
357 查看
tinyxml是个高效精简的xml解析开源代码.
针对tinyxml直接使用对于对xml不是很熟悉的入门新手来说,有些概念难以理解,因此我将其封装后,供大家使用.
头文件:
#include<string>
#include
"tinyxml.h"
using
namespace std;
class
CXML
{
public:
CXML(void)
{
}
~CXML(void)
{
}
private:
TiXmlDocument m_xml;
TiXmlElement* pElement;
private:
TiXmlElement* getFirstElement(string ElementMark,TiXmlElement*
pcrElement);
public:
//解析xml字符串
int ParseXmlStr(string xmlstr);
//解析xml文件
int ParseXmlFile(string xmlFile);
//根据标签取值
int getFirstElementValue(string ElementMark,string&
value);
//针对同一标签的记录取值,如果返回值是0表明再无此标签内容值可取
int getNextElementValue(string ElementMark,string& value);
//取得属性值
int getElementAttributeValue(string AttributeName,string&
value);
//获取根结点
TiXmlElement* getRootElement();
//返回当前的xml字符串
string getXmlStr();
//清空解析的内容
void Clear();
//添加子节点
TiXmlElement* addXmlRootElement(string
ElementMark);//添加一个根节点
//添加子节点
TiXmlElement* addXmlChildElement(TiXmlElement* pElement,string
ElementMark);
//给节点添加值
void addElementValue(TiXmlElement* pElement,string value);
//添加属性及属性值
void
addXmlAttribute(TiXmlElement* pElement,string AttributeMark,string
value);
//添加声明
void
addXmlDeclaration(string vesion,string encoding,string standalone);
//添加注释
void
addXmlComment(TiXmlElement* pElement,string Comment);
//将xml内容保存到文件
void saveFile(string
FileName);
};
///////////////////实现文件
#include
"XML.h"
int
CXML::ParseXmlFile(string xmlFile)
{
int result=0;
try
{
if(m_xml.LoadFile(xmlFile.c_str()))
result=1;
else
result=0;
}
catch(...)
{
}
return result;
}
int
CXML::ParseXmlStr(std::string xmlStr)
{
int result=0;
if(xmlStr=="")
return 0;
try
{
if(m_xml.Parse(xmlStr.c_str()))
result=1;
else
result=0;
}
catch(...)
{
}
return result;
}
TiXmlElement*
CXML::getFirstElement(string ElementMark,TiXmlElement* pcrElement)
{
TiXmlElement* pElementtmp=NULL;
pElementtmp=pcrElement;
while(pElementtmp)
{
if(strcmp(pElementtmp->Value(),ElementMark.c_str())==0)
{
//printf("%s/r/n",pElementtmp->Value());
return pElementtmp;
}
else
{
TiXmlElement* nextElement=pElementtmp->FirstChildElement();
while(nextElement)
{
//printf("%s/r/n",nextElement->Value());
if(strcmp(nextElement->Value(),ElementMark.c_str())==0)
{
return nextElement;
}
else
{
TiXmlElement* reElement=NULL;
reElement=getFirstElement(ElementMark,nextElement);
if(reElement)
{
return reElement;
}
}
nextElement=nextElement->NextSiblingElement();
}
}
pElementtmp=pElementtmp->NextSiblingElement();
}
return NULL;
}
//根据标签取值
int
CXML::getFirstElementValue(string ElementMark,string& value)
{
int result=0;
if(ElementMark=="")
return 0;
try
{
TiXmlElement* pcrElement=NULL;
pcrElement=m_xml.RootElement();
pcrElement=this->getFirstElement(ElementMark,pcrElement);
if(pcrElement)
{
this->pElement=pcrElement;
value=this->pElement->GetText();
result=1;
}
}
catch(...)
{
}
return result;
}
int
CXML::getNextElementValue(string ElementMark,string& value)
{
value="";
this->pElement=this->pElement->NextSiblingElement(ElementMark.c_str());
if(this->pElement)
{
value=this->pElement->GetText();
return 1;
}
return 0;
}
string
CXML::getXmlStr()
{
string result="";
try
{
TiXmlPrinter printer;
m_xml.Accept(&printer);
result=printer.CStr();
}
catch(...)
{
}
return result;
}
void
CXML::Clear()
{
m_xml.Clear();
}
//添加子节点
TiXmlElement*
CXML::addXmlRootElement(string ElementMark)
{
TiXmlElement* RootElement=new
TiXmlElement(ElementMark.c_str());
m_xml.LinkEndChild(RootElement);
return RootElement;
}
TiXmlElement*
CXML::addXmlChildElement(TiXmlElement* pElement,string ElementMark)
{
if(pElement)
{
TiXmlElement* tempElement=new
TiXmlElement(ElementMark.c_str());
pElement->LinkEndChild(tempElement);
return tempElement;
}
return 0;
}
void
CXML::addElementValue(TiXmlElement *pElement, std::string value)
{
if(pElement)
{
TiXmlText *pContent=new
TiXmlText(value.c_str());
pElement->LinkEndChild(pContent);
}
}
//添加属性及属性值
void CXML::addXmlAttribute(TiXmlElement*
pElement,string AttributeMark,string value)
{
if(pElement)
{
pElement->SetAttribute(AttributeMark.c_str(),value.c_str());
}
}
//添加声明
void CXML::addXmlDeclaration(string vesion,string
encoding,string standalone)
{
TiXmlDeclaration *pDeclaration=new
TiXmlDeclaration(vesion.c_str(),encoding.c_str(),standalone.c_str());
m_xml.LinkEndChild(pDeclaration);
}
//添加注释
void CXML::addXmlComment(TiXmlElement*
pElement,string Comment)
{
if(pElement)
{
TiXmlComment *pComment=new TiXmlComment(Comment.c_str());
pElement->LinkEndChild(pComment);
}
}
TiXmlElement*
CXML::getRootElement()
{
return m_xml.RootElement();
}
//取得属性值
int
CXML::getElementAttributeValue(string AttributeName,string& value)
{
if(this->pElement->Attribute(AttributeName.c_str()))
{
value=this->pElement->Attribute(AttributeName.c_str());
return 1;
}
return 0;
}
void
CXML::saveFile(string FileName)
{
this->m_xml.SaveFile(FileName.c_str());
}
//////////////////////////////////////////
注意:
xml字符串如果不是从文件中读出,那么必须以"/r/n"结束,否则解析失败
针对tinyxml直接使用对于对xml不是很熟悉的入门新手来说,有些概念难以理解,因此我将其封装后,供大家使用.
头文件:
#include<string>
#include
"tinyxml.h"
using
namespace std;
class
CXML
{
public:
CXML(void)
{
}
~CXML(void)
{
}
private:
TiXmlDocument m_xml;
TiXmlElement* pElement;
private:
TiXmlElement* getFirstElement(string ElementMark,TiXmlElement*
pcrElement);
public:
//解析xml字符串
int ParseXmlStr(string xmlstr);
//解析xml文件
int ParseXmlFile(string xmlFile);
//根据标签取值
int getFirstElementValue(string ElementMark,string&
value);
//针对同一标签的记录取值,如果返回值是0表明再无此标签内容值可取
int getNextElementValue(string ElementMark,string& value);
//取得属性值
int getElementAttributeValue(string AttributeName,string&
value);
//获取根结点
TiXmlElement* getRootElement();
//返回当前的xml字符串
string getXmlStr();
//清空解析的内容
void Clear();
//添加子节点
TiXmlElement* addXmlRootElement(string
ElementMark);//添加一个根节点
//添加子节点
TiXmlElement* addXmlChildElement(TiXmlElement* pElement,string
ElementMark);
//给节点添加值
void addElementValue(TiXmlElement* pElement,string value);
//添加属性及属性值
void
addXmlAttribute(TiXmlElement* pElement,string AttributeMark,string
value);
//添加声明
void
addXmlDeclaration(string vesion,string encoding,string standalone);
//添加注释
void
addXmlComment(TiXmlElement* pElement,string Comment);
//将xml内容保存到文件
void saveFile(string
FileName);
};
///////////////////实现文件
#include
"XML.h"
int
CXML::ParseXmlFile(string xmlFile)
{
int result=0;
try
{
if(m_xml.LoadFile(xmlFile.c_str()))
result=1;
else
result=0;
}
catch(...)
{
}
return result;
}
int
CXML::ParseXmlStr(std::string xmlStr)
{
int result=0;
if(xmlStr=="")
return 0;
try
{
if(m_xml.Parse(xmlStr.c_str()))
result=1;
else
result=0;
}
catch(...)
{
}
return result;
}
TiXmlElement*
CXML::getFirstElement(string ElementMark,TiXmlElement* pcrElement)
{
TiXmlElement* pElementtmp=NULL;
pElementtmp=pcrElement;
while(pElementtmp)
{
if(strcmp(pElementtmp->Value(),ElementMark.c_str())==0)
{
//printf("%s/r/n",pElementtmp->Value());
return pElementtmp;
}
else
{
TiXmlElement* nextElement=pElementtmp->FirstChildElement();
while(nextElement)
{
//printf("%s/r/n",nextElement->Value());
if(strcmp(nextElement->Value(),ElementMark.c_str())==0)
{
return nextElement;
}
else
{
TiXmlElement* reElement=NULL;
reElement=getFirstElement(ElementMark,nextElement);
if(reElement)
{
return reElement;
}
}
nextElement=nextElement->NextSiblingElement();
}
}
pElementtmp=pElementtmp->NextSiblingElement();
}
return NULL;
}
//根据标签取值
int
CXML::getFirstElementValue(string ElementMark,string& value)
{
int result=0;
if(ElementMark=="")
return 0;
try
{
TiXmlElement* pcrElement=NULL;
pcrElement=m_xml.RootElement();
pcrElement=this->getFirstElement(ElementMark,pcrElement);
if(pcrElement)
{
this->pElement=pcrElement;
value=this->pElement->GetText();
result=1;
}
}
catch(...)
{
}
return result;
}
int
CXML::getNextElementValue(string ElementMark,string& value)
{
value="";
this->pElement=this->pElement->NextSiblingElement(ElementMark.c_str());
if(this->pElement)
{
value=this->pElement->GetText();
return 1;
}
return 0;
}
string
CXML::getXmlStr()
{
string result="";
try
{
TiXmlPrinter printer;
m_xml.Accept(&printer);
result=printer.CStr();
}
catch(...)
{
}
return result;
}
void
CXML::Clear()
{
m_xml.Clear();
}
//添加子节点
TiXmlElement*
CXML::addXmlRootElement(string ElementMark)
{
TiXmlElement* RootElement=new
TiXmlElement(ElementMark.c_str());
m_xml.LinkEndChild(RootElement);
return RootElement;
}
TiXmlElement*
CXML::addXmlChildElement(TiXmlElement* pElement,string ElementMark)
{
if(pElement)
{
TiXmlElement* tempElement=new
TiXmlElement(ElementMark.c_str());
pElement->LinkEndChild(tempElement);
return tempElement;
}
return 0;
}
void
CXML::addElementValue(TiXmlElement *pElement, std::string value)
{
if(pElement)
{
TiXmlText *pContent=new
TiXmlText(value.c_str());
pElement->LinkEndChild(pContent);
}
}
//添加属性及属性值
void CXML::addXmlAttribute(TiXmlElement*
pElement,string AttributeMark,string value)
{
if(pElement)
{
pElement->SetAttribute(AttributeMark.c_str(),value.c_str());
}
}
//添加声明
void CXML::addXmlDeclaration(string vesion,string
encoding,string standalone)
{
TiXmlDeclaration *pDeclaration=new
TiXmlDeclaration(vesion.c_str(),encoding.c_str(),standalone.c_str());
m_xml.LinkEndChild(pDeclaration);
}
//添加注释
void CXML::addXmlComment(TiXmlElement*
pElement,string Comment)
{
if(pElement)
{
TiXmlComment *pComment=new TiXmlComment(Comment.c_str());
pElement->LinkEndChild(pComment);
}
}
TiXmlElement*
CXML::getRootElement()
{
return m_xml.RootElement();
}
//取得属性值
int
CXML::getElementAttributeValue(string AttributeName,string& value)
{
if(this->pElement->Attribute(AttributeName.c_str()))
{
value=this->pElement->Attribute(AttributeName.c_str());
return 1;
}
return 0;
}
void
CXML::saveFile(string FileName)
{
this->m_xml.SaveFile(FileName.c_str());
}
//////////////////////////////////////////
注意:
xml字符串如果不是从文件中读出,那么必须以"/r/n"结束,否则解析失败
相关文章推荐
- tinyxml的封装与使用
- tinyxml的封装与使用
- tinyxml的封装与使用
- 转贴-有关TinyXML使用的简单总结
- tinyxml使用
- 有关TinyXML使用的简单总结:来解析XML数据
- tinyxml的安装和使用
- 对tinyxml操作的常用接口封装——CUDxml
- 使用封装的XML类操作XML文档
- C++之tinyXML使用
- 使用SAX解析XML封装实体Bean 推荐
- (转载)C++之tinyXML使用
- C++之tinyXML使用
- tinyxml 使用指导
- tinyxml在linux和windows下的编译及使用详解
- XML解析器(TinyXML)的使用
- 使用Tinyxml.lib链接出错问题解决 (LIBCMT.lib(sprintf.obj) : error LNK2005: _sprintf_s 已经在 msvcrtd.lib(MSVCR80D.dll) 中定义)
- C++之tinyXML使用浅谈
- TinyXML使用
- C++之tinyXML使用 .