C++ —— Tinyxml2在Vs2017下相关使用1(附源码)

相关链接

C++ —— Tinyxml2在Vs2017下相关使用2(较文1更复杂,附源码)

tinyxml2简介

     TinyXML2是一个简单,小巧,高效,C++XML解析器,可以很容易地集成到其他程序中。TinyXML-2解析一个XML文档,并从中构建一个 可以读取、修改和保存的文档对象模型 (DOM)。XML代表“可扩展标记语言”。这是一个通用目的 用于描述任意数据的人类和机器可读标记语言。 为存储应用程序数据而创建的所有随机文件格式都可以 全部替换为 XML。一个解析器可以解决所有问题。

源码下载

     Github - Tinyxml2源码下载地址

     一般来说,下载源码后无需编译仅将其中的tinyxml2.cpp、tinyxml2.h包含在自己的项目中就可以使用了。
     (若需构建库使用CMake构建源码,然后利用Vs仅编译tinyxml2项目。)

代码演示创建写入xml

C++ —— Tinyxml2在Vs2017下相关使用1(附源码)_第1张图片

#include 
#include "./tinyxml2/src/tinyxml2.h"

int main()
{
	tinyxml2::XMLDocument xml;
	xml.Parse(""
		""
		""
	);

	tinyxml2::XMLElement* rootNode = xml.NewElement("hebavior");
	rootNode->SetAttribute("name", "maintree_task");
	rootNode->SetAttribute("agenttype", "FirstAgent");
	rootNode->SetAttribute("version", "5");
	xml.InsertEndChild(rootNode);

	tinyxml2::XMLElement* node1 = xml.NewElement("node");
	node1->SetAttribute("class", "Sequence");
	node1->SetAttribute("id", "0");
	rootNode->InsertEndChild(node1);

	tinyxml2::XMLElement* node2_1 = xml.NewElement("node");
	node2_1->SetAttribute("class", "Action");
	node2_1->SetAttribute("id", "1");
	tinyxml2::XMLElement* node2_1_1 = xml.NewElement("property");
	node2_1_1->SetAttribute("Method", "Self.FirstAgent::say("wait subtree...")");
	node2_1->InsertEndChild(node2_1_1);
	tinyxml2::XMLElement* node2_1_2 = xml.NewElement("property");
	node2_1_2->SetAttribute("ResultOption", "BT_SUCCESS");
	node2_1->InsertEndChild(node2_1_2);
	node1->InsertEndChild(node2_1);

	tinyxml2::XMLElement* node2 = xml.NewElement("name");
	node2->InsertFirstChild(xml.NewText("9527"));
	node2->SetAttribute("real", "yes");
	rootNode->InsertEndChild(node2);
	
	xml.SaveFile("./temp_behaviac.xml");

	return 0;
}

代码演示读取xml

C++ —— Tinyxml2在Vs2017下相关使用1(附源码)_第2张图片

#include 
#include "./tinyxml2/src/tinyxml2.h"

int main()
{
	tinyxml2::XMLDocument xml;
	tinyxml2::XMLError errXml = xml.LoadFile("./temp_behaviac.xml");
	if (tinyxml2::XML_SUCCESS == errXml)
	{
		tinyxml2::XMLElement* root = xml.RootElement();
		const char *rootText = root->Value();
		std::cout << "root:" << rootText << std::endl;

		if (const tinyxml2::XMLAttribute *XMLAttribute_name = root->FindAttribute("name"))
		{
			std::cout << "root - name:" << XMLAttribute_name->Value() << std::endl;
		}
		if (const tinyxml2::XMLAttribute *XMLAttribute_agenttype = root->FindAttribute("agenttype"))
		{
			std::cout << "root - agenttype:" << XMLAttribute_agenttype->Value() << std::endl;
		}
		if (const tinyxml2::XMLAttribute *XMLAttribute_version = root->FindAttribute("version"))
		{
			std::cout << "root - version:" << XMLAttribute_version->Value() << std::endl;
		}

		tinyxml2::XMLElement *element1 = root->FirstChildElement();
		if (element1)
		{
			const char *head1 = element1->Value();
			if (head1 && strcmp(head1, "node") == 0)
			{
				if (const tinyxml2::XMLAttribute *tAttribute = element1->FindAttribute("class"))
				{
					std::cout << "\tclass - class:" << tAttribute->Value() << std::endl;
				}
				if (const tinyxml2::XMLAttribute *tAttribute = element1->FindAttribute("id"))
				{
					std::cout << "\tclass - id:" << tAttribute->Value() << std::endl;
				}

				tinyxml2::XMLElement *element2 = element1->FirstChildElement();
				if (element2)
				{
					const char *head2 = element2->Value();
					if (head2 && strcmp(head2, "node") == 0)
					{
						if (const tinyxml2::XMLAttribute *tAttribute = element2->FindAttribute("class"))
						{
							std::cout << "\t\tclass2 - class:" << tAttribute->Value() << std::endl;
						}
						if (const tinyxml2::XMLAttribute *tAttribute = element2->FindAttribute("id"))
						{
							std::cout << "\t\tclass2 - id:" << tAttribute->Value() << std::endl;
						}

						tinyxml2::XMLElement *element3_1 = element2->FirstChildElement();
						if (element3_1)
						{
							const char *head3 = element3_1->Value();
							if (head3 && strcmp(head3, "property") == 0)
							{
								if (const tinyxml2::XMLAttribute *tAttribute = element3_1->FindAttribute("Method"))
								{
									std::cout << "\t\t\tclass3 - Method:" << tAttribute->Value() << std::endl;
								}
							}
						}
						tinyxml2::XMLElement *element3_2 = element3_1->NextSiblingElement();
						if (element3_2)
						{
							const char *head4 = element3_2->Value();
							if (head4 && strcmp(head4, "property") == 0)
							{
								if (const tinyxml2::XMLAttribute *tAttribute = element3_2->FindAttribute("ResultOption"))
								{
									std::cout << "\t\t\tclass4 - ResultOption:" << tAttribute->Value() << std::endl;
								}
							}
						}
					}
					head2 = nullptr;
				}
				element2 = nullptr;
			}
			head1 = nullptr;

			tinyxml2::XMLElement *element1_2 = element1->NextSiblingElement();
			if (element1_2)
			{
				const char *head = element1_2->Value();
				if (head && strcmp(head, "name") == 0)
				{
					if (const tinyxml2::XMLAttribute *tAttribute = element1_2->FindAttribute("real"))
					{
						std::cout << "\tname - real:" << tAttribute->Value() << std::endl;
					}
				}
				
				if (tinyxml2::XMLNode *node_name = element1_2->FirstChild())
				{
					std::cout << "\tname - child:" << node_name->Value() << std::endl;
				}
			}
		}
		element1 = nullptr;
	}
	
	return 0;
}

笔者

笔者 - jxd

你可能感兴趣的:(C++,c++,tinyxml2,xml)