工作日志: tinyxml使用
tingxml是一个小型的xml解析库,里面包含创建、续写(增删改查)。
#include <stdio.h>
#include<stdlib.h>
#include "tinyxml.h"
#include "tinystr.h"
/*创建下面结构的xml文件*/
/*<?xml version="1.0" "utf-8"?>
<RootNode>
<!-- Settings my configs -->
<FatherNode_1>
<ChildNode_1>i am the first child node</ChildNode_1>
<ChildNode_2>i am the second child node</ChildNode_2>
</FatherNode_1>
<FatherNode_2>
<ChildNode_3 name="steven" xCoor="1000" xCooy="1000" height="500" width="500" />
</FatherNode_2>
<FatherNode_3 ip="192.168.0.1" timeout="123.456000" />
</RootNode>*/
#define xmlConfig_file_path "/update/xmlpares.xml"
void _xml_create(); //创建、增加xml文件
void _xml_delete(); //删除xml文件元素
void _xml_modification(); //修改、写xml文件
void _xml_lookup(); //读取、查找xml文件
int main(int argc, char *argv[]) {
_xml_create();
_xml_lookup();
_xml_modification();
_xml_delete();
return EXIT_SUCCESS;
}
void _xml_create()
{
/*xml 结构关系*/
/* xmlDoc->xmlRoot->xmlfather->xmlchild*/
/* TiXmlDocument->TiXmlElement->TiXmlElement*/
/*xml文件对象*/
TiXmlDocument xmlDoc; //TiXmlDocument: 一个对象代表一个xml文件
/*文件说明*/
TiXmlDeclaration* xmlDeclar = new TiXmlDeclaration( "1.0", "utf-8", "" );
xmlDoc.LinkEndChild(xmlDeclar);
/*根节点*/
TiXmlElement * xmlRoot = new TiXmlElement( "RootNode" ); //TiXmlElement:一个对象代表一组元素,如:<elem>...</elem>
xmlDoc.LinkEndChild( xmlRoot );
/*注释说明*/
TiXmlComment * xmlComment = new TiXmlComment(); //TiXmlComment: 用作注释说明
xmlComment->SetValue(" Settings my configs " );
xmlRoot->LinkEndChild( xmlComment ); //xmlRoot:表示在本元素内注释
/*根节点下面第一个父节点,父节点下有两个子节点*/
TiXmlElement * xmlfather = new TiXmlElement( "FatherNode_1" );
xmlRoot->LinkEndChild( xmlfather );
TiXmlElement *xmlchild = new TiXmlElement("ChildNode_1");
xmlchild->LinkEndChild(new TiXmlText("i am the first child node")); //TiXmlText: 元素的属性文本
xmlfather->LinkEndChild(xmlchild);
xmlchild = new TiXmlElement("ChildNode_2");
xmlchild->LinkEndChild(new TiXmlText("i am the second child node"));
xmlfather->LinkEndChild(xmlchild);
/*根节点下第二个父节点, 有个子节点*/
xmlfather = new TiXmlElement("FatherNode_2");
xmlRoot->LinkEndChild( xmlfather );
xmlchild = new TiXmlElement("ChildNode_3");
xmlfather->LinkEndChild(xmlchild);
xmlchild->SetAttribute("name", "steven");
xmlchild->SetAttribute("xCoor", 1000); //设置元素的属性的值
xmlchild->SetAttribute("yCoor", 1000);
xmlchild->SetAttribute("height", 500);
xmlchild->SetAttribute("width", 500);
/*根节点下第三个父节点*/
xmlfather = new TiXmlElement("FatherNode_3");
xmlRoot->LinkEndChild( xmlfather );
xmlfather->SetAttribute("ip", "192.168.0.1");
xmlfather->SetDoubleAttribute("timeout", 123.456); // 浮点属性
xmlDoc.SaveFile(xmlConfig_file_path);
}
void _xml_lookup()
{
TiXmlDocument xmlDoc;
bool bFlag = xmlDoc.LoadFile(xmlConfig_file_path);
if(!bFlag){
printf("LoadFile Failed!\n");
}
/*获取根节点*/
TiXmlElement* xmlRoot = xmlDoc.FirstChildElement();
if(!xmlRoot){
printf("Get xmlRoot Failed\n");
}
/* FirstChildElement(): 不传参数时默认获取本身节点下的第一个子节点*/
TiXmlElement *xmlchild = xmlRoot->FirstChildElement()->FirstChildElement("ChildNode_1");
printf("atu: %s value: %s\n",xmlchild->Value(),xmlchild->GetText());
xmlchild = xmlRoot->FirstChildElement()->FirstChildElement("ChildNode_2");
printf("atu: %s value: %s\n",xmlchild->Value(),xmlchild->GetText());
xmlchild = xmlRoot->FirstChildElement("FatherNode_2")->FirstChildElement("ChildNode_3");
const char *strPname = xmlchild->Attribute("name");
const char *strPx = xmlchild->Attribute("xCoor");
const char *strPy = xmlchild->Attribute("yCoor");
const char *strPh = xmlchild->Attribute("height");
const char *strPw = xmlchild->Attribute("width");
printf("atu: %s %s %s %s %s \n",strPname, strPx,strPy,strPh,strPw);
}
void _xml_modification()
{
TiXmlDocument xmlDoc;
bool bFlag = xmlDoc.LoadFile(xmlConfig_file_path);
if(!bFlag){
printf("LoadFile Failed!\n");
}
/*获取根节点*/
TiXmlElement* xmlRoot = xmlDoc.FirstChildElement();
if(!xmlRoot){
printf("Get xmlRoot Failed\n");
}
TiXmlElement *xmlchild = xmlRoot->FirstChildElement()->FirstChildElement("ChildNode_1");//修改元素值
TiXmlNode *pmodif = xmlchild->FirstChild();
if(pmodif){
pmodif->SetValue("auto haxc");
}
xmlchild = xmlRoot->FirstChildElement()->FirstChildElement("ChildNode_2");//修改元素值
pmodif = xmlchild->FirstChild();
if(pmodif){
pmodif->SetValue("auto future");
}
xmlchild = xmlRoot->FirstChildElement("FatherNode_2")->FirstChildElement("ChildNode_3");//修改元素属性值
xmlchild->SetAttribute("width", 800);
xmlDoc.SaveFile();
}
void _xml_delete()
{
TiXmlDocument xmlDoc;
bool bFlag = xmlDoc.LoadFile(xmlConfig_file_path);
if(!bFlag){
printf("LoadFile Failed!\n");
}
/*获取根节点*/
TiXmlElement* xmlRoot = xmlDoc.FirstChildElement();
if(!xmlRoot){
printf("Get xmlRoot Failed\n");
}
/*找到父节点指针,删除其下面的子节点*/
TiXmlElement *xmlchild = xmlRoot->FirstChildElement("FatherNode_3");
xmlRoot->RemoveChild(xmlchild);
xmlDoc.SaveFile();
}