以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  XML解析器(TinyXML)的使用指南(转载)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=36591)


--  作者:longshentailang
--  发布时间:8/9/2006 3:53:00 PM

--  XML解析器(TinyXML)的使用指南(转载)
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.编译运行


--  作者:longshentailang
--  发布时间:8/9/2006 3:58:00 PM

--  tinyxml_2_3_4.zip
我这里已经把TinyXML库的文件上传上来了,如果不便于从下面链接下载该文件,可以从这里下载tinyxml_2_3_4.zip文件.
--  作者:longshentailang
--  发布时间:8/9/2006 4:02:00 PM

--  上传的tinyxml_2_3_4.zip文件
不好意思,上面没有上传成功,这次是再传的!
--  作者:lagen
--  发布时间:3/6/2007 4:07:00 PM

--  
hao
--  作者:derry755
--  发布时间:4/9/2007 3:49:00 PM

--  
gooood
--  作者:邹小虎
--  发布时间:4/11/2007 9:31:00 AM

--  
good,thanks!
--  作者:lxlcnman
--  发布时间:4/29/2007 11:35:00 PM

--  
我下不了
--  作者:skyblue523
--  发布时间:5/13/2007 12:37:00 PM

--  
谢谢共享

--  作者:seavan
--  发布时间:11/6/2007 10:46:00 AM

--  为什么下载没有连接?
为什么下载没有连接?
--  作者:天涯咫尺之遥
--  发布时间:11/19/2007 11:19:00 PM

--  
我下不下来呢 能不能把源文件给我呢 qiaoxiaoli0224@126.com
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
91.797ms