C++通过TinyXML类库读写XML文件

本文介绍了一个轻量级的XML解析库TinyXML的使用方法。包括如何下载安装、基本的XML文件创建与解析流程,以及在Visual Studio环境中解决编译错误的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/tennysonsky/article/details/48630005#comments

TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。


DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。


使用之前,需要先下载TinyXML类库:http://download.youkuaiyun.com/detail/overlord_bingo/9789750

然后解压缩TinyXML后,将后六个文件添加到你的c++工程中,分别是main.cpptinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。


如本示例中,只有 main.cpp 才是测试代码;

编写代码时,只需要包含 tinyxml.h 头文件即可,但是,编译时却需要把所有.cpp 文件都加上

在VS中编译时出现:error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?

解决方式:
一.
1) 在解决方案资源管理器中,右击相应的.cpp文件,点击“属性”
2) 在左侧配置属性中,点开“C/C++”,单击“预编译头”
3) 更改右侧第一行的“创建/使用预编译头”,把选项从“使用预编译头(/Yu)”改成“不使用预编译头”
4) 注:每一个报错的.cpp都要如此更改哦~辛苦一下呗~
二.
(不推荐)
1)在解决方案右击工程,点击属性
2)在配置属性 -> c/c++ -> 预编译头 中 将 “使用预编译头(/YU)” 改为 “不适用预编译头”
这种做法会使每次编译过程非常缓慢

示例代码如下:

#include <stdio.h>  
#include "tinyxml.h"  
#include <iostream>  
#include <cstring>  
using namespace std;  
  
/* 
    TiXmlDocument:文档类,它代表了整个xml文件 
    TiXmlDeclaration:声明类,它表示文件的声明部分 
    TiXmlComment:注释类,它表示文件的注释部分 
    TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类 
    TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性 
    TiXmlText:文本对象,它嵌套在某个元素内部 
*/  
//创建xml文件  
int writeXmlFile()
{
	TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针  


	//文档格式声明  
	TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
	writeDoc->LinkEndChild(decl); //写入文档  


	int n = 3;  //父节点个数  


	TiXmlElement *RootElement = new TiXmlElement("Info");//根元素  
	RootElement->SetAttribute("num", n); //属性  
	writeDoc->LinkEndChild(RootElement);


	for (int i = 0; i<n; i++)//n个父节点  
	{
		TiXmlElement *StuElement = new TiXmlElement("Stu");//Stu  
		//设置属性  
		StuElement->SetAttribute("class", "A");
		if (2 == i)
		{
			StuElement->SetAttribute("class", "B");
		}


		StuElement->SetAttribute("id", i + 1);
		StuElement->SetAttribute("flag", (i + 1) * 10);
		RootElement->LinkEndChild(StuElement);//父节点写入文档  


		//姓名  
		TiXmlElement *nameElement = new TiXmlElement("name");
		StuElement->LinkEndChild(nameElement);


		TiXmlText *nameContent = new TiXmlText("mike");
		nameElement->LinkEndChild(nameContent);


		//分数  
		TiXmlElement *scoreElement = new TiXmlElement("score");
		StuElement->LinkEndChild(scoreElement);


		TiXmlText *scoreContent = new TiXmlText("88");
		scoreElement->LinkEndChild(scoreContent);


		//城市  
		TiXmlElement *cityElement = new TiXmlElement("city");
		StuElement->LinkEndChild(cityElement);


		TiXmlText *cityContent = new TiXmlText("Shenzhen");
		cityElement->LinkEndChild(cityContent);


	}


	writeDoc->SaveFile("stu_info.xml");
	delete writeDoc;


	return 1;
}


//解析xml文件  
int readXmlFile()
{
	TiXmlDocument mydoc("stu_info.xml");//xml文档对象  
	bool loadOk = mydoc.LoadFile();//加载文档  
	if (!loadOk)
	{
		cout << "could not load the test file.Error:" << mydoc.ErrorDesc() << endl;
		exit(1);
	}


	TiXmlElement *RootElement = mydoc.RootElement();  //根元素, Info  
	cout << "[root name]" << RootElement->Value()<<" ";
	TiXmlAttribute *pattr = RootElement->FirstAttribute();//第一个属性
	while (NULL != pattr) //输出所有属性  
	{
		cout << pattr->Name() << ":" << pattr->Value() << " ";
		pattr = pattr->Next();
	}
	cout << endl;
	TiXmlElement *pEle = RootElement;


	//遍历该结点  
	for (TiXmlElement *StuElement = pEle->FirstChildElement();//第一个子元素  
		StuElement != NULL;
		StuElement = StuElement->NextSiblingElement())//下一个兄弟元素  
	{
		// StuElement->Value() 节点名称  
		cout << StuElement->Value() << " ";
		TiXmlAttribute *pAttr = StuElement->FirstAttribute();//第一个属性  


		while (NULL != pAttr) //输出所有属性  
		{
			cout << pAttr->Name() << ":" << pAttr->Value() << " ";
			pAttr = pAttr->Next();
		}
		cout << endl;


		//输出子元素的值  
		for (TiXmlElement *sonElement = StuElement->FirstChildElement();
			sonElement!= NULL;
			sonElement = sonElement->NextSiblingElement())
		{
			cout << sonElement->Value() <<":"<< sonElement->FirstChild()->Value() << endl;
		}
	}


	return 1;
}


int _tmain(int argc, _TCHAR* argv[])
{
	writeXmlFile();
	readXmlFile();
	system("pause");
	return 0;
}


编译运行结果如下:



生成的xml文件内容如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>  
  2. <Info num="3">  
  3.     <Stu class="A" id="1" flag="10">  
  4.         <name>mike</name>  
  5.         <score>88</score>  
  6.         <city>Shenzhen</city>  
  7.     </Stu>  
  8.     <Stu class="A" id="2" flag="20">  
  9.         <name>mike</name>  
  10.         <score>88</score>  
  11.         <city>Shenzhen</city>  
  12.     </Stu>  
  13.     <Stu class="B" id="3" flag="30">  
  14.         <name>mike</name>  
  15.         <score>88</score>  
  16.         <city>Shenzhen</city>  
  17.     </Stu>  
  18. </Info>  


以上
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值