可以从网站http://sourceforge.net/projects/tinyxml/?source=typ_redirect处下载源码。 下面说下使用: xml内容格式如下: <?xml version="1.0" encoding="GBK" ?> <config> <libs> <item id="0"> <charact T="1" m="测试"/> <table>基地发源地</table> </item> <item id="1"> <charact T="2" m="哈哈"/> <table>你快递</table> </item> </libs> </config>
找到节点的位置接口: bool GetNodePointerByName(TiXmlElement* pRootEle,char *strNodeName,const char *id, TiXmlElement* &Node) {
//TiXmlElement *pElement = myDocument->RootElement(); if (NULL == pRootEle)
return false;
TiXmlElement *temproot = pRootEle; pRootEle = pRootEle->FirstChild("libs")->ToElement(); for (temproot = pRootEle->FirstChildElement();temproot;temproot = temproot->NextSiblingElement())
if (0 == strcmp(libid,temproot->Attribute("libid"))){
Node = temproot;return true;
}
return false;} 1.删除<item>........ </item>这样一个节点。 xmlpath是xml文件的路径,strNodeName是节点的名字,此处是item,id就是区分是哪一个节点要用的。
bool del_node(const char *xmlpath,char *strNodeName,const char *id) {
bool flags = false; TiXmlDocument *pDoc = new TiXmlDocument(); if (NULL==pDoc) {
goto END;
} pDoc->LoadFile(xmlpath); TiXmlElement *pRootEle = pDoc->RootElement(); if (NULL==pRootEle) {
goto END;
} TiXmlElement *pNode = NULL; GetNodePointerByName(pRootEle, strNodeName,id, pNode);//获取节点的位置。 // 假如是根节点 if (pRootEle==pNode) {
if(pDoc->RemoveChild(pRootEle)){
pDoc->SaveFile(xmlpath);goto END;
}else
goto END;
} // 假如是其它节点 if (NULL!=pNode) {
TiXmlNode *pParNode = pNode->Parent();if (NULL==pParNode){
goto END;
}TiXmlElement* pParentEle = pParNode->ToElement();if (NULL!=pParentEle){
if(pParentEle->RemoveChild(pNode))//删除节点
pDoc->SaveFile(xmlpath);//保存
else
goto END;
}
} else {
goto END;
} flags = true; END: if (pDoc != NULL) {
delete pDoc;
} return flags;}
2.修改节点的内容 通过id查找到需要修改的节点,然后遍历整个节点,把需要修改的地方SetValue新的值,最后保存 bool modifynode_attribute(const char *xmlpath,const char *id,char *strNodeName,char *T,char *m,char *table) {
TiXmlDocument *pDoc = new TiXmlDocument(); if (NULL==pDoc) {
return false;
} pDoc->LoadFile(xmlpath); TiXmlElement *pRootEle = pDoc->RootElement(); if (NULL==pRootEle) {
delete pDoc;return false;
} TiXmlElement *pNode = NULL; GetNodePointerByName(pRootEle,strNodeName,id,pNode); if (NULL!=pNode) {
char *name = "";pNode = pNode->FirstChildElement();TiXmlAttribute* pAttr = NULL;for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next()) //遍历父节点的所有子节点{
name = (char *)pAttr->Name();if(strcmp(name, "id") == 0 ){
pAttr->SetValue(id);
}else if (strcmp(name, "T") == 0 ){
pAttr->SetValue(T);
}else if (strcmp(name, "m") == 0 ){
pAttr->SetValue(m);
}
}pNode = pNode->NextSiblingElement();pNode->FirstChild()->SetValue(table);pDoc->SaveFile(xmlpath);delete pDoc;return true;
} else {
delete pDoc;return false;
}} 3.添加节点 实现添加节点,先找到你要添加节点的位置,然后把你要添加的子节点,事先给建立好,在这就是<item>.....</item>,最后就使用InsertEndChild,把新建的子节点插入到想加的位置。 bool IS_get_tableinfo::addnode(const char *XmlFile,char *strNodeName, const char *id, char *T, char *m, char *table) {
// 定义一个TiXmlDocument类指针 TiXmlDocument *pDoc = new TiXmlDocument(); if (NULL==pDoc) {
return false;
} pDoc->LoadFile(XmlFile); TiXmlElement *pRootEle = pDoc->RootElement(); if (NULL==pRootEle) {
delete pDoc;return false;
} // TiXmlElement *pNode = NULL; //GetNodePointerByName(pRootEle,strNodeName,id,pNode); TiXmlElement *pElement = pDoc->RootElement(); if (NULL == pElement) {
delete pDoc;return false;
} pElement = pElement->FirstChild("libs")->ToElement();//找到父节点libs位置 if (NULL!=pElement) {
// 创建添加的子节点:pNewNodeTiXmlElement *pNewNode = new TiXmlElement(strNodeName);if (NULL==pNewNode){
delete pDoc;return false;
}// 设置节点的属性值,然后插入节点pNewNode->SetAttribute("id",id);TiXmlElement *pNode = new TiXmlElement("charact");//添加节点的子子节点charactif (NULL==pNewNode){
delete pDoc;return false;
}pNode->SetAttribute("T",T);pNode->SetAttribute("m",m);pNewNode->LinkEndChild(pNode);//把子节点链接到父节点TiXmlElement *pNodesql = new TiXmlElement("table");//新建table子节点if (NULL == pNodesql){
delete pDoc;return false;
}pNewNode->LinkEndChild(pNodesql);TiXmlText *sql = new TiXmlText(table);pNodesql->LinkEndChild(table);pElement->InsertEndChild(*pNewNode);//插入新子节点pDoc->SaveFile(XmlFile);//保存delete pNodesql;return true;
} else {
delete pDoc;return false;
}}