posts - 45,  comments - 232,  trackbacks - 0
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 天下无双 阅读(7341) 评论(3)  编辑 收藏 引用 所属分类: C/C++

FeedBack:
# re: XML解析器(TinyXML)的使用指南(转)
2012-03-25 15:59 | xhui
不行啊,新建的cpp编译显示 fatal error C1083: Cannot open include file: 'tinyxml.h': No such file or directory,怎么回事  回复  更多评论
  
# re: XML解析器(TinyXML)的使用指南(转)
2013-02-20 18:00 | 哈哈哈
TiXmlNode::ELEMENT 报错?  回复  更多评论
  
# re: XML解析器(TinyXML)的使用指南(转)
2013-04-21 10:30 | duan
在项目-属性-配置属性-c++-常规-附件包含目录里把那个.h文件的所在目录加进去行了。@xhui
  回复  更多评论
  

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



常用链接

留言簿(15)

随笔分类

随笔档案

相册

我的其它领域Blog

搜索

  •  

积分与排名

  • 积分 - 202665
  • 排名 - 128

最新评论

阅读排行榜

评论排行榜