一、TinyXML
tinyXml是一个免费开源的C++库,可以到官网上下载:TinyXML官网
tinyXML官方文档:http://www.grinninglizard.com/tinyxmldocs/tutorial0.html
(1)注意事项
解压之后
- 把tinystr.cpp, tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp, tinystr.h, tinyxml.h拷贝到工程目录下;
- 然后加入头文件引用:#include "tinystr.h" #include "tinyxml.h"
为了方便从XML中读取数据,可以自己写个相应的类或者结构体来存储。
vector<Customer *> customerSet(0); //每个元素是Customer对象的指针
int i,j,k,count;
int temp1; // 存放整型数据
float temp2; // 存放浮点型数据
Customer* customer; // 临时顾客节点指针
for (i=0; i<NUM_OF_CUSTOMER; i++) { // 先初始化顾客集
customer = new Customer();
customerSet.push_back(customer);
}
TiXmlDocument doc(FILENAME); // 读入XML文件
if(!doc.LoadFile()) return -1; // 如果无法读取文件,则返回
TiXmlHandle hDoc(&doc); // hDoc是&doc指向的对象
TiXmlElement* pElem; // 指向元素的指针
pElem = hDoc.FirstChildElement().Element(); //指向根节点
TiXmlHandle hRoot(pElem); // hRoot是根节点
//读取x,y,它们放在network->nodes->node节点中
TiXmlElement* nodeElem = hRoot.FirstChild("network").FirstChild("nodes").FirstChild("node").Element(); //当前指向了node节点
count = 0; //记录移动到了哪个node节点,并且把该node节点的信息录入到顺序对应的customer中
for(nodeElem; nodeElem; nodeElem = nodeElem->NextSiblingElement()) { // 挨个读取node节点的信息
customer = customerSet[count]; // 当前节点,注意不能赋值给一个新的对象,否则会调用复制构造函数
TiXmlHandle node(nodeElem); // nodeElem所指向的节点
TiXmlElement* xElem = node.FirstChild("cx").Element(); // cx节点
TiXmlElement* yElem = node.FirstChild("cy").Element(); // cy节点
nodeElem->QueryIntAttribute("id", &temp1); //把id放到temp1中,属性值读法
customer->setId(temp1);
temp2 = atof(xElem->GetText()); // char转float
customer->setX(temp2);
temp2 = atof(yElem->GetText());
customer->setY(temp2);
count++;
}
(2)TinyXML的类组织形式
- TiXmlDocument : XML文档类,它一般用于指示一个文档对象;
- TiXmlDeclaration: XML标识类,也就是XML文件第一行中标注的相关信息;
- TiXmlElement: XML节点类,这个类用来表示一个节点;
- TiXmlText: XML节点类的文本信息类,标注了XML节点类的文本信息;
- TiXmlComment: XML的注释信息类,用来标识XML文档类的注释信息;
(3)XML使用方法
- TiXmlDocument:文件节点,把Xml文件的内容读入到该类型变量中
- TiXmlElement*:指向节点的指针
- TiXmlHandle:节点的实例,也就是TiXmlElement所指向的对象
- FirstChild("nodeName"):第一个名字为“nodeName”的子节点
- NextSiblingElement():下一个兄弟节点元素,它们有相同的父节点
- QueryIntAttribute("attributeName", &var):把节点属性名为attributeName的属性值以int类型赋值给var变量
- GetText():获取当前节点元素的内容,即包含在<node>text</node>中的text
string fileName = "test.xml";
TiXmlDocument *doc = new TiXmlDocument(); //创建xml文档对象
TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0","UTF-8","");
doc->LinkEndChild(pDeclaration);
TiXmlElement *RootLv1 = new TiXmlElement("Numbers"); //创建一个根结点
doc->LinkEndChild(RootLv1); //链接到文档对象下
TiXmlElement *RootLv2 = new TiXmlElement("number"); //创建一个节点
RootLv1->LinkEndChild(RootLv2); //链接到节点RootLv1下
RootLv2->SetAttribute("Number", "1"); //设置节点RootLv2属性
TiXmlElement *Name = new TiXmlElement("name"); //创建节点
RootLv2->LinkEndChild(Name); //链接节点到RootLv2下
TiXmlElement *Price = new TiXmlElement("price"); //创建节点
RootLv2->LinkEndChild(Price); //链接节点到RootLv2下
TiXmlText *NameText = new TiXmlText("Robin"); //创建XmlText文本
Name->LinkEndChild(NameText); //链接到Name下
TiXmlText *PriceText = new TiXmlText("22.5"); //创建XmlText
Price->LinkEndChild(PriceText); //链接到Price下
doc->SaveFile("c:\\test.xml"); //保存到文件
return 0;
函数atof在库<stdlib>中,Unicode字符环境下用以将char数组转化为浮点数。
二、CMarkup
1.参考:https://blog.csdn.net/github_36679988/article/details/53173791
2.c++中map的使用参考:
2)c++使用map
参考博客
【1】TinyXML:c++中读取TinyXML到类对象中
【3】XTP库读写XML文件