TinyXML2是一个开源、简单、小巧、高效的C++ XML解析器,它只有一个.h文件和一个.cpp文件组成,可以轻松集成到其它程序中。它解析XML文档并从中构建可以读取、修改和保存的文档对象模型Document Object Model, DOM)。它不能解析DTDDocument Type Definitions, 文档类型定义)或XSLeXtensible 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):创建XMLtest_tinyxml2_create)和解析XMLtest_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.Parsedeclaration);
if ret != 0) {
fprintfstderr, "fail to parse xml file: %s\n", declaration);
return -1;
}
tinyxml2::XMLComment* comment = doc.NewComment"this is a xml test file");
doc.InsertEndChildcomment);
tinyxml2::XMLElement* root = doc.NewElement"Root");
doc.InsertEndChildroot);
// User
tinyxml2::XMLElement* user = doc.NewElement"User");
user->SetAttribute"Name", "fengbingchun");
root->InsertEndChilduser);
tinyxml2::XMLElement* blog = doc.NewElement"Blog");
tinyxml2::XMLText* text1 = doc.NewText"CSDN");
blog->InsertEndChildtext1);
user->InsertEndChildblog);
tinyxml2::XMLElement* code = doc.NewElement"Code");
tinyxml2::XMLText* text2 = doc.NewText"GitHub");
code->InsertEndChildtext2);
user->InsertEndChildcode);
// Blog
tinyxml2::XMLElement* blog2 = doc.NewElement"Blog");
blog2->SetAttribute"Name", "CSDN");
root->InsertEndChildblog2);
tinyxml2::XMLElement* addr = doc.NewElement"Address");
tinyxml2::XMLText* text3 = doc.NewText"https://blog.csdn.net/fengbingchun");
addr->InsertEndChildtext3);
blog2->InsertEndChildaddr);
tinyxml2::XMLElement* id = doc.NewElement"ID");
tinyxml2::XMLText* text4 = doc.NewText"fengbingchun");
id->InsertEndChildtext4);
blog2->InsertEndChildid);
// Code
tinyxml2::XMLElement* code2 = doc.NewElement"Code");
code2->SetAttribute"Name", "GitHub");
root->InsertEndChildcode2);
tinyxml2::XMLElement* addr2 = doc.NewElement"Address");
tinyxml2::XMLText* text5 = doc.NewText"https://github.com//fengbingchun");
addr2->InsertEndChildtext5);
code2->InsertEndChildaddr2);
tinyxml2::XMLElement* repositories = doc.NewElement"Repositories");
tinyxml2::XMLText* text6 = doc.NewText"27");
repositories->InsertEndChildtext6);
code2->InsertEndChildrepositories);
#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.SaveFilefile_name);
if ret != 0) {
fprintfstderr, "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.LoadFilefile_name);
if ret != 0) {
fprintfstderr, "fail to load xml file: %s\n", file_name);
return -1;
}
tinyxml2::XMLElement* root = doc.RootElement);
fprintfstdout, "root element name: %s\n", root->Name));
// User
tinyxml2::XMLElement* user = root->FirstChildElement"User");
if !user) {
fprintfstderr, "no child element: User\n");
return -1;
}
fprintfstdout, "user name: %s\n", user->Attribute"Name"));
tinyxml2::XMLElement* blog = user->FirstChildElement"Blog");
if !blog) {
fprintfstderr, "no child element: Blog, in User\n");
return -1;
}
fprintfstdout, "blog value: %s\n", blog->GetText));
fprintfstdout, "code value: %s\n\n", user->FirstChildElement"Code")->GetText));
// Blog
tinyxml2::XMLElement* blog2 = root->FirstChildElement"Blog");
if !blog2) {
fprintfstderr, "no child element: Blog\n");
return -1;
}
fprintfstdout, "blog name: %s\n", blog2->Attribute"Name"));
tinyxml2::XMLElement* addr = blog2->FirstChildElement"Address");
if !addr) {
fprintfstderr, "no child element: Address, in Blog\n");
return -1;
}
fprintfstdout, "address value: %s\n", addr->GetText));
fprintfstdout, "id value: %s\n\n", blog2->FirstChildElement"ID")->GetText));
// Code
tinyxml2::XMLElement* code = root->FirstChildElement"Code");
if !code) {
fprintfstderr, "no child element: Code\n");
return -1;
}
fprintfstdout, "code name: %s\n", code->Attribute"Name"));
tinyxml2::XMLElement* addr2 = code->FirstChildElement"Address");
if !addr2) {
fprintfstderr, "no child element: Address, in Code\n");
return -1;
}
fprintfstdout, "address value: %s\n", addr2->GetText));
fprintfstdout, "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文件输出结果如下:
GitHub:https://github.com/fengbingchun/Messy_Test