Rename Element(TagName) in DOM
-
Hello, does anybody know how to rename a node in DOM? I have copied(cloned) a node and placed on another place. Now it would be nice to rename the node. It should be like this: Before: After: Greetings Juergen
-
Hello, does anybody know how to rename a node in DOM? I have copied(cloned) a node and placed on another place. Now it would be nice to rename the node. It should be like this: Before: After: Greetings Juergen
After spending almost three houres of time I have found a solution for this "simple" problem If there are other solutions so tell them. OK here it is. In the DOM object there is no possibility to rename the node. You have to do this in a work around. - Create an Element Object with the desired name - Then copy all attributes and childnodes from the target node to the new newelmentnode - Finally replace the nodes. In VC++ it looks like this: BOOL CTssHardwareXml::RenameActualNode( CString strBefore, CString strAfter ) { HRESULT hr,hr1,hr2; IXMLDOMNode* pNodeActual = NULL; IXMLDOMNodeList* pIXMLDOMNodeList = NULL; IXMLDOMElement* pElementNew = NULL; IXMLDOMNode* pNodeTemp = NULL; IXMLDOMNode* pNodeClone = NULL; IXMLDOMNode* pNodeSuccessFull = NULL; IXMLDOMNamedNodeMap* pAttributeMapActual = NULL; IXMLDOMNamedNodeMap* pAttributeMapNew = NULL; DWORD dwErrorCount = 0; long lLength = 0; _bstr_t bstrNodeName = strAfter; CString strSelctionNode; strSelctionNode.Format("./%s",strBefore); BSTR bstrAttributeName; VARIANT varAttributeValue; try{ hr = m_pIXMLDOMNode->selectNodes( _bstr_t(strSelctionNode),&pIXMLDOMNodeList); if(SUCCEEDED(hr) && pIXMLDOMNodeList){ // In this version just only the first one will be renamed !!!! //------------------------------------------------------------- hr = pIXMLDOMNodeList->get_item(0,&pNodeActual); if(!(SUCCEEDED(hr) && pNodeActual)){ dwErrorCount++; } pIXMLDOMNodeList->Release(); } // Create a new Element hr = m_pIXMLDOMDocument2->createElement( bstrNodeName,&pElementNew); if( !dwErrorCount && SUCCEEDED(hr) && pElementNew ){ // Copy Attributes from Actual to NewElement hr1 = pNodeActual->get_attributes(&pAttributeMapActual); hr2 = pElementNew->get_attributes(&pAttributeMapNew); if(SUCCEEDED(hr1) && SUCCEEDED(hr2) && pAttributeMapActual && pAttributeMapNew){ hr = pAttributeMapActual->get_length(&lLength); if(SUCCEEDED(hr)){ //Loop throug Map Attribute for (long i = 0; i < lLength ;i++){ pNodeTemp = NULL; pNodeSuccessFull= NULL; pNodeClone = NULL; hr = pAttributeMapActual->nextNode(&pNodeTemp); if(SUCCEEDED(hr) && pNodeTemp){ hr1 = pNodeTemp->get_nodeName(&bstrAttributeName); hr2 = pNodeTemp->get_nodeValue(&varAttributeValue); if(SUCCEEDED(hr1) && SUCCEEDED(hr2)){ IXMLDOMAttribute *pIXMLDOMAttribute = NULL; hr = m_pIXMLDOMDocument2->