首页 技术 正文
技术 2022年11月15日
0 收藏 644 点赞 4,181 浏览 2711 个字

关于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.编译运行

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905