前言
TinyXml2是一个轻量级基于c++的开源xml解析库。使用非常简单方便
下载
https://github.com/leethomason/tinyxml2
使用方法
将tinyxml2.cpp和tinyxml2.h拷贝至项目目录,使用时包含
#include "tinyxml2.h"
using namespace tinyxml2
生成的xml内容参考
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Node1 att1="text" att2="123" att3="-123" att4="true">node text</Node1>
<Node2>12345</Node2>
<Node3>false</Node3>
<Node4>
<List1>List1</List1>
<List2>-123</List2>
</Node4>
<Node5>
<List>0</List>
<List>1</List>
<List>2</List>
<List>3</List>
<List>4</List>
<List2>0</List2>
<List2>1</List2>
<List2>2</List2>
<List2>3</List2>
<List2>4</List2>
</Node5>
</Root>
代码示例
#include <stdio.h>
#include "tinyxml2\tinyxml2.h"
using namespace tinyxml2;
/* 写xml */
XMLError writeXML(const char* xmlPath)
{
XMLDocument doc; // 创建xml文档
/* 添加申明,就是xml文件最上边的“<?xml version="1.0" encoding="UTF-8"?>” */
doc.InsertFirstChild(doc.NewDeclaration());
/* 还可以这样添加自定义声明 */
//doc.InsertFirstChild(doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\""));
/* 添加跟元素 */
XMLElement* root = doc.NewElement("Root"); // 创建跟元素
doc.InsertEndChild(root); // 将跟节点加入文档
/* 添加子元素1 */
XMLElement* node = doc.NewElement("Node1"); // 创建子元素1
root->InsertEndChild(node); // 将子元素1加入跟元素
node->SetAttribute( "att1", "text" ); // 添加文本属性
node->SetAttribute( "att2", 123 ); // 添加数字属性
node->SetAttribute( "att3", -123 ); // 添加数字属性
node->SetAttribute( "att4", true ); // 添加布尔属性
node->SetText( "node text" ); // 添加元素内容
/* 添加子元素2 */
node = doc.NewElement("Node2"); // 创建子元素2
root->InsertEndChild(node); // 将子元素2加入跟元素
node->SetText( 12345 ); // 添加元素内容 数字
/* 添加子元素3 */
node = doc.NewElement("Node3"); // 创建子元素3
root->InsertEndChild(node); // 将子元素3加入跟元素
node->SetText( false ); // 添加元素内容 布尔
/* 添加子元素4 */
node = doc.NewElement("Node4"); // 创建子元素4
root->InsertEndChild(node); // 将子元素4加入跟元素
/* 子元素4添加从元素 */
XMLElement* list = doc.NewElement("List1"); // 创建从元素41
node->InsertEndChild(list); // 将从元素41加入子元素4
list->SetText( "List1" ); // 添加元素内容
/* 子元素4添加从元素 */
list = doc.NewElement("List2"); // 创建从元素42
node->InsertEndChild(list); // 将从元素42加入子元素4
list->SetText( -123 ); // 添加元素内容 有符号数
/* 添加子元素5 */
node = doc.NewElement("Node5"); // 创建子元素5
root->InsertEndChild(node); // 将子元素5加入跟元素
/* 子元素5添加从元素 */
for(int i=0; i< 5; i++)
{
list = doc.NewElement("List");
node->InsertEndChild(list);
list->SetText( i );
}
/* 子元素5添加从元素 */
for(int i=0; i< 5; i++)
{
list = doc.NewElement("List2");
node->InsertEndChild(list);
list->SetText( i );
}
/* 写入文件 */
return doc.SaveFile(xmlPath);
}
/* 读xml */
XMLError readXML(const char* xmlPath)
{
XMLDocument doc;
XMLError error;
/* 打开文档 */
error = doc.LoadFile(xmlPath);
if(error != XML_SUCCESS) return error;
/* 获取跟元素 */
XMLElement* root = doc.RootElement();
printf("Root - Name: %s \r\n\r\n", root->Name()); // 跟元素名
/* 获取子元素1 */
XMLElement* node = root->FirstChildElement( "Node1");
if(node)
{
printf("Node - Name: %s \r\n", node->Name()); // 元素名
printf("Node - att1: %s \r\n", node->Attribute( "att1" )); // 文本属性
printf("Node - att2: %d \r\n", node->UnsignedAttribute( "att2" )); // 数字属性 无符号数
printf("Node - att3: %d \r\n", node->IntAttribute( "att3" )); // 数字属性 有符号数
printf("Node - att4: %s \r\n", node->BoolAttribute( "att4" ) ? "真" : "假"); // 布尔属性
printf("Node - Text: %s \r\n", node->GetText()); // 元素内容
printf("\r\n");
}
/* 获取子元素2 */
node = root->FirstChildElement( "Node2");
if(node)
{
printf("Node - Name: %s \r\n", node->Name()); // 元素名
printf("Node - Text: %d \r\n", node->UnsignedText()); // 元素内容
printf("\r\n");
}
/* 获取子元素3 */
node = root->FirstChildElement( "Node3");
if(node)
{
printf("Node - Name: %s \r\n", node->Name()); // 元素名
printf("Node - Text: %s \r\n", node->BoolText() ? "真" : "假"); // 元素内容
printf("\r\n");
}
/* 获取子元素4 */
node = root->FirstChildElement( "Node4");
if(node)
{
printf("Node - Name: %s \r\n", node->Name()); // 元素名
/* 从元素 */
XMLElement* list = node->FirstChildElement( "List1");
if(list)
{
printf("List - Name: %s \r\n", list->Name());
printf("List - Text: %s \r\n", list->GetText());
}
/* 从元素 */
list = node->FirstChildElement( "List2");
if(list)
{
printf("List - Name: %s \r\n", list->Name());
printf("List - Text: %d \r\n", list->IntText());
}
printf("\r\n");
}
/* 获取子元素5 */
node = root->FirstChildElement( "Node5");
if(node)
{
printf("Node - Name: %s \r\n", node->Name());
#if 1
/* 从元素List */
XMLElement* list = node->FirstChildElement( "List");
while (list)
{
printf("List - Name: %s \t Text: %d \r\n", list->Name(),list->IntText());
list = list->NextSiblingElement("List");
}
/* 从元素List1 */
list = node->FirstChildElement( "List2");
while (list)
{
printf("List - Name: %s \t Text: %d \r\n", list->Name(),list->IntText());
list = list->NextSiblingElement("List2");
}
#else
/* 获取所有从元素 */
XMLElement* list = node->FirstChildElement();
while (list)
{
printf("List - Name: %s \t Text: %d \r\n", list->Name(),list->IntText());
list = list->NextSiblingElement();
}
#endif
printf("\r\n");
}
return error;
}
int main(void)
{
printf("按任意键开始写xml \r\n"); getchar();
XMLError error = writeXML("text.xml");
printf("写入结果:%s %s \r\n\r\n",
XMLDocument::ErrorIDToName(error),
(error == XML_SUCCESS) ? "(已写入text.xml)" : "(写入失败)");
printf("任意键开始读 text.xml"); getchar();
readXML("text.xml");
printf("任意键退出……"); getchar();
return 0;
}