开源库TinyXML2简介及使用

开源库TinyXML2简介及使用TinyXML2是一个开源、简单、小巧、高效的C++XML解析器,它只有一个.h文件和一个.cpp文件组成,可以轻松集成到其它程序中。它解析XML文档并从中构建可以读取、修改和保存的文档对象模型(DocumentObjectModel,DOM)。它不能解析DTD(DocumentTypeDefinitions,文档类型定义)或XSL(eXtensibleStylesheetLan…

大家好,又见面了,我是你们的朋友全栈君。

TinyXML2是一个开源、简单、小巧、高效的C++ XML解析器,它只有一个.h文件和一个.cpp文件组成,可以轻松集成到其它程序中。它解析XML文档并从中构建可以读取、修改和保存的文档对象模型(Document Object Model, DOM)。它不能解析DTD(Document Type Definitions, 文档类型定义)或XSL(eXtensible Stylesheet Language, 扩展样式表语言)。在TinyXML2中,XML数据被解析为可以浏览和操作的C++对象,然后写入磁盘和其它输出流。它不依赖于C++的STL。

TinyXML2的license为ZLib,可以商用,它的源码在https://github.com/leethomason/tinyxml2 ,最新发布版本为7.1.0。

关于XML的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/38978591

以下是测试代码(test_tinyxml2.cpp):创建XML(test_tinyxml2_create)和解析XML(test_tinyxml2_parse)

#include "funset.hpp"
#include <iostream>
#include "tinyxml2.h"

int test_tinyxml2_create()
{
	const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	tinyxml2::XMLDocument doc;
	tinyxml2::XMLError ret = doc.Parse(declaration);
	if (ret != 0) {
		fprintf(stderr, "fail to parse xml file: %s\n", declaration);
		return -1;
	}

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

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

	// User
	tinyxml2::XMLElement* user = doc.NewElement("User");
	user->SetAttribute("Name", "fengbingchun");
	root->InsertEndChild(user);

	tinyxml2::XMLElement* blog = doc.NewElement("Blog");
	tinyxml2::XMLText* text1 = doc.NewText("CSDN");
	blog->InsertEndChild(text1);
	user->InsertEndChild(blog);
	
	tinyxml2::XMLElement* code = doc.NewElement("Code");
	tinyxml2::XMLText* text2 = doc.NewText("GitHub");
	code->InsertEndChild(text2);
	user->InsertEndChild(code);
	
	// Blog
	tinyxml2::XMLElement* blog2 = doc.NewElement("Blog");
	blog2->SetAttribute("Name", "CSDN");
	root->InsertEndChild(blog2);

	tinyxml2::XMLElement* addr = doc.NewElement("Address");
	tinyxml2::XMLText* text3 = doc.NewText("https://blog.csdn.net/fengbingchun");
	addr->InsertEndChild(text3);
	blog2->InsertEndChild(addr);
	
	tinyxml2::XMLElement* id = doc.NewElement("ID");
	tinyxml2::XMLText* text4 = doc.NewText("fengbingchun");
	id->InsertEndChild(text4);
	blog2->InsertEndChild(id);

	// Code
	tinyxml2::XMLElement* code2 = doc.NewElement("Code");
	code2->SetAttribute("Name", "GitHub");
	root->InsertEndChild(code2);

	tinyxml2::XMLElement* addr2 = doc.NewElement("Address");
	tinyxml2::XMLText* text5 = doc.NewText("https://github.com//fengbingchun");
	addr2->InsertEndChild(text5);
	code2->InsertEndChild(addr2);
	
	tinyxml2::XMLElement* repositories = doc.NewElement("Repositories");
	tinyxml2::XMLText* text6 = doc.NewText("27");
	repositories->InsertEndChild(text6);
	code2->InsertEndChild(repositories);

#ifdef _MSC_VER
	const char* file_name = "E:/GitCode/Messy_Test/testdata/test.xml"; 
#else
	const char* file_name = "testdata/test.xml";
#endif

	ret = doc.SaveFile(file_name);
	if (ret != 0) {
		fprintf(stderr, "fail to save xml file: %s\n", file_name);
		return -1;
	}

	return 0;
}

int test_tinyxml2_parse()
{
#ifdef _MSC_VER
	const char* file_name = "E:/GitCode/Messy_Test/testdata/test_tinyxml2.xml"; 
#else
	const char* file_name = "testdata/test_tinyxml2.xml";
#endif

	tinyxml2::XMLDocument doc;
	tinyxml2::XMLError ret = doc.LoadFile(file_name);
	if (ret != 0) {
		fprintf(stderr, "fail to load xml file: %s\n", file_name);
		return -1;
	}

	tinyxml2::XMLElement* root = doc.RootElement();
	fprintf(stdout, "root element name: %s\n", root->Name());

	// User
	tinyxml2::XMLElement* user = root->FirstChildElement("User");
	if (!user) {
		fprintf(stderr, "no child element: User\n");
		return -1;
	}
	fprintf(stdout, "user name: %s\n", user->Attribute("Name"));

	tinyxml2::XMLElement* blog = user->FirstChildElement("Blog");
	if (!blog) {
		fprintf(stderr, "no child element: Blog, in User\n");
		return -1;
	}
	fprintf(stdout, "blog value: %s\n", blog->GetText());
	fprintf(stdout, "code value: %s\n\n", user->FirstChildElement("Code")->GetText());
	
	// Blog
	tinyxml2::XMLElement* blog2 = root->FirstChildElement("Blog");
	if (!blog2) {
		fprintf(stderr, "no child element: Blog\n");
		return -1;
	}
	fprintf(stdout, "blog name: %s\n", blog2->Attribute("Name"));

	tinyxml2::XMLElement* addr = blog2->FirstChildElement("Address");
	if (!addr) {
		fprintf(stderr, "no child element: Address, in Blog\n");
		return -1;
	}
	fprintf(stdout, "address value: %s\n", addr->GetText());
	fprintf(stdout, "id value: %s\n\n", blog2->FirstChildElement("ID")->GetText());
	
	// Code
	tinyxml2::XMLElement* code = root->FirstChildElement("Code");
	if (!code) {
		fprintf(stderr, "no child element: Code\n");
		return -1;
	}
	fprintf(stdout, "code name: %s\n", code->Attribute("Name"));

	tinyxml2::XMLElement* addr2 = code->FirstChildElement("Address");
	if (!addr2) {
		fprintf(stderr, "no child element: Address, in Code\n");
		return -1;
	}
	fprintf(stdout, "address value: %s\n", addr2->GetText());
	fprintf(stdout, "repositories value: %s\n\n", code->FirstChildElement("Repositories")->GetText());

	return 0;
}

创建xml文件的执行结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--this is a xml test file-->
<Root>
    <User Name="fengbingchun">
        <Blog>CSDN</Blog>
        <Code>GitHub</Code>
    </User>
    <Blog Name="CSDN">
        <Address>https://blog.csdn.net/fengbingchun</Address>
        <ID>fengbingchun</ID>
    </Blog>
    <Code Name="GitHub">
        <Address>https://github.com//fengbingchun</Address>
        <Repositories>27</Repositories>
    </Code>
</Root>

解析以上xml文件输出结果如下:

开源库TinyXML2简介及使用

GitHub:https://github.com/fengbingchun/Messy_Test

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130527.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MPU9250传感器

    MPU9250内部包括3轴陀螺仪、3轴加速度计和3轴磁力计,这3个功能输出都是16位的数字量;可以通过常用的数据总线(IIC)接口和单片机进行数据交互,传输速率400kHz/s。陀螺仪的角速度测量范围±2000(°/s),具有良好的动态响应特性。加速度计的测量范围最大为±16g(g为重力加速度),静态测量精度高。磁力计采用高灵度霍尔型传感器进行数据采集,磁感应强度测量范围为±4800μT,可用于对偏航角的辅助测量。MPU9250自带的数字运动处理器DMP硬件加速引擎,可

    2022年4月8日
  • Gradle DSL method not found: ‘android()

    Gradle DSL method not found: ‘android()

    2022年3月5日
  • js判断字符串是否为空格_替换字符串c++

    js判断字符串是否为空格_替换字符串c++//判断字符串是否为空functionisNull(variable){if(variable!==null||variable!==undefined||variable!==”){ if(variable.replace(/(^s*)|(s*$)/g,””).length>0){returnfalse} }returntrue;

    2022年10月24日
  • Spring Cloud版本与Spring Boot版本之间匹配关系「建议收藏」

    Spring Cloud版本与Spring Boot版本之间匹配关系「建议收藏」很多人在使用springboot和springcloud,但是对于这两者之间的版本关系不是很清楚,特别是在面临升级的时候不知道该如何操作。本文简要摘录的官方文档的部分内容作为依据,供广大同行参考。问题的提出,我现在使用的是1.5.1版本的springboot和Edgware.RELEASE版本的springcloud,我想升级到springboot到2.0.0,springcloud需要升…

    2022年4月30日
  • python批量下载网页图片_Python下载图片

    python批量下载网页图片_Python下载图片根据图片url。批量下载图片:importosimportrequestspath=os.getcwd()+’\\abc\\’#设置图片文件路径,前提是必须要有abc这个文件夹importpandasaspddf=pd.read_excel(‘url1.xlsx’)urls=df[‘url’]foriinrange(len(urls)):print(i)#url=’http://pic.qiushibaike.com/syste..

    2022年9月15日
  • oninput事件

    oninput事件js监听input等表单输入框的变化事件oninput,手机页面开发中使用到文本框textarea输入字符监听文本框变化计算还可以输入多少字符,如果使用onkeyup的话是无法监听到输入法输入的文本变化的,复制粘贴也不能监听到,于是就用到了oninput事件来监听文本框value值的改变。由于是手机端页面没有考虑IE这货。js代码如下://计算文本输入统计(function

    2022年6月11日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号