我想改变MSXML XMLDOMElement的标签名,但遗憾的是nodeName属性为只读。 有没有简单的方法来做到这一点,或者我做一些插入解决/更换和深拷贝的孩子吗?
<xml> ...
<oldTagName>
... sub-elements
</oldTagName>
<more xml> ...
应该成为
<xml> ...
<newTagName>
... sub-elements
</newTagName>
<more xml> ...
根据文档对象模型不能重命名的节点。
请参阅: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247
renameNode有可能在DOM Level 3的,但不是在MSXML库
这是使用MSXML接口“通用的”重命名节点功能(ObjectPascal),工作正常,并可以是可用的:
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;
仅供参考,我结束了一个替换功能,其复制所有的孩子到新创建的节点:(VB6的示例代码)
Private Sub ReplaceNodeName(oDoc As DOMDocument, oElement As IXMLDOMElement, newName As String)
Dim ohElement As IXMLDOMElement
Dim sElement As IXMLDOMElement
Dim oChild As IXMLDOMNode
' search the children '
If Not oElement Is Nothing Then
Set ohElement = oElement.parentNode
Set sElement = oDoc.createElement(newName)
For Each oChild In oElement.childNodes
Call sElement.appendChild(oChild)
Next
Call ohElement.replaceChild(sElement, oElement)
End If
End Sub
这是我的C ++版本,也许你就会明白它更容易:
#define SAFERELEASE(comobj) \
if (comobj) \
{ \
comobj->Release(); \
comobj = NULL; \
}
void RenameElement(IXMLDOMElement * pElement, LPWSTR newName)
{
if (NULL == pElement)
return;
IXMLDOMDocument * pParentDoc = NULL;
pElement->get_ownerDocument(&pParentDoc);
IXMLDOMElement * pNewElement = NULL;
pParentDoc->createElement(newName, &pNewElement);
VARIANT_BOOL bHasNodes;
pElement->hasChildNodes(&bHasNodes);
while (bHasNodes)
{
IXMLDOMNode * pFirstChild = NULL;
pElement->get_firstChild(&pFirstChild);
pNewElement->appendChild(pFirstChild, NULL);
pElement->hasChildNodes(&bHasNodes);
SAFERELEASE(pFirstChild);
}
IXMLDOMNamedNodeMap * pAttrMap = NULL;
IXMLDOMNamedNodeMap * pAttrMapNew = NULL;
pElement->get_attributes(&pAttrMap);
pNewElement->get_attributes(&pAttrMapNew);
long nAttrLength = 0;
pAttrMap->get_length(&nAttrLength);
for (int n = 0; n < nAttrLength; n++)
{
IXMLDOMNode * pListItem = NULL;
pAttrMap->get_item(n, &pListItem);
BSTR wsAttrName = NULL;
pListItem->get_nodeName(&wsAttrName);
BSTR wsAttrValue = NULL;
pListItem->get_text(&wsAttrValue);
IXMLDOMAttribute * pAttribute = NULL;
pParentDoc->createAttribute((BSTR)wsAttrName, &pAttribute);
pAttribute->put_value(CComVariant((BSTR)wsAttrValue));
pAttrMapNew->setNamedItem(pAttribute, NULL);
SAFERELEASE(pAttribute);
SysFreeString(wsAttrValue);
SysFreeString(wsAttrName);
SAFERELEASE(pListItem);
}
IXMLDOMNode * pParent = NULL;
pElement->get_parentNode(&pParent);
pParent->replaceChild(pNewElement, pElement, NULL);
SAFERELEASE(pAttrMapNew);
SAFERELEASE(pAttrMap);
SAFERELEASE(pParent);
SAFERELEASE(pNewElement);
SAFERELEASE(pParentDoc);
}