Element renameElementAndCloneChild(Element source,Document targetDocument,String newName){
  Element newElement=targetDocument.createElement(newName);
  NamedNodeMap namedNodeMap=source.getAttributes();
  for (int iAttr=0; iAttr < namedNodeMap.getLength(); iAttr++) {
    Node node=namedNodeMap.item(iAttr);
    newElement.setAttribute(node.getNodeName(),node.getNodeValue());
  }
  NodeList listofChilds=source.getChildNodes();
  for (int iChild=0; iChild < listofChilds.getLength(); iChild++) {
    Node node=listofChilds.item(iChild);
    newElement.appendChild(node.cloneNode(true));
  }
  return newElement;
}

Element renameElementAndCloneChild ( Element source , Document targetDocument , String newName ) { Element newElement = targetDocument . createElement ( newName ) ; NamedNodeMap namedNodeMap = source . getAttributes ( ) ; for ( int iAttr = 0 ; iAttr < namedNodeMap . getLength ( ) ; iAttr ++ ) { Node node = namedNodeMap . item ( iAttr ) ; newElement . setAttribute ( node . getNodeName ( ) , node . getNodeValue ( ) ) ; } NodeList listofChilds = source . getChildNodes ( ) ; for ( int iChild = 0 ; iChild < listofChilds . getLength ( ) ; iChild ++ ) { Node node = listofChilds . item ( iChild ) ; newElement . appendChild ( node . cloneNode ( true ) ) ; } return newElement ; }
renames an element and preserves its child elements
