Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Rename Element(TagName) in DOM

Rename Element(TagName) in DOM

Scheduled Pinned Locked Moved XML / XSL
htmltutorialquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fastfootskater
    wrote on last edited by
    #1

    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

    F 1 Reply Last reply
    0
    • F Fastfootskater

      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

      F Offline
      F Offline
      Fastfootskater
      wrote on last edited by
      #2

      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->

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups