#include "XmlTest.h"
#define MAX_NUM 256
void createXml(const char * ccXmlName)
{
TiXmlDocument *myDocument = new TiXmlDocument();
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0","gb2312","yes");
myDocument->LinkEndChild(decl);
TiXmlElement *RootElement = new TiXmlElement("Persons");
myDocument->LinkEndChild(RootElement);
for(int i=0; i<10;i++)
{
TiXmlElement *PersonElement = new TiXmlElement("Person");
RootElement->LinkEndChild(PersonElement);
PersonElement->SetAttribute("ID",i);
TiXmlElement *NameElement = new TiXmlElement("name");
TiXmlElement *AgeElement = new TiXmlElement("age");
PersonElement->LinkEndChild(NameElement);
PersonElement->LinkEndChild(AgeElement);
TiXmlText *NameContent = new TiXmlText("周星星");
TiXmlText *AgeContent = new TiXmlText("20");
NameElement->LinkEndChild(NameContent);
AgeElement->LinkEndChild(AgeContent);
}
myDocument->SaveFile(ccXmlName);
}
void readXml(const char * ccXmlName)
{
TiXmlDocument *myDocument = new TiXmlDocument(ccXmlName);
myDocument->LoadFile();
TiXmlDeclaration* decl = myDocument->FirstChild()->ToDeclaration();
cout << "xml文件的版本是:" << decl->Version() << endl;
cout << "xml的编码格式是:" << decl->Encoding() << endl;
TiXmlElement *RootElement = myDocument->RootElement();
cout << RootElement->Value() << endl;
TiXmlNode* pNode = NULL;
string msg = "";
for(pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())
{
msg += dumpNode(pNode,0);
}
cout << msg << endl;
}
string dumpNode(TiXmlNode * pNode,int flag)
{
string msg = "";
if(pNode == NULL)
{
return "";
}
TiXmlText * pText = NULL;
TiXmlNode * pChildNode = NULL;
int t = pNode->Type();
switch(t)
{
case TiXmlText::TINYXML_TEXT:
{ pText = pNode->ToText();
string text = pText->Value();
if(!text.empty())
{
msg = msg + "="+ text;
}
break;
}
case TiXmlText::TINYXML_ELEMENT:
{
msg = msg + "\r\n";
int num = flag;
while(num >= 1)
{
msg = msg + " ";
num--;
}
msg = msg + pNode->Value();
TiXmlElement * pElement = pNode->ToElement();
TiXmlAttribute * pAttr = pElement->FirstAttribute();
TiXmlAttribute * pNextAttr =NULL;
string tmpAttrMsg = "[";
if(pAttr != NULL )
{
string tmpAttrVal = "";
string tmpAttrName = "";
do
{
tmpAttrVal = pAttr->Value();
tmpAttrName = pAttr->Name();
tmpAttrMsg += tmpAttrName + "=" +tmpAttrVal + ",";
}while(pAttr = pAttr->Next());
tmpAttrMsg = tmpAttrMsg.erase(tmpAttrMsg.find_last_of(","));
}
tmpAttrMsg += "]";
msg += tmpAttrMsg;
break;
}
case TiXmlText::TINYXML_DOCUMENT:
case TiXmlText::TINYXML_COMMENT:
case TiXmlText::TINYXML_UNKNOWN:
case TiXmlText::TINYXML_DECLARATION:
case TiXmlText::TINYXML_TYPECOUNT:
{
;
}
}
for(pChildNode=pNode->FirstChild();pChildNode!=0;pChildNode = pChildNode->NextSibling())
{
msg = msg + dumpNode(pChildNode,flag+1);
}
return msg;
}
TiXmlNode * SelectSingleNode(const char * cXmlName,string nodeName,string nodeAttrName,string nodeAttrValue)
{
TiXmlDocument *xmlDoc = new TiXmlDocument(cXmlName);
if(!xmlDoc->LoadFile())
{
return NULL;
}
if(xmlDoc == NULL)
{
return NULL;
}
TiXmlElement *RootElement = xmlDoc->RootElement();
if(RootElement == NULL)
{
cout << "解析错误,无法获取根元素" << endl;
return NULL;
}
TiXmlNode * pNode = NULL;
TiXmlNode * pSelectNode = NULL;
string msg = "";
for(pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())
{
pSelectNode = selectChildNode(pNode,nodeName,nodeAttrName,nodeAttrValue);
if(pSelectNode)
{
break;
}
}
if(pSelectNode)
{
cout << "解析成功" << endl;
cout << "[节点名]=" << pSelectNode->Value() << endl;
return pSelectNode;
}
else
{
cout << "解析错误,无法获取节点" << endl;
return NULL;
}
}
TiXmlNode * SelectSingleNodeByRootEle(TiXmlElement* RootElement,string nodeAttrValue)
{
if(RootElement == NULL)
{
cout << "解析错误,nodeAttrValue);
if(pSelectNode)
{
break;
}
}
if(pSelectNode)
{
return pSelectNode;
}
else
{
cout << "解析错误,无法获取节点" << endl;
return NULL;
}
}
TiXmlNode * selectChildNode(TiXmlNode * pNode,string nodeAttrValue)
{
if(pNode == NULL)
{
return NULL;
}
TiXmlNode * pSelectedNode = NULL;
TiXmlNode * pChildNode = NULL;
int t = pNode->Type();
switch (t)
{
case TiXmlText::TINYXML_DOCUMENT:
case TiXmlText::TINYXML_DECLARATION:
case TiXmlText::TINYXML_TEXT:
case TiXmlText::TINYXML_UNKNOWN:
case TiXmlText::TINYXML_COMMENT:
break;
case TiXmlText::TINYXML_ELEMENT:
if(pNode->Value() == nodeName)
{
if(!nodeAttrName.empty() && !nodeAttrValue.empty())
{
TiXmlElement * pElement = pNode->ToElement();
TiXmlAttribute * pAttr = pElement->FirstAttribute();
TiXmlAttribute * pNextAttr =NULL;
if(pAttr != NULL)
{
do
{
if(pAttr->Name()==nodeAttrName&&pAttr->Value()== nodeAttrValue)
{
return pNode;
}
}while(pAttr = pAttr->Next());
}
}
else
{
return pNode;
}
}
else
{
for(pChildNode=pNode->FirstChild();
pChildNode!=0;
pChildNode = pChildNode->NextSibling())
{
pSelectedNode = selectChildNode(
pChildNode,nodeAttrValue);
if(pSelectedNode)
{
return pSelectedNode;
}
}
}
default:break;
}
return NULL;
}
bool insert(const char * ccXmlName)
{
TiXmlDocument *myDocument = new TiXmlDocument(ccXmlName);
myDocument->LoadFile();
if(myDocument == NULL)
{
return false;
}
TiXmlDeclaration* decl = myDocument->FirstChild()->ToDeclaration();
if(decl != NULL)
{
cout << "xml文件的版本是:" << decl->Version() << endl;
cout << "xml的编码格式是:" << decl->Encoding() << endl;
}
TiXmlElement *RootElement = myDocument->RootElement();
if(RootElement != NULL)
{
TiXmlElement *PersonElement = new TiXmlElement("Person");
PersonElement->SetAttribute("Id",1);
RootElement->LinkEndChild(PersonElement);
TiXmlText *textElement = new TiXmlText("Jam");
PersonElement->LinkEndChild(textElement);
TiXmlElement *TeamElement = new TiXmlElement("team");
TeamElement->SetAttribute("TID","001");
RootElement->LinkEndChild(TeamElement);
TiXmlElement *teamName = new TiXmlElement("name");
TiXmlText *nameText = new TiXmlText("workgroup");
teamName->LinkEndChild(nameText);
TeamElement->LinkEndChild(teamName);
TiXmlElement *teamType = new TiXmlElement("type");
TiXmlText *typeText = new TiXmlText("SI");
teamType->LinkEndChild(typeText);
TeamElement->LinkEndChild(teamType);
myDocument->SaveFile("Person.xml");
cout << RootElement->Value() << endl;
return true;
}
return false;
}
string getAttribute(TiXmlNode * pNode)
{
if(pNode == NULL) return "";
string msg = "";
TiXmlElement * pElement = pNode->ToElement();
TiXmlAttribute * pAttr = pElement->FirstAttribute();
TiXmlAttribute * pNextAttr =NULL;
string tmpAttrMsg = "";
if(pAttr != NULL)
{
string tmpAttrVal = "";
string tmpAttrName = "";
do
{
tmpAttrVal = pAttr->Value();
tmpAttrName = pAttr->Name();
tmpAttrMsg += "[" + tmpAttrName + "]=" + tmpAttrVal+"\n";
}while(pAttr = pAttr->Next());
}
msg += tmpAttrMsg;
return msg;
}
bool insertAElement(const char * cXmlName,TiXmlElement * pElement)
{
TiXmlDocument *xmlDoc = new TiXmlDocument(cXmlName);
xmlDoc->LoadFile();
if(xmlDoc == NULL)
{
return false;
}
TiXmlDeclaration* decl = xmlDoc->FirstChild()->ToDeclaration();
if(decl != NULL)
{
cout << "xml文件的版本是:" << decl->Version() << endl;
cout << "xml的编码格式是:" << decl->Encoding() << endl;
}
TiXmlElement *RootElement = xmlDoc->RootElement();
if(RootElement != NULL)
{
TiXmlNode * pNode = SelectSingleNode(cXmlName,"name","length","100");
if(pNode == NULL)
{
return false;
}
TiXmlElement *pNewElement = new TiXmlElement("Person");
if(pNewElement == NULL)
{
return false;
}
pNewElement->SetAttribute("Id","1");
TiXmlText *textElement = new TiXmlText("gbnn");
if(textElement == NULL)
{
return false;
}
pNewElement->LinkEndChild(textElement);
TiXmlNode * pRet = pNode->InsertBeforeChild(pNode->LastChild(),*pNewElement);
xmlDoc->Print();
bool b = xmlDoc->SaveFile();
if(b)
{
cout << "添加成功" << endl;
return b;
}
else
{
cout << "添加失败" << endl;
return false;
}
}
else
{
return false;
}
}
bool ModifySingleNode(const char * cXmlName,string strNodeName,string strNodeAttrName,string strNdeAttrValue,string strPath)
{
if (strNodeName.empty() || strNodeAttrName.empty() || strNdeAttrValue.empty() ||strPath.empty())
{
return false;
}
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)
{
return false;
}
pDoc->LoadFile(cXmlName);
TiXmlElement* pRootElement = pDoc->RootElement();
TiXmlNode * pNode = SelectSingleNodeByRootEle(pRootElement,strNodeName,strNodeAttrName,strNdeAttrValue);
if (NULL == pNode)
{
return false;
}
TiXmlElement * pElement = pNode->ToElement();
TiXmlAttribute * pAttr = pElement->FirstAttribute();
TiXmlAttribute * pNextAttr =NULL;
if(pAttr != NULL)
{
string tmpAttrVal = "";
string tmpAttrName = "";
do
{
tmpAttrVal = pAttr->Value();
tmpAttrName = pAttr->Name();
if (tmpAttrName == strNodeAttrName && tmpAttrVal == strNdeAttrValue)
{
continue;
}
if ("href" == tmpAttrName)
{
pAttr->SetValue(strPath.c_str());
}
if ("test" == tmpAttrName)
{
pAttr->SetValue(strPath.c_str());
}
}while(pAttr = pAttr->Next());
}
pDoc->SaveFile("hhhhhhhhhhhhh.xml");
return true;
}
void ShowSingleNode(const char * cXmlName,string strNdeAttrValue)
{
TiXmlNode * pNode = SelectSingleNode(cXmlName,strNdeAttrValue);
if (NULL == pNode)
{
return;
}
string strTem = getAttribute(pNode);
cout << strTem.c_str() << endl;
}
string PathOpt()
{
char cStr[MAX_NUM]; GetCurrentDirectory(MAX_NUM,cStr); string str = cStr; //取当前路径的上一目录所在路径 size_t pos = str.find_last_of("\\"); str.erase(str.begin()+pos,str.end()); return str; } int main() { const char * cXmlName = "files.xml"; string strPath; //createXml(ccXmlName); //readXml(cXmlName); //insert(ccXmlName); //readXml(ccXmlName); ShowSingleNode(cXmlName,"Document","title","About VMware Player Help"); strPath = PathOpt(); bool ret = ModifySingleNode(cXmlName,"About VMware Player Help",strPath); if (ret) { cout << "OK" << endl; } else { cout << "false" << endl; } ShowSingleNode("hhhhhhhhhhhhh.xml","About VMware Player Help"); return 0; }