XML解析器(TinyXML)的使用指南
作者:thelONE??來源
www.sqlite.com.cn
??
最近軟件體系結構課的一個大作業挺難的,要做很多的東西,比如網絡連接,視頻播放,XML等工作.?這里我給大家提供一個關于XML文件的解析方法的引導,?大家可以去試試這個工具(TinyXML)
1.首先下載TinyXML庫的文件,這里給出鏈接,大家自己去下吧,記著要上國際
http://prdownloads.sourceforge.net/tinyxml/tinyxml_2_3_4.zip?download
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
6.然后創建一個cpp文件,輸入下面的內容:
???1.?#include?<iostream>
??????#include?<fstream>
??????#include?"tinyxml.h"
using?namespace?std;
int?main()
{
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();
//////////////////////////////////////////////////////////////////////////
if(!doc->Parse(buffer))
{
??cout?<<?doc->ErrorDesc()?<<?endl;
}
const?TiXmlElement*?root?=?doc->RootElement();
for(?const?TiXmlNode*?child?=?root->FirstChild();
??child;
??child=child->NextSibling())
{
??OutputDebugStringA(child->Value());
??/*
??生成一個StaticBox
??<staticbox?mesh="crate.mesh">
??<position?x="-8"?y="2"?z="4"?/>
??<dimension?x="2"?y="4"?z="2"?/>
??</staticbox>
??*/
??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;
?????}
????}
???}
???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"?/>
</staticbox>
<staticbox?mesh="crate.mesh">
??<position?x="3"?y="2"?z="4"?/>
??<dimension?x="2"?y="4"?z="2"?/>
</staticbox>
</Scene>
8.編譯運行
posted on 2007-01-26 12:44
天下無雙 閱讀(7386)
評論(3) 編輯 收藏 引用 所屬分類:
C/C++