当前位置:编程学习 > XML/UML >>

C++建立通讯录XML文件(包含XML文件的各种操作)

本程序用tinyxml解析器对xml文件对其进行解析,工程中只需添加并引用tinyxml的二个头文件和四个源文件tinyxml.h、tinystr.h、tinyxml.cpp、tinystr.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp(有时需要修改一点东西,比如在vs下,这四个源文件需要添加#include "stdafx.h")即可(如有需要可以参考tinyxml中的testxml.cpp源文件), tinyxml的程序包可以在网上下载。

-----------------------------------------------myStruct.h----------------------------------------------------

#define MAX 50 www.zzzyk.com

//由于很多tinyxml函数不支持string。所以这里用c字符串
struct Attribute
{
 char attriName[MAX]; //xml元素属性名
 char attriValue[MAX]; //xml元素属性值
};

 

struct Element
{
 char elemName[MAX];   //xml元素名
 Attribute elemAttri;      //xml元素属性
 char elemText[MAX];    //xml元素text内容
};

 

-----------------------------------------------XMLFunction.h----------------------------------------------

#include "myStruct.h"
#include "tinyxml.h"

 

 //检查通讯录文件是否存在
  void IsHaveXmlFile(const char *direct);
   
 //获取我的信息
 Element getMyInfo(const char* direct);

 

 //创建的联系人xml
  void create_xml(const char* direct,Element &mXmlElement);

 

  //打印所有联系人
  bool printAllContacter(const char *direct);

 

  //在已经创建的联系人xml尾部文件中增加联系人
 bool insertSubElement(const char* direct,const Element &inElement);

 

 //查找联系人信息
   Element searchSubElement(const char *direct,const char *searchName);

 

 //删除指定的联系人
 int removeSubElement(const char *direct,const char*removeName);

 

 //替换已有联系人信息
 int replaceSubElement(const char *direct,const Element &newElem);

 

 //清空联系人
 int clear(const char *direct);

 

---------------------------------------------XMLFunction.cpp-------------------------------------------#include "stdafx.h"
#include "XMLFunction.h"
#include<fstream>
#include<iostream>

using namespace std;

 

//文件操作前,检查文件是否存在且是否能够打开且存在根元素
//如果是,则xmlDoc指向xml文件,xmlRootElement;指向此文件根元素
TiXmlElement *xmlRootElement=NULL;

TiXmlDocument *xmlDoc=NULL;

 

 

//查询联系人xml文件是否存在
void IsHaveXmlFile(const char *direct)
{
 bool openFlag = true;
 //加载一个XML的文档对象。
 xmlDoc = new TiXmlDocument(direct);
 if(!xmlDoc->LoadFile()) //是tinyXml会自动处理文档的BOM
 {
  openFlag = false;
  cout<<"此通讯录文件加载不成功!"<<endl;
 }
 if(xmlDoc ==NULL)
 {
  openFlag = false;
  cout<<"此通讯录文件不存在!"<<endl;
 }

 


 //获得根元素
 xmlRootElement = xmlDoc->RootElement();
 if(xmlRootElement == NULL)
 {
  openFlag = false;
  cout<<"此通讯录文件无根元素!"<<endl;
 }
 if(!openFlag)
 {
  xmlRootElement = NULL;
  xmlDoc = NULL;
 }
}

 

 

//获取我的信息
Element getMyInfo(const char* direct)
{
 //首先检查指定文件是否存在,不存在则不允许插入,要先用C/c命令创建
 Element myElem;
 IsHaveXmlFile(direct);
 if(xmlDoc == NULL)
 {
  strcpy_s(myElem.elemAttri.attriName,"unname");
  cout<<"请用下面的C/c命令创建此通讯录文件!"<<endl;
 }
 else
 {

  //创建一个XML的文档对象。
  //TiXmlDocument  *myDoc=new TiXmlDocument();
  //myDoc->LoadFile(direct);
  //TiXmlElement *RootElem = myDoc->RootElement();
  strcpy_s(myElem.elemName ,xmlRootElement->Value());
  strcpy_s(myElem.elemAttri.attriName,xmlRootElement->FirstAttribute()->Name());
  strcpy_s(myElem.elemAttri.attriValue,xmlRootElement->FirstAttribute()->Value());
 }
 return myElem;
}

 

 

//创建的联系人xml
void create_xml(const char* direct,Element &mXmlElement)
{
 fstream file_in1(direct,ios::in|ios::out|ios::trunc);
 file_in1.close();
 //创建一个XML的文档对象。
 TiXmlDocument  *myDoc=new TiXmlDocument();
 myDoc->LoadFile(direct);
 TiXmlDeclaration* decl = new TiXmlDeclaration("1.0","utf-8","yes");
 myDoc->LinkEndChild(decl);
 //创建一个根元素并连接。
 TiXmlElement *RootElem =new TiXmlElement(mXmlElement.elemName);
 RootElem->SetAttribute(mXmlElement.elemAttri.attriName,mXmlElement.elemAttri.attriValue);
 myDoc->LinkEndChild(RootElem);
 myDoc->SaveFile();
}

 


//打印所有联系人
bool printAllContacter(const char *direct)
{
 IsHaveXmlFile(direct);
 bool pFlag;
 if(xmlDoc != NULL)
 {
  xmlDoc->Print();
  pFlag = true;
 }
 else
  pFlag = false; // 检查文件存在的各种错误
 return pFlag;
}

 

 

//在已经创建的联系人xml尾部文件中增加联系人
bool insertSubElement(const char* direct,const Element &inElement)
{

 //写入文件
 IsHaveXmlFile(direct);
 bool insFlag ;
 if(xmlDoc != NULL)
 {
  TiXmlElement *newElem = new TiXmlElement(inElement.elemName);
  xmlRootElement->LinkEndChild(newElem);
  newElem->SetAttribute(inElement.elemAttri.attriName,inElement.elemAttri.attriValue);
  TiXmlText *text = new TiXmlText(inElement.elemText);
  newElem->LinkEndChild(text);
  xmlDoc->SaveFile(direct);
  insFlag = true;
 }
 else
  insFlag = false; // 检查文件存在的各种错误
 return insFlag;
}

 

 

//查找联系人信息
Element searchSubElement(const char *direct,const char *searchName)
{
 Element childElem;
 IsHaveXmlFile(direct);
 if(xmlDoc != NULL)
 {
  TiXmlElement * pElem  = NULL;
  for(pElem=xmlRootElement->FirstChildElement();pElem;pElem=pElem->NextSiblingElement())
  {
   if(!strcmp(pElem->FirstAttribute()->Name(),searchName))
   {
    break;
   }
  }

  if(pElem)
  {
   cout << "解析成功" << endl;

   strcpy_s(childElem.elemAttri.attriName ,pElem->FirstAttribute()->Name());
   strcpy_s(childElem.elemAttri.attriValue, pElem->FirstAttribute()->Value());
   strcpy_s(childElem.elemText, pElem->GetText());
&nbs

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,