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());
}
else
{
strcpy_s(childElem.elemAttri.attriName,"0");;
}
}
else
strcpy_s(childElem.elemAttri.attriName,"-1"); //检查文件存在的各种错误
return childElem;
}
//删除指定的联系人
int removeSubElement(const char *direct,const char*removeName)
{
IsHaveXmlFile(direct);
int remFlag;
if(xmlDoc != NULL)
{
TiXmlElement * pElem = NULL;
for(pElem=xmlRootElement->FirstChildElement();pElem;pElem=pElem->NextSiblingElement())
if(!strcmp(pElem->FirstAttribute()->Name(),removeName))
break;
if(pElem)
{
remFlag = (int)xmlRootElement->RemoveChild(pElem);
xmlDoc->SaveFile(direct);
}
else
remFlag = -1;//没有找到此联系人
}
else
remFlag = -2; //检查文件存在的各种错误
return remFlag;
}
//替换已有联系人信息
int replaceSubElement(const char *direct,const Element &newElem)
{
int repFlag;
IsHaveXmlFile(direct);
if(xmlDoc != NULL)
{
TiXmlElement * pElem = NULL;
for(pElem=xmlRootElement->FirstChildElement();pElem;pElem=pElem->NextSiblingElement())
if(!strcmp(pElem->FirstAttribute()->Name(),newElem.elemAttri.attriName))
break;
if(pElem)
{
TiXmlElement *repElem = new TiXmlElement(pElem->Value());
repElem->SetAttribute(newElem.elemAttri.attriName,newElem.elemAttri.attriValue);
TiXmlText *text = new TiXmlText(newElem.elemText);
repElem->LinkEndChild(text);
TiXmlNode *node =xmlRootElement->ReplaceChild(pElem,*repElem);
xmlDoc->SaveFile(direct);
if(node !=NULL)
repFlag = 1;
else
repFlag = 0;
}
else
repFlag = -1;//没有找到此联系人
}
else
repFlag = -2; //检查文件存在的各种错误
return repFlag;
}
//清空联系人
int clear(const char *direct)
{
int cleFlag ;
cout<<"您确定要清空您的通讯录吗?(Y/N)"<
cin>>cmd1;
if(cmd1=='Y'||cmd1=='y')
{
cout<<"您真的确定要清空您的通讯录吗?(Y/N)"<
cin>>cmd2;
if(cmd2=='Y'||cmd2=='y')
{
if(xmlDoc != NULL)
{
xmlRootElement->Clear();
xmlDoc->SaveFile(direct);
cleFlag = 1;
}