(3)TinyXml2的详解及使用
TinyXml2是开源的c++xml文件解析库,简单实用,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作。
(1)DOM
DOM(Document Object Model),即文档对象模型,是针对XML并经过扩展用于HTML的应用程序编程接口(API)。
DOM本质上是一种接口(API),是专门操作网页内容的API标准。DOM把整个页面映射为一个多层节点结构,HTML或XML页面中的每个组成部分都是某种类型的节点。借助DOM提供的API,开发人员可以删除、添加、替换或修改任何节点。
DOM标准被分为三个不同的部分:
1.核心 DOM - 针对任何结构化文档的标准模型
2.XML DOM - 针对 XML 文档的标准模型
3.HTML DOM - 针对 HTML 文档的标准模型
文档--整个网页文档
对象--将网页中的每一个部分都转化为了一个对象
模型--使用模型来表示对象间的关系,这样方便我们获取对像
(2)tinyxml2的简单示例
待读取的xml文档:
<?xml version="1.0"?> <root> <name>MenAngel</name> </root>
xml文件名为test.xml,代码如下
编译命令 :g++ -g -std=c++11 -I /usr/include main.cpp /usr/include/tinyxml2/tinyxml2.cpp -o test
#include <iostream>
#include "tinyxml2/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int main(int argv,char *argc[])
{
XMLDocument xmlDoc;
xmlDoc.LoadFile("test.xml");
int errorID = xmlDoc.ErrorID();
if(errorID)
{
cout<<"Load xml test.xml fail!"<<endl;
return -1;
}
cout<<"Load xml test.xml success!"<<endl;
XMLElement *pRootElement = xmlDoc.RootElement();
const char * name = pRootElement->FirstChildElement("name")->GetText();
cout<<"name = "<<name<<endl;
return 0;
}
Load xml test.xml success!
name = MenAngel
(3)tinyxml2的详细使用范例
通常xml包含如下四个部分:
//节点构成xml的基本单元 文档节点; 对应 XMLDocument 元素节点; 对应 XMLElement 属性节点; 对应 XMLAttribute 文本节点; 对应 XMLText
1.生成xml文档(增删改查)
#include <iostream>
#include "tinyxml2/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int createXML(const char * xmlPath)
{
XMLDocument doc;
if(XML_ERROR_FILE_NOT_FOUND != doc.LoadFile(xmlPath))
{
cout<<"file is exits!"<<endl;
return 0;
}
//1.添加声明 方法一
//const char* declaration ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
//doc.Parse(declaration);//会覆盖xml所有内容
//2.添加声明 方法二
//<?xml version="1.0" encoding="UTF-8"?>
XMLDeclaration *declaration = doc.NewDeclaration();
doc.InsertFirstChild(declaration);
//3.新建root根节点
XMLElement *root = doc.NewElement("root");
doc.InsertFirstChild(root);
doc.InsertEndChild(root);
//4.给root添加节点
XMLElement* userNode = doc.NewElement("person");
//4.1设置属性
userNode->SetAttribute("name","MenAngel");
userNode->SetAttribute("passwd",123456);
//4.2设置开始标签及文本值
userNode->InsertFirstChild(doc.NewText("sunjimeng"));
//4.3设置结束标签
root->InsertEndChild(userNode);
return doc.SaveFile(xmlPath);
}
int addXML(const char *xmlPath)
{
XMLDocument doc;
if( XML_SUCCESS != doc.LoadFile(xmlPath))
{
cout<<"load xml file failed"<<endl;
return -1;
}
//再添加一个没有属性的name = MenAngel的 person节点
XMLElement *newPerson = doc.NewElement("person");
newPerson->InsertFirstChild(doc.NewText("MenAngel"));
XMLElement *root = doc.RootElement();
root->InsertEndChild(newPerson);
return doc.SaveFile(xmlPath);
}
int modifyXML(const char *xmlPath)
{
XMLDocument doc;
if( XML_SUCCESS != doc.LoadFile(xmlPath))
{
cout<<"load xml file failed"<<endl;
return -1;
}
//1.获得根节点
XMLElement *root = doc.RootElement();
//2.获得根节点root指定名称的第一个子元素
//获得根节点第一个子元素 root->FirstChild();
XMLElement *newPerson = root->FirstChildElement("person");
newPerson->SetAttribute("name","user");
newPerson->SetText("MenAngel");
newPerson->DeleteAttribute("passwd");
//3.查询属性及值
cout<<"<person> name = "<<newPerson->Attribute("name")<<" value = "<<newPerson->GetText()<<endl;
newPerson = newPerson->NextSiblingElement();
root->DeleteChild(newPerson);
return doc.SaveFile(xmlPath);
}
int main(int argv,char *argc[])
{
const char * filename = "create.xml";
int iRet = createXML(filename);
if(iRet != XML_SUCCESS)
cout<<"create xml fail!"<<endl;
iRet = addXML(filename);
if(iRet != XML_SUCCESS)
cout<<"add xml fail!"<<endl;
iRet = modifyXML(filename);
if(iRet != XML_SUCCESS)
cout<<"modify or delte xml fail!"<<endl;
return iRet;
}
//命令行打印
<person> name = user value = MenAngel
//create.xml
<?xml version="1.0" encoding="UTF-8"?> <root> <person name="user">MenAngel</person> </root>
2.利用for和while循环遍历xml文档中所有文件(两层)
//test.xml
<?xml version="1.0"?>
<root>
<name>MenAngel</name>
<age>23</age>
<gender>boy</gender>
<hobbys type="array">
<value>语文</value>
<value>数学</value>
<value>英语</value>
</hobbys>
<scores type="array">
<value>
<course>语文</course>
<score>90</score>
</value>
<value>
<course>数学</course>
<score>90</score>
</value>
<value>
<course>英语</course>
<score>90</score>
</value>
</scores>
</root>
//main.cpp
#include <iostream>
#include "tinyxml2/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int main(int argv,char *argc[])
{
XMLDocument doc;
if(XML_SUCCESS != doc.LoadFile("test.xml"))
{
cout<<"load xml file failed!"<<endl;
return -1;
}
XMLElement *root = doc.RootElement();
XMLElement *ptrNode = root->FirstChildElement();
do
{ //hobbys标签、scores标签为array类型可以进入
if(ptrNode->Attribute("type") != NULL)
{
cout<<ptrNode->Name()<<" is "<<ptrNode->Attribute("type")<<endl;
//两种标签下均是 value子标签,可以for循环迭代
for(const XMLNode *tempNode = ptrNode->FirstChild();(tempNode != NULL && tempNode->FirstChildElement() == NULL);tempNode = tempNode->NextSibling())
{
cout<<"方法一:"<<tempNode->Value()<<":"<<tempNode->ToElement()->GetText()<<endl;
}
for(XMLElement *ptrElement = ptrNode->FirstChildElement("value");ptrElement; ptrElement=ptrElement->NextSiblingElement("value"))
//hobbys标签下没有子节点,直接是值
if(ptrElement->FirstChildElement() == NULL && ptrElement->GetText() != NULL)
cout<<"方法二:"<<ptrElement->Name()<<":"<<ptrElement->GetText()<<endl;
//scores标签下有两个子节点:course、scores
else
{
cout<<" "<<ptrElement->Value()<<":"<<endl
<<" "<<ptrElement->FirstChildElement("course")->Name()<<":"<<ptrElement->FirstChildElement("course")->GetText()<<endl
<<" "<<ptrElement->FirstChildElement("score")->Name()<<":"<<ptrElement->FirstChildElement("score")->GetText()<<endl;
}
ptrNode = ptrNode->NextSiblingElement();
continue;
}
cout<<ptrNode->Name()<<":"<<ptrNode->GetText()<<endl;
ptrNode = ptrNode->NextSiblingElement();
}while(ptrNode != NULL);
}
//命令行打印
name:MenAngel
age:23
gender:boy
hobbys is array
方法一:value:语文
方法一:value:数学
方法一:value:英语
方法二:value:语文
方法二:value:数学
方法二:value:英语
scores is array
value:
course:语文
score:90
value:
course:数学
score:90
value:
course:英语
score:90
3.利用递归遍历xml文档(小于100层)
#include <iostream>
#include "tinyxml2/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void printBlank(int level = 0)
{
while(level > 0)
{
cout<<" ";
level--;
}
}
int printXMLDocument(XMLElement *element,int level = 0)
{
printBlank(level);
cout<<element->Value()<<":"<<endl;
if(element->FirstChildElement() == NULL)
{
printBlank(level);
cout<<element->Name()<<" is "<<element->GetText()<<endl;
}
for(XMLElement *ptrElement = element->FirstChildElement();ptrElement;ptrElement = ptrElement->NextSiblingElement())
{
printXMLDocument(ptrElement,level + 1);
}
}
int main(int argv,char *argc[])
{
XMLDocument doc;
if(XML_SUCCESS != doc.LoadFile("test.xml"))
{
cout<<"load xml file failed!"<<endl;
return -1;
}
XMLElement *root = doc.RootElement();
printXMLDocument(root);
return 0;
}
root:
name:
name is MenAngel
age:
age is 23
gender:
gender is boy
hobbys:
value:
value is 语文
value:
value is 数学
value:
value is 英语
scores:
value:
course:
course is 语文
score:
score is 90
value:
course:
course is 数学
score:
score is 90
value:
course:
course is 英语
score:
score is 90
4.打印XMLDocument装载的XML文件
//main.cpp
#include <iostream> #include "tinyxml2/tinyxml2.h" using namespace std; using namespace tinyxml2; int main(int argv,char *argc[]) { XMLDocument doc; if(XML_SUCCESS != doc.LoadFile("test.xml")) { cout<<"load xml file failed!"<<endl; return -1; } XMLPrinter printer; doc.Accept( &printer ); const char* xmlcstr = printer.CStr(); cout<<xmlcstr<<endl; return 0; }
//命令行打印
<?xml version="1.0"?> <root> <name>MenAngel</name> <age>23</age> <gender>boy</gender> <hobbys type="array"> <value>语文</value> <value>数学</value> <value>英语</value> </hobbys> <scores type="array"> <value> <course>语文</course> <score>90</score> </value> <value> <course>数学</course> <score>90</score> </value> <value> <course>英语</course> <score>90</score> </value> </scores> </root>
5.查询指定类型的属性
#include <iostream>
#include "tinyxml2/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int main(int argv,char *argc[])
{
XMLDocument doc;
if(XML_SUCCESS != doc.LoadFile("test.xml"))
{
cout<<"load xml file failed!"<<endl;
return -1;
}
XMLElement *ptrElement = doc.RootElement()->FirstChildElement("scores")->FirstChildElement("value")->NextSiblingElement();
cout<<"root scores value course = "<<ptrElement->FirstChildElement()->GetText()<<endl;
cout<<"root scores value score = "<<ptrElement->FirstChildElement()->NextSiblingElement()->GetText()<<endl;
int v1;
const char *s1 = new char[10];
//查询取值
ptrElement->FirstChildElement("score")->QueryIntText(&v1);
cout<<"root scores value score = "<<v1<<endl;
//查询取属性值
doc.RootElement()->FirstChildElement("scores")->QueryStringAttribute("type",&s1);
cout<<"root scores attribute type = "<<s1<<endl;
return 0;
}
root scores value course = 数学
root scores value score = 90
root scores value score = 90
root scores attribute type = array