tinyxml2的使用方法

网上下载tinyxml2.h和tinyxml2.cpp,Windows和linux都可以用。
VC++编译后得到tinyxml2.lib和tinyxml2.dll。
新建项目,包含tinyxml2.h文件

#include <stdio.h>
#include <tinyxml2.h>
#pragma comment(lib,"tinyxml2.lib")
using namespace tinyxml2;

void createXML()
{
	XMLDocument doc;

	char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	doc.Parse(declaration);

	XMLComment* comment = doc.NewComment("this is a xml test file");
	doc.InsertEndChild(comment);

	XMLElement* root = doc.NewElement("Root");
	doc.InsertEndChild(root);

	XMLElement* user = doc.NewElement("User");
	user->SetAttribute("Name", "fengbingchun");

	root->InsertEndChild(user);

	XMLElement* blog = doc.NewElement("Blog");
	XMLText* text1 = doc.NewText("CSDN");
	blog->InsertEndChild(text1);
	user->InsertEndChild(blog);

	doc.SaveFile("test1.xml");
}

void readXML()
{
	XMLDocument doc;
	doc.LoadFile("test2.xml");

	XMLElement* root = doc.RootElement();

	XMLElement* ip = root->FirstChildElement("ip");
	printf("ip:%s\n",ip->GetText());

	XMLElement* port = root->FirstChildElement("port");
	printf("ip:%d\n", atoi(port->GetText()));

	XMLElement* username = root->FirstChildElement("username");
	printf("username:%s\n", username->GetText());

	XMLElement* password = root->FirstChildElement("password");
	printf("password:%s\n", password->GetText());

	XMLElement* MonitorDir = root->FirstChildElement("MonitorDir");
	printf("MonitorDir:%s\n", MonitorDir->GetText());

	XMLElement* remotePath = root->FirstChildElement("remotePath");
	printf("remotePath:%s\n", remotePath->GetText());


}

int main()
{
	readXML();
	getchar();
	return 0;
}

创建的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--this is a xml test file-->
<Root>
    <User Name="fengbingchun">
        <Blog>CSDN</Blog>
    </User>
</Root>

读取的 xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<ftp>
    <ip>192.168.100.5</ip>
	<port>21</port>
	<username>rxm25</username>
	<password>rxm25</password>
	<MonitorDir>MonitorDir</MonitorDir>
	<remotePath>rxm25Data</remotePath>
</ftp>

附Qt操作xml链接:https://www.cnblogs.com/warmlight/p/11389944.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值