阿彪的Blog

能力在于平日里积累

原创 如何使用TinyXML来解析xml文档:收藏

新一篇: 使用C接口编写得到时间信息 | 旧一篇: 在VC里如何检查一个文件是否存在

如何使用TinyXML来解析xml文档:

TinyXML解析器是一个开源的xml文档解析器.
这个例子是用于解析一个xml文件.

xml文件内容为

<?xml version="1.0" encoding="GB2312" ?>
<root>
 <Record>
  <Name id="Visual">阿彪</Name>
 </Record>
 <Record>
  <Name id="Design">还是阿彪</Name>
 </Record>
</root>

 

#include <iostream>
#include <tinyxml.h>

using namespace std;

int main(int,char *[])
{
 TiXmlDocument dom("D:/1.xml");
 bool bSuccess = dom.LoadFile();
 if (!bSuccess)
 {
  cout<<"打开失败!"<<endl;
  cout<<"错误的原因:"<<dom.ErrorDesc()<<endl;
 }
 TiXmlElement *pElement=dom.FirstChildElement();

 TiXmlNode* pRecord=pElement->FirstChild("Record");
 while (pRecord != 0)
 {
  // 得到结点里的内容
  TiXmlNode* pName=pRecord->FirstChild("Name");
  TiXmlElement* pNameEle=pName->ToElement();
  cout<<"结点名:"<<pNameEle->Value()<<endl;
  cout<<"内容:"<<pNameEle->GetText()<<endl;

  // 得到结点的属性
  cout<<" 结点的属性"<<pNameEle->Attribute("id")<<endl;

  // 通过自己来找与自己同层的兄弟
  pRecord=pRecord->NextSibling("Record");
 }
 getchar();
 return 0;

发表于 @ 2006年12月08日 15:51:00|评论(loading...)|编辑

新一篇: 使用C接口编写得到时间信息 | 旧一篇: 在VC里如何检查一个文件是否存在

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 风之际