使用XML解析器(TinyXML)编程
我是在VC下编译运行。
2.下载后解压这个压缩包,把所有的东西放到一个找的着的地方(比如,E:\开发库\TinyXML)
3.用Visual C++(推荐VC++.NET2003)创建一个新的工程(Win32控制台)
4.在TinyXML的目录里面找到tinystr.h, tinyxml.h, tinystr.cpp, tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp六个文件加入到刚刚创建的项目中去
5.打开tinyxml.h, 在第一行加入下面这行:
#define TIXML_USE_STL //标志使用STL的内容
#define TIXML_USE_STL //标志使用STL的内容
6.然后创建一个cpp文件,输入下面的内容:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "tinyxml.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//原先代码是直接加载XML文件,我作了一下修改,把内容读到字符串后再解析,实际使用时就去掉读取XML文件这一步
string filename = "first.xml";
//TiXmlDocument* doc = new TiXmlDocument(filename.c_str());
//////////////////////////////////////////////////////////////////////////
// 在这里复制文件
//////////////////////////////////////////////////////////////////////////
std::ifstream ifs(filename.c_str());
char buffer[1024];
char c, *p = buffer;
while(ifs.get(c))
{
*p++=c;
}
*p = 0;
ifs.close();
//////////////////////////////////////////////////////////////////////////
//这里开始从字符串中解析XML
//创建TiXmlDocument对象
TiXmlDocument* doc = new TiXmlDocument();
//解析
if(!doc->Parse(buffer))
{
cout << doc->ErrorDesc() << endl;
}
//获取根节点
const TiXmlElement* root = doc->RootElement();
//循环获取该根节点下面的节点
for( const TiXmlNode* child = root->FirstChild();
child;
child=child->NextSibling())
{
//判断为元素类型并且是staticbox元素,Value()获取该标签的名称
if((child->Type() == TiXmlNode::ELEMENT) && (!strcmp(child->Value(),"staticbox")))
{
const TiXmlElement *box = (const TiXmlElement*)child;
double px, py, pz;
double dx, dy, dz;
//获取属性值
std::string mesh;
mesh = box->Attribute("mesh");
//继续循环获取子节点相关数据
for(const TiXmlNode *sub_tag = box->FirstChild(); sub_tag; sub_tag = sub_tag->NextSibling() )
{
if(sub_tag->Type() == TiXmlNode::ELEMENT)
{
const TiXmlElement *sub_element = (const TiXmlElement*)sub_tag;
if(!strcmp(sub_tag->Value(),"position"))
{
px = (sub_element->Attribute("x",&px))?px:0.0;
py = (sub_element->Attribute("y",&py))?py:0.0;
pz = (sub_element->Attribute("z",&pz))?pz:0.0;
}
else if(!strcmp(sub_tag->Value(),"dimension"))
{
dx = (sub_element->Attribute("x",&dx))?dx:1.0;
dy = (sub_element->Attribute("y",&dy))?dy:1.0;
dz = (sub_element->Attribute("z",&dz))?dz:1.0;
}
else if(!strcmp(sub_tag->Value(),"test"))
{
//使用GetText()方法来获取该标签的值,如这里获取的是test的值1和2
string temp = sub_element->GetText();
}
}
}
cout << "<StaticBox>\n";
cout << "\tPosition = (" << px << ", " << py << ", " << pz << ")\n";
cout << "\tDimension = (" << dx << ", " << dy << ", " << dz << ")\n\n";
}
}
delete doc;
getchar();
return 0;
}
7.然后在项目的文件夹中加入一个xml文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<Scene>
<staticbox mesh="crate.mesh">
<position x="-8" y="2" z="4" />
<dimension x="2" y="4" z="2" />
<test>1</test>
</staticbox>
<staticbox mesh="crate.mesh">
<position x="3" y="2" z="4" />
<dimension x="2" y="4" z="2" />
<test>2</test>
</staticbox>
</Scene>
8.编译运行
附另一个TinyXML读写XML的例子,此处写XML是直接写入XML文件,如果要使用这里的函数不写入文件而是组织成XML流,需要相应修改代码。代码在VS2005中测试运行成功。
#define TIXML_USE_STL //标志使用STL的内容
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include "tinyxml.h"
using namespace std;
int WriteXML() ;
int ReadXML() ;
//读取内存里的一段XML
int ReadXML2() ;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "开始测试!" <<endl;
WriteXML() ;
ReadXML() ;
ReadXML2() ;
cout << "结束测试!" << endl ;
return 0;
}
int WriteXML()
{
TiXmlDocument xmlDoc( "test.xml" ); // 建立一个XML文件
TiXmlDeclaration Declaration( "1.0","gb2312", "yes" ); // 声明XML的属性
xmlDoc.InsertEndChild( Declaration ); // 写入基本的XML头结构
TiXmlNode * pNode = NULL;
TiXmlElement* pRootElm = NULL;
TiXmlElement* pChildeElm = NULL;
TiXmlElement* pItemElm = NULL;
TiXmlText* pText = NULL; // 一个指向Text的指针
pText = new TiXmlText("good") ;
pRootElm = new TiXmlElement( "todo" );
pNode = xmlDoc.InsertEndChild(*pRootElm) ;
pRootElm = pNode->ToElement() ;
pChildeElm = new TiXmlElement("child1") ;
pChildeElm->SetAttribute("num", 9) ;
pNode = pRootElm->InsertEndChild(*pChildeElm) ;
TiXmlElement* pChildeElm1 = NULL;
pChildeElm1 = pNode->ToElement() ;
pChildeElm1->InsertEndChild(*pText) ;
delete pChildeElm ;
pChildeElm = NULL ;
pChildeElm = new TiXmlElement("child2") ;
pChildeElm->SetAttribute("num", 10) ;
pNode = pRootElm->InsertBeforeChild (pChildeElm1, *pChildeElm) ;
TiXmlElement* pChildeElm2 = NULL;
pChildeElm2 = pNode->ToElement() ;
pChildeElm2->InsertEndChild(*pText) ;
xmlDoc.Print() ;
xmlDoc.SaveFile(); // 把XML文件写入硬盘
return 0 ;
}
int ReadXML()
{
cout<<endl ;
cout<<"开始read XML"<<endl ;
TiXmlDocument xmlDoc( "test.xml" );
xmlDoc.LoadFile() ;
TiXmlElement* xmlRootElement = 0 ;
TiXmlElement* xmlSubElement = 0 ;
TiXmlNode * pNode = NULL;
pNode = xmlDoc.FirstChild("todo") ;
xmlRootElement = pNode->ToElement() ;
if(xmlRootElement)
{
pNode = xmlRootElement->FirstChild("child1") ;
xmlSubElement = pNode->ToElement() ;
cout<<xmlSubElement->Value()<<endl ;
cout<<xmlSubElement->Attribute("num")<<endl ;
cout<<xmlSubElement->GetText()<<endl ;
}
else
{
cout<<"找不到根元素"<<endl ;
}
cout<<"结束read XML"<<endl ;
return 0 ;
}
int ReadXML2()
{
cout<<endl ;
cout<<"开始read XML"<<endl;
const char* demoEnd ="<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"yes\" ?>"
"<todo>"
"<child2 num=\"10\">good</child2>"
"<child1 num=\"9\">good</child1>"
"</todo>" ;
TiXmlDocument xmlDoc;
xmlDoc.Parse( demoEnd ) ;
TiXmlElement* xmlRootElement = 0 ;
TiXmlElement* xmlSubElement = 0 ;
TiXmlNode * pNode = NULL;
pNode = xmlDoc.FirstChild("todo") ;
xmlRootElement = pNode->ToElement() ;
if(xmlRootElement)
{
pNode = xmlRootElement->FirstChild("child1") ;
xmlSubElement = pNode->ToElement() ;
cout<<xmlSubElement->Value()<<endl ;
cout<<xmlSubElement->Attribute("num")<<endl ;
cout<<xmlSubElement->GetText()<<endl ;
}
else
{
cout<<"找不到根元素"<<endl ;
}
cout<<"结束read XML"<<endl ;
return 0 ;
}
//----------------------------mycode--------------------------------//
private:
template <class T>
static T * _tinyxmlparser(const TiXmlElement *_element,T *_fieldsptr);
template <class T>
static T * _tinyxmlparser(const TiXmlElement *_element,T *_fieldsptr);
public:
static void * xmlparser(const string &xmlStream,int layers=1);
//---------------------------------cpp----------------------------------//
template <class T>
T * Util::_tinyxmlparser(const TiXmlElement *_element,T *_fieldsptr)
{
const TiXmlNode *child = _element->FirstChild();
if (!child || child->Type() != TiXmlNode::ELEMENT)
{
if (!child || child->Type() != TiXmlNode::ELEMENT)
{
string str_value = _element->GetText()==NULL?"":_element->GetText();
_fieldsptr->insert(typename T::value_type(_element->Value(),str_value));
}
else
{
for(const TiXmlElement *sub_tag =_element->FirstChildElement(); sub_tag; sub_tag = sub_tag->NextSiblingElement())
{
_tinyxmlparser(sub_tag,_fieldsptr);
}
}
return _fieldsptr;
}
_fieldsptr->insert(typename T::value_type(_element->Value(),str_value));
}
else
{
for(const TiXmlElement *sub_tag =_element->FirstChildElement(); sub_tag; sub_tag = sub_tag->NextSiblingElement())
{
_tinyxmlparser(sub_tag,_fieldsptr);
}
}
return _fieldsptr;
}
void * Util::xmlparser(const std::string &xmlStream,int layers)
{
try
{
//创建TiXmlDocument对象
TiXmlDocument doc;// = TiXmlDocument;
//解析
const char *buffer = xmlStream.c_str();
if(!doc.Parse(buffer))
{
throw MyError("xml_error.log",doc.ErrorDesc());
}
//获取根节点
const TiXmlElement* root = doc.RootElement();
{
try
{
//创建TiXmlDocument对象
TiXmlDocument doc;// = TiXmlDocument;
//解析
const char *buffer = xmlStream.c_str();
if(!doc.Parse(buffer))
{
throw MyError("xml_error.log",doc.ErrorDesc());
}
//获取根节点
const TiXmlElement* root = doc.RootElement();
switch(layers)
{
{
case 1:
{
fields_t * _fields = new map<string,string>;
_tinyxmlparser(root,_fields);
return _fields;
}
break;
default:
{
multimap<string,string> * _fields = new multimap<string,string>;
_tinyxmlparser(root,_fields);
return _fields;
}
break;
}
{
fields_t * _fields = new map<string,string>;
_tinyxmlparser(root,_fields);
return _fields;
}
break;
default:
{
multimap<string,string> * _fields = new multimap<string,string>;
_tinyxmlparser(root,_fields);
return _fields;
}
break;
}
}
catch(...)
{
return NULL;
}
}