我正在使用下面的代碼從XML刪除名稱空間屬性,但我沒有成功。我只想從節點刪除命名空間Action__CompIntfc__CIName如何使用Delphi 7從XML中刪除名稱空間
<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;
刪除命名空間 「的xmlns =」 後http://schemas.xmlsoap.org/soap/encoding/ 「從XML從下面節點
<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>
那麼你的問題是什麼?你實際得到了什麼結果? – MartynA
您無法更改DOM樹中某個節點的名稱空間,因此您需要分別在不使用名稱空間的情況下在不同名稱空間中創建新節點。例如XSLT可以做到這一點,它看起來像你的Delphi代碼使用支持XSLT的MSXML,所以告訴我們你是否可以使用XSLT解決方案。 –
@MartinHonnen:「你不能改變節點的名字空間」確實如此。在我的回答中,我已經用新創建的代替了所討論的節點,正如你所建議的那樣,但是不使用XSLT。 – MartynA