c++之xml的创建,增删改查
- 1.创建写入
- 2.添加
- 3.删除
- 4.修改:
1.创建写入
#include <stdio.h>
#include <typeinfo>
#include "F:/EDGE/tinyxml/tinyxml.h"
#include <iostream>
#include <string>
#include <Winsock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;
void main() {printf("\n----- XML -----\n");//新建一个xml文件// 定义一个TiXmlDocument类指针TiXmlDocument* tinyXmlDoc = new TiXmlDocument();// xml的声明(三个属性:版本,编码格式,保留空串即可)TiXmlDeclaration* tinyXmlDeclare = new TiXmlDeclaration("1.0", "utf-8", ""); // 声明头部格式// 插入文档类中tinyXmlDoc->LinkEndChild(tinyXmlDeclare);// 根节点TiXmlElement* Library = new TiXmlElement("Library");tinyXmlDoc->LinkEndChild(Library); // 把根节点插入到文档类中// 创建Book节点TiXmlElement* Book = new TiXmlElement("Book");TiXmlText* bookText = new TiXmlText("书本"); // 创建文本Book->LinkEndChild(bookText); // 给Book节点添加文本Library->LinkEndChild(Book); // 插入到根节点下// 创建Book1节点TiXmlElement* Book1 = new TiXmlElement("Book1");// 插入属性Book1->SetAttribute("ID", 1);Book1->SetAttribute("Name", "红楼梦");Book1->SetAttribute("Price", "64.6");// 创建Book1的子节点DescriptionTiXmlElement* Description = new TiXmlElement("Description");TiXmlText* descriptionText = new TiXmlText("林黛玉"); // 创建文本Description->LinkEndChild(descriptionText); // 给Description节点添加文本Book1->LinkEndChild(Description); // 插入到Book1节点下// 创建Book1的子节点PageTiXmlElement* Page = new TiXmlElement("Page");TiXmlText* pageText = new TiXmlText("500页"); // 创建文本Page->LinkEndChild(pageText); // 给Page节点添加文本Book1->LinkEndChild(Page); // 插入到Book1节点下Library->LinkEndChild(Book1); // 插入到根节点下// 保存到文件 /*bool result = tinyXmlDoc->SaveFile("E:\\abs\\test.xml");if (result == true) printf("文件写入成功!\n");else printf("文件写入失败!\n");*/// 打印出来看看//tinyXmlDoc->Print();TiXmlPrinter a;tinyXmlDoc->Accept(&a);printf("%s\n", a.CStr());printf(typeid(a.CStr()).name());}
打印:
2.添加
#include <stdio.h>
#include <typeinfo>
#include "F:/EDGE/tinyxml/tinyxml.h"
#include <iostream>
#include <string>
#include <Winsock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;void main() {printf("\n----- add_XML -----\n");// 定义一个TiXmlDocument类指针TiXmlDocument* tinyXmlDoc = new TiXmlDocument("E:\\abs\\test.xml");tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);// 读取文档声明信息(也就是xml的头部信息:<?xml version="1.0" encoding="utf-8" ?>)TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();if (pDeclar != NULL) {printf("头部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());}// 得到文件根节点TiXmlElement* Library = new TiXmlElement("Library");Library = tinyXmlDoc->RootElement();// 创建Book1节点TiXmlElement* Book1 = new TiXmlElement("Book1");// 插入属性Book1->SetAttribute("ID", 2);Book1->SetAttribute("Name", "西游记");Book1->SetAttribute("Price", "99.81");// 创建Book1的子节点DescriptionTiXmlElement* Description = new TiXmlElement("Description");TiXmlText* descriptionText = new TiXmlText("师徒四人"); // 创建文本Description->LinkEndChild(descriptionText); // 给Description节点添加文本Book1->LinkEndChild(Description); // 插入到Book1节点下// 创建Book1的子节点PageTiXmlElement* Page = new TiXmlElement("Page");TiXmlText* pageText = new TiXmlText("81页"); // 创建文本Page->LinkEndChild(pageText); // 给Page节点添加文本Book1->LinkEndChild(Page); // 插入到Book1节点下Library->LinkEndChild(Book1); // 插入到根节点下// 创建Book1节点Book1 = new TiXmlElement("Book1");// 插入属性Book1->SetAttribute("ID", 3);Book1->SetAttribute("Name", "三国演义");Book1->SetAttribute("Price", "66.66");// 创建Book1的子节点DescriptionDescription = new TiXmlElement("Description");descriptionText = new TiXmlText("三国大战"); // 创建文本Description->LinkEndChild(descriptionText); // 给Description节点添加文本Book1->LinkEndChild(Description); // 插入到Book1节点下// 创建Book1的子节点PagePage = new TiXmlElement("Page");pageText = new TiXmlText("30页"); // 创建文本Page->LinkEndChild(pageText); // 给Page节点添加文本Book1->LinkEndChild(Page); // 插入到Book1节点下Library->LinkEndChild(Book1); // 插入到根节点下// 保存到文件 bool result = tinyXmlDoc->SaveFile("E:\\abs\\test.xml");if (result == true) printf("文件写入成功!\n");else printf("文件写入失败!\n");// 打印出来看看tinyXmlDoc->Print();
}
打印:
3.删除
#include <stdio.h>
#include <typeinfo>
#include "F:/EDGE/tinyxml/tinyxml.h"
#include <iostream>
#include <string>
#include <Winsock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;void main() {printf("\n----- del_XML -----\n");// 定义一个TiXmlDocument类指针TiXmlDocument* tinyXmlDoc = new TiXmlDocument("E:\\abs\\test.xml");tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);// 读取文档声明信息(也就是xml的头部信息:<?xml version="1.0" encoding="utf-8" ?>)TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();if (pDeclar != NULL) {printf("头部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());}// 得到文件根节点TiXmlElement* Library = new TiXmlElement("Library");Library = tinyXmlDoc->RootElement();/* 删除 Book 节点 */// 方式一//TiXmlElement* Book = Library->FirstChildElement("Book");//Book->Clear();// 方式二// 获取Book节点TiXmlElement* Book = Library->FirstChildElement("Book");Library->RemoveChild(Book->ToElement());//TiXmlElement* pItem1 = Library->FirstChildElement("Book");//for (; pItem1 != NULL; ) {// if (strncmp(pItem1->GetText(), "书本", 6) == 0) {// // 提前存储删除节点的下一个节点// TiXmlElement* temporary = pItem1->NextSiblingElement("Book1");// // 删除当前节点,删除后pItem为NULL,如果再继续使用它会报错// Library->RemoveChild(pItem1->ToElement());// // 所以这里要进行赋值回来// pItem1 = temporary;// // } else {// // 寻找下一个Book1节点// pItem1 = pItem1->NextSiblingElement("Book1");// }//}// 删除属性TiXmlElement* pItem = Library->FirstChildElement("Book1");for (; pItem != NULL; pItem = pItem->NextSiblingElement("Book1")) {// 找到属性ID = 1的节点if (strcmp(pItem->Attribute("ID"), "1") == 0) {// 删除属性为Name的属性pItem->RemoveAttribute("Name");}}// 删除整个节点pItem = Library->FirstChildElement("Book1");for (; pItem != NULL; ) {// 找到属性ID = 2的节点if (strcmp(pItem->Attribute("ID"), "2") == 0) {// 提前存储删除节点的下一个节点TiXmlElement* temporary = pItem->NextSiblingElement("Book1");// 删除当前节点,删除后pItem为NULL,如果再继续使用它会报错Library->RemoveChild(pItem->ToElement());// 所以这里要进行赋值回来pItem = temporary;}else {// 寻找下一个Book1节点pItem = pItem->NextSiblingElement("Book1");}}// 保存到文件 bool result = tinyXmlDoc->SaveFile("E:\\abs\\test.xml");if (result == true) printf("文件写入成功!\n");else printf("文件写入失败!\n");// 打印出来看看tinyXmlDoc->Print();}
打印:
4.修改:
#include <stdio.h>
#include <typeinfo>
#include "F:/EDGE/tinyxml/tinyxml.h"
#include <iostream>
#include <string>
#include <Winsock2.h>#pragma comment(lib, "ws2_32.lib")using namespace std;void main() {printf("\n----- alt_XML -----\n");// 定义一个TiXmlDocument类指针TiXmlDocument* tinyXmlDoc = new TiXmlDocument("E:\\abs\\test.xml");tinyXmlDoc->LoadFile(TIXML_ENCODING_LEGACY);// 读取文档声明信息(也就是xml的头部信息:<?xml version="1.0" encoding="utf-8" ?>)TiXmlDeclaration* pDeclar = tinyXmlDoc->FirstChild()->ToDeclaration();if (pDeclar != NULL) {printf("头部信息: version is %s , encoding is %s\n", pDeclar->Version(), pDeclar->Encoding());}// 得到文件根节点TiXmlElement* Library = new TiXmlElement("Library");Library = tinyXmlDoc->RootElement();TiXmlElement* pItem = Library->FirstChildElement("Book1");for (; pItem != NULL; pItem = pItem->NextSiblingElement("Book1")) {// 找到属性ID = 1的节点if (strcmp(pItem->Attribute("ID"), "1") == 0) {// 设置Book1的子节点Description的值TiXmlElement* Description = pItem->FirstChildElement("Description"); // 获得<Description>108个没房住</Description>TiXmlNode* des = Description->FirstChild(); // 获取元素指针 // 获得存储 108个没房住 的指针des->SetValue("108个没房住"); // 重新为其设置值 TiXmlElement* Page = pItem->FirstChildElement("Page");TiXmlNode* page = Page->FirstChild();page->SetValue("999页");}// 设置属性if (strcmp(pItem->Attribute("ID"), "3") == 0) {pItem->SetAttribute("Price", "33.33");}}// 保存到文件 bool result = tinyXmlDoc->SaveFile("E:\\abs\\test.xml");if (result == true) printf("文件写入成功!\n");else printf("文件写入失败!\n");//printf("%s\n", tinyXmlDoc->Value());// 打印出来看看tinyXmlDoc->Print();
}
打印: