/************************************************************************** * name : AddElemBeforeCurNode * description: 在当前节点前添加元素 * input : pszElemName 待添加的元素名 * output : NA * return : true - 成功,false - 失败 * remark : NA **************************************************************************/ bool CXml::AddElemBeforeCurNode(const char *pszElemName) { CHECK_POINTER(pszElemName, false); //Tinyxml会管理所有的申请的内存,不需要调用者delete TiXmlElement *pXmlAddElem = NULL; try { pXmlAddElem = new TiXmlElement(pszElemName); } catch (...) { pXmlAddElem = NULL; return false;//lint !e438 } CHECK_POINTER(pXmlAddElem, false);//lint !e774 if (NULL == m_pXmlNode) { if (NULL == m_pXMlDoc) { SAFE_NEW(m_pXMlDoc, TiXmlDocument);//lint !e774 } CHECK_POINTER(m_pXMlDoc, false);//lint !e774 (void)(m_pXMlDoc->LinkEndChild(pXmlAddElem)); GetRootPos(); return true; } // 判断指针是否为空 CHECK_POINTER(m_pXmlNode, false); //lint !e774 TiXmlNode *pParent = m_pXmlNode->Parent(); CHECK_POINTER(pParent,false); TiXmlNode *pXmlNode = pParent->InsertBeforeChild(m_pXmlNode,(*pXmlAddElem)); if (NULL == pXmlNode) { return false; } m_pXmlNode = pXmlNode; m_pXmlElem = m_pXmlNode->ToElement(); return true; }
void GD_EXTENSION_API InsertElementIntoAnother(const std::string &refNameOfElementToAdd, const std::string &refNameOfParentElement, const std::string &refNameOfNextElement, RuntimeScene &scene) { TiXmlNode *parentEle = RefManager::GetInstance(&scene)->GetRef(refNameOfParentElement); TiXmlNode *nextEle = RefManager::GetInstance(&scene)->GetRef(refNameOfNextElement); TiXmlNode *toBeAddedEle = RefManager::GetInstance(&scene)->GetRef(refNameOfElementToAdd); if(!nextEle || nextEle->Parent() != parentEle) { parentEle->LinkEndChild(toBeAddedEle); } else { TiXmlNode *insertedEle = 0; insertedEle = parentEle->InsertBeforeChild(nextEle, *toBeAddedEle); RefManager::GetInstance(&scene)->SetRef(refNameOfElementToAdd, insertedEle); } }
void GD_EXTENSION_API InsertElementIntoAnother(const gd::String &refNameOfElementToAdd, const gd::String &refNameOfParentElement, const gd::String &refNameOfNextElement, RuntimeScene &scene) { TiXmlNode *parentEle = RefManager::Get(&scene)->GetRef(refNameOfParentElement); TiXmlNode *nextEle = RefManager::Get(&scene)->GetRef(refNameOfNextElement); TiXmlNode *toBeAddedEle = RefManager::Get(&scene)->GetRef(refNameOfElementToAdd); if ( parentEle == NULL || toBeAddedEle == NULL ) return; //These element cannot be invalid else { if(!nextEle || nextEle->Parent() != parentEle) { parentEle->LinkEndChild(toBeAddedEle); } else { TiXmlNode *insertedEle = 0; insertedEle = parentEle->InsertBeforeChild(nextEle, *toBeAddedEle); RefManager::Get(&scene)->SetRef(refNameOfElementToAdd, insertedEle); } } }
//修改结点struNote。或返回结点struNode的值 bool CSimpleXml::AccessXmlNode( XMLNODEINFO &struNote, int nType) { int nEnd = 0; if(m_pDoc == NULL || m_pRoot == NULL) { return false; } TiXmlText* pText = NULL; // 一个指向Text的指针 pText = new TiXmlText(struNote.strData.c_str()) ; string strNodeName = struNote.strNodeName; nEnd = strNodeName.find("/"); string strNode = strNodeName.substr(0, nEnd); strNodeName = strNodeName.substr(nEnd + 1, strNodeName.length() - nEnd); TiXmlNode *node = m_pRoot; TiXmlNode *destnode = NULL; while(node) { string strchildnode = node->Value(); if(strNode != strchildnode) { node = node->NextSibling(); continue;//此子结点非我们要的子结点,跳到下一个子结点 } if((strNode == strNodeName) && (strNode == node->Value()))//Node就是我们访问的结点。 { destnode = node->FirstChild(); if(destnode && destnode->Type() == TiXmlNode::TEXT)//是叶子结点,修改为我们的值 { if(nType == QUERY) { struNote.strData = destnode->Value();//查询结点的值 } else { destnode->SetValue(struNote.strData.c_str());//修改为我们的值 } } else if(destnode && destnode->Type() == TiXmlNode::ELEMENT)//不是叶子结点,加上我们的值 { node->InsertBeforeChild(destnode, *pText); } else if(!destnode)//要写的结点原值为空,加上我们的值 { node->InsertEndChild(*pText); } return true; } node = node->FirstChild();//Node不是我们访问的结点,开始遍历Node的子结点 nEnd = strNodeName.find("/"); strNode = strNodeName.substr(0, nEnd); strNodeName = strNodeName.substr(nEnd + 1, strNodeName.length() - nEnd); if(node && (strNode == strNodeName) && (strNode == node->Value()))//Node就是我们访问的结点 { destnode = node->FirstChild(); if(destnode && destnode->Type() == TiXmlNode::TEXT)//是叶子结点,加上我们的值 { if(nType == QUERY) { struNote.strData = destnode->Value();//查询结点的值 } else { destnode->SetValue(struNote.strData.c_str());//修改为我们的值 } } else if(destnode && destnode->Type() == TiXmlNode::ELEMENT)//不是叶子结点,加上我们的值 { node->InsertBeforeChild(destnode, *pText); } else if(!destnode)//要写的结点原值为空,加上我们的值 { node->InsertEndChild(*pText); } return true; } } return false; }