Change NodeName of an XML tag element using MSXML

后端 未结 4 1149
滥情空心
滥情空心 2021-01-19 08:03

I\'d like to change the tag name of a MSXML XMLDOMElement, but unfortunately the nodeName property is read-only. Is there any straightforward way to do it, or have

4条回答
  •  一整个雨季
    2021-01-19 08:21

    This is "universal" rename node function (ObjectPascal) using MSXML interfaces, works fine and may be usable:

    function RenameXMLTag(e: IXMLNode; NewName: WideString): IXMLNode;
    var
     Doc : IXMLDocument;
     NewElem, NParent : IXMLNode;
     DNOld, DNNew : IDOMNode;
     AC : IXMLNodeList;
     i: Integer;
    begin
      Doc := e.OwnerDocument;
      NewElem := Doc.CreateNode(NewName, e.NodeType);
      while e.HasChildNodes do
        NewElem.DOMNode.AppendChild(e.DOMNode.firstChild);
      AC := e.AttributeNodes;
      for i := 0 to AC.Count - 1 do
        NewElem.Attributes[AC[i].NodeName] := AC[i].NodeValue;
      NParent := e.ParentNode;
      DNOld := e.DOMNode;
      DNNew := NewElem.DOMNode;
      NParent.DOMNode.replaceChild(DNNew, DNOld);
      Result := NewElem;
    end;
    

提交回复
热议问题