TinyXml使用指南(2)

#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

  
  
  
  

  */
  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 << "/n";
   cout << "/tPosition = (" << px << ", " << py << ", " << pz << ")/n";
   cout << "/tDimension = (" << dx << ", " << dy << ", " << dz << ")/n/n";
  }
}

delete doc;

getchar();

return 0;

}

然后在项目的文件夹中加入一个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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值