TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
https://sourceforge.net/projects/tinyxml/
打开网页,点击Download下载后,里面包含了:tinyxml.h、tinystr.h、tinyxml.cpp、tinystr.cpp、tinyxmlparser.cpp、tinyxmlerror.cpp等文件。
#include
#include "tinyxml.h"
using namespace std;
int main(){
TiXmlDocument* tinyXmlDoc = new TiXmlDocument();//创建一个XML
TiXmlDeclaration* tinyXmlDeclare = new TiXmlDeclaration("1.0", "utf-8", "指定是否在XML中包含独立性声明");//创建头部信息
tinyXmlDoc->LinkEndChild(tinyXmlDeclare);// 插入文档类中
TiXmlElement* Root = new TiXmlElement("Root");// 创建根节点的名称
tinyXmlDoc->LinkEndChild(Root); // 把根节点插入到XML中
TiXmlElement *Child_one = new TiXmlElement("Child_one");//添加子节点Child_one
Child_one->SetAttribute("Name", "大明"); //设置节点的属性
Child_one->SetAttribute("Gender", "Male");
Child_one->SetAttribute("Age", "40");
TiXmlText *Hobby = new TiXmlText("爱好:游泳"); // 创建文本
Child_one->LinkEndChild(Hobby); // 给Child_one节点添加文本
TiXmlElement *Sunzi_one = new TiXmlElement("Sunzi_one"); //创建Sunzi_one节点
Sunzi_one->SetAttribute("Name", "小明"); //设置节点的属性
Sunzi_one->SetAttribute("Gender", "Male");
Sunzi_one->SetAttribute("Age", "16");
TiXmlText *Sunzione_hobby = new TiXmlText("爱好:象棋"); // 创建文本
Sunzi_one->LinkEndChild(Sunzione_hobby); // 给Sunzi_one节点添加文本
Child_one->LinkEndChild(Sunzi_one); // Sunzi_one节点插入到Child_one节点下
TiXmlElement *Sunzi_two = new TiXmlElement("Sunzi_two"); //创建Sunzi_two节点
Sunzi_two->SetAttribute("Name", "小创"); //设置节点的属性
Sunzi_two->SetAttribute("Gender", "Male");
Sunzi_two->SetAttribute("Age", "14");
TiXmlText *Sunzitwo_hobby = new TiXmlText("爱好:武术"); // 创建文本
Sunzi_two->LinkEndChild(Sunzitwo_hobby); // 给Sunzi_two节点添加文本
Child_one->LinkEndChild(Sunzi_two); // Sunzi_two节点插入到Child_one节点下
TiXmlElement *Sunzi_three = new TiXmlElement("Sunzi_three"); //创建Sunzi_three节点
Sunzi_three->SetAttribute("Name", "小花"); //设置节点的属性
Sunzi_three->SetAttribute("Gender", "Female");
Sunzi_three->SetAttribute("Age", "13");
TiXmlText *Sunzithree_hobby = new TiXmlText("爱好:跳舞"); // 创建文本
Sunzi_three->LinkEndChild(Sunzithree_hobby); // 给Sunzi_three节点添加文本
Child_one->LinkEndChild(Sunzi_three); // Sunzi_three节点插入到Child_one节点下
Root->LinkEndChild(Child_one); //子节点Child_one插入到Root节点下
//保存xml文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");//将tinyXmlDoc写入xml文件
if (result == true) printf("XML文件写入成功!\n");
else printf("XML文件写入失败!\n");
tinyXmlDoc->Print(); //控制台打印XML
system("pause");
return 0;
}
#include
#include "tinyxml.h"
using namespace std;
int main(){
// 定义xml文档类对象时指定文件名
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");
// 然后在使用这个对象去调用LoadFile传参TIXML_ENCODING_LEGACY即可
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
// 读取xml的头部信息
TiXmlDeclaration *pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("头部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
}
//获取根节点
TiXmlElement *Root = new TiXmlElement("Root");
Root = tinyXmlDoc->RootElement();
TiXmlElement *Child_two = new TiXmlElement("Child_two");
// 插入属性
Child_two->SetAttribute("Name", "大华"); //设置节点的属性
Child_two->SetAttribute("Gender", "Male");
Child_two->SetAttribute("Age", "38");
TiXmlText *Hobby = new TiXmlText("爱好围棋"); // 创建文本
Child_two->LinkEndChild(Hobby); // 给Child_two节点添加文本
TiXmlElement *Description = new TiXmlElement("Description");
TiXmlText *descriptionText = new TiXmlText("还喜欢学习编程"); // 创建文本
Description->LinkEndChild(descriptionText); // 给Description节点添加文本
Child_two->LinkEndChild(Description); // 插入到Book1节点下
Root->LinkEndChild(Child_two); // 插入到根节点下
// 保存到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("XML文件写入成功!\n");
else printf("XML文件写入失败!\n");
tinyXmlDoc->Print(); //控制台打印XML
system("pause");
return 0;
}
#include
#include "tinyxml.h"
using namespace std;
int main(){
// 定义xml文档类对象时指定文件名
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");
// 然后在使用这个对象去调用LoadFile传参TIXML_ENCODING_LEGACY即可
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
// 读取xml的头部信息
TiXmlDeclaration *pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("头部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
}
//获取根节点
TiXmlElement *Root = new TiXmlElement("Root");
Root = tinyXmlDoc->RootElement();
TiXmlElement *Child_two = new TiXmlElement("Child_two");
// 查找节点,修改属性值
TiXmlElement* pItem = Root->FirstChildElement("Child_two");
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two")) {
// 找到属性ID = 1的节点
if (strcmp(pItem->Attribute("Name"), "大华") == 0) {
pItem->SetAttribute("Age", "39");
// 设置Child_two的子节点Description的值
TiXmlElement* Description = pItem->FirstChildElement("Description"); // 获得还喜欢学习编程
TiXmlNode* des = Description->FirstChild(); // 获取元素指针 // 获得存储 "还喜欢学习编程" 的指针
des->SetValue("最讨厌编程"); // 重新为其设置值
}
}
// 保存到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("XML文件写入成功!\n");
else printf("XML文件写入失败!\n");
tinyXmlDoc->Print(); //控制台打印XML
system("pause");
return 0;
}
#include
#include "tinyxml.h"
using namespace std;
int main()
{
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");// 定义一个TiXmlDocument类指针
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
//读取xml的头部信息
TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("Header info,version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
printf("\n");
}
//获取文件根节点
TiXmlElement* Root = new TiXmlElement("Root");
if (Root){
Root = tinyXmlDoc->RootElement();
}
// 解析Child_one节点
TiXmlElement* Child_one = Root->FirstChildElement("Child_one");
if (Child_one){
printf("Child_one : %s\n", Child_one->GetText());
printf("\n");
}
TiXmlElement* pItem = Root->FirstChildElement("Child_two"); // 函数FirstChildElement():找到指定名字的元素
if (pItem){
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Child_two"))// 函数NextSiblingElement:在同一级元素中查找下一个指定名字的元素
{
// 解析属性
printf("Child_two: \n");
printf("Name = %s\n", pItem->Attribute("Name"));
printf("Gender = %s\n", pItem->Attribute("Gender"));
printf("Age = %s\n", pItem->Attribute("Age"));
// 解析Child_two的子节点
TiXmlElement* Description = pItem->FirstChildElement("Description");
if(Description){
printf("Description = %s\n", Description->GetText());
}
printf("\n");
}
}
printf("\n");
system("pause");
}
#include
#include "tinyxml.h"
using namespace std;
int main()
{
TiXmlDocument* tinyXmlDoc = new TiXmlDocument("C:/Users/Administrator/Desktop/result.xml");// 定义一个TiXmlDocument类指针
tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);
//读取xml的头部信息
TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();
if (pDeclar != NULL) {
printf("Header info,version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());
printf("\n");
}
//获取文件根节点
TiXmlElement* Root = new TiXmlElement("Root");
if (Root){
Root = tinyXmlDoc->RootElement();
}
// 删除Sunzi_two节点的Gender属性
TiXmlElement* Child_one = Root->FirstChildElement("Child_one");
TiXmlElement* pItem = Child_one->FirstChildElement("Sunzi_two");
for (; pItem != NULL; pItem = pItem->NextSiblingElement("Sunzi_two")) {
// 找到属性Name=小创的节点
if (strcmp(pItem->Attribute("Name"), "小创") == 0) {
// 删除Gender属性
pItem->RemoveAttribute("Gender");
}
}
//删除Child_two节点中属性Name="大华"的节点
pItem = Root->FirstChildElement("Child_two");
for (; pItem != NULL; ) {
// 找到属性Name="大华"的节点
if (strcmp(pItem->Attribute("Name"), "大华") == 0) {
// 提前存储删除节点的下一个节点
TiXmlElement* temporary = pItem->NextSiblingElement("Child_two");
// 删除当前节点,删除后pItem为NULL,如果再继续使用它会报错
Root->RemoveChild(pItem->ToElement());
// 这里要进行赋值回来
pItem = temporary;
} else {
// 寻找下一个Child_two节点
pItem = pItem->NextSiblingElement("Child_two");
}
}
// 保存到文件
bool result = tinyXmlDoc->SaveFile("C:/Users/Administrator/Desktop/result.xml");
if (result == true) printf("文件写入成功!\n");
else printf("文件写入失败!\n");
// 打印出来看看
tinyXmlDoc->Print();
printf("\n");
system("pause");
}
以上就是今天要讲的内容,本文仅仅简单介绍了tinyxml的使用,包括创建XML文件及增删改查的操作演示。