【发布时间】:2016-03-07 15:52:22
【问题描述】:
我正在使用下面的代码从 xml 中删除名称空间属性,但我没有成功;没有成功。我只想从节点 Action__CompIntfc__CIName
中删除命名空间<Action__CompIntfc__CIName xmlns="http://schemas.xmlsoap.org/soap/encoding/">
下面是我的代码
procedure TForm1.Button1Click(Sender: TObject);
var
xmldoc : IXMLDOMDocument;
xmlString : WideString;
RecNodelist: IXMLDOMNodeList;
DataNode: IXMLDOMElement;
attr : IXMLDOMAttribute;
begin
xmlString := '<?xml version="1.0"?>'
+'<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">'
+'<SOAP-ENV:Body>'
+'<Action__CompIntfc__CIName xmlns="http://schemas.xmlsoap.org/soap/encoding/">'
+'<test>1</test>'
+'</Action__CompIntfc__CIName>'
+'<Action__CompIntfc__CIName xmlns="http://schemas.xmlsoap.org/soap/encoding/">'
+'<test>15</test>'
+'</Action__CompIntfc__CIName>'
+'</SOAP-ENV:Body>'
+'</SOAP-ENV:Envelope>';
try
XMLdoc := CoDOMDocument.Create;
xmldoc.loadXML(xmlString);
RecNodelist := XMLdoc.selectNodes('//SOAP-ENV:Envelope/SOAP-ENV:Body/Action__CompIntfc__CIName');
DataNode := RecNodelist.NextNode as IXMLDOMElement;
while DataNode <> nil do
begin
showmessage(DataNode.xml);
attr := DataNode.getAttributeNode('xmlns');
DataNode.removeAttributeNode(attr);
showmessage(DataNode.xml);
DataNode := RecNodelist.NextNode as IXMLDOMElement;
end;
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
end;
从下面的节点从 XML 中删除命名空间 "xmlns="http://schemas.xmlsoap.org/soap/encoding/" 后
<Action__CompIntfc__CIName xmlns="http://schemas.xmlsoap.org/soap/encoding/">
我希望我的 xml 是
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<Action__CompIntfc__CIName>
<test>1</test>
</Action__CompIntfc__CIName>
<Action__CompIntfc__CIName>
<test>15</test>
</Action__CompIntfc__CIName>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
【问题讨论】:
-
那么你的问题是什么?你实际得到了什么结果?
-
你不能改变DOM树中节点的命名空间,你需要在没有命名空间的不同命名空间中分别创建新的节点。例如,XSLT 可以做到这一点,看起来您的 Delphi 代码使用支持 XSLT 的 MSXML,所以请告诉我们您是否可以使用 XSLT 解决方案。
-
@MartinHonnen:“您无法更改节点的命名空间”确实如此。在我的回答中,我已按照您的建议将有问题的节点替换为新创建的节点,但没有使用 XSLT。
标签: xml delphi namespaces delphi-7