2011-01-12 149 views
1

我一直有这个问题很长一段时间了,我无法解决它自己。我试过Google,Bing和stackOverflow?没有运气...如何使用TXMLDocument手动构建肥皂信封(Delphi 2006)

我试图手动构建一个SOAP头使用Delphi 2006中的TXMLDocument的组件:

... ... ... ... ... ...

我在做什么是我要构建一个所谓的新元素“肥皂:信封”。在这个新元素中,我创建了三个名为“xmlns:soap”,“xmlns:xsd”和“xmlns:xsi”的属性。

当我试图写在任何三个属性,然后我得到下面的错误值:

试图修改只读节点。

有没有人知道如何使用TXMLDocument来完成这项任务?

/布赖恩

+0

<皂提供的代码:信封的xmlns:SOAP =“HTTP://schemas.xmlsoap。 org/soap/envelope /“xmlns:xsd =”http://www.w3.org/2001/XMLSchema“xmlns:xsi =”http://www.w3.org/2001/XMLSchema-instance“> ... ... ... ... ... ...

回答

2

下面的代码在这里工作正常:

procedure WriteSoapFile; 
var 
    Document: IXMLDocument; 
    Envelope: IXMLNode; 
    Body: IXMLNode; 
begin 
    Document := NewXMLDocument; 
    Envelope := Document.AddChild('soap:Envelope'); 
    Envelope.Attributes['xmlns:soap'] := 'schemas.xmlsoap.org/soap/envelope/'; 
    Envelope.Attributes['xmlns:xsd'] := 'w3.org/2001/XMLSchema'; 
    Envelope.Attributes['xmlns:xsi'] := 'w3.org/2001/XMLSchema-instance'; 
    Body := Envelope.AddChild('soap:Body'); 
    Document.SaveToFile('Test.xml'); 
end; 

你应该能够使用TXMLDocument而不是IXMLDocument,它仅仅是个接口周围部件的包装材料。

+0

哇。这样可行。非常感谢!!!我试图使用属性NodeValue设置值:= ....; 我正在使用以下语义(非常简单):MyNode:= Document.CreateNode(....); MyNode.NodeValue:= ....;/Brian –

2

这是我的解决方案,它使用DeclareNamespace声明命名空间:

procedure WriteSoapFile; 
const 
    NS_SOAP = 'schemas.xmlsoap.org/soap/envelope/'; 
var 
    Document: IXMLDocument; 
    Envelope: IXMLNode; 
    Body: IXMLNode; 
begin 
    Document := NewXMLDocument; 
    Envelope := Document.CreateElement('soap:Envelope', NS_SOAP); 
    Envelope.DeclareNamespace('soap', NS_SOAP); 
    Envelope.DeclareNamespace('xsd', 'w3.org/2001/XMLSchema'); 
    Envelope.DeclareNamespace('xsi', 'w3.org/2001/XMLSchema-instance'); 
    Body := Envelope.AddChild('Body'); 
    Document.DocumentElement := Envelope; 
    Document.SaveToFile('Test.xml'); 
end; 

基于在How to set the prefix of a document element in Delphi

+0

也感谢这个解决方案。我爱你们(o;谢谢你,谢谢你,谢谢!!! –

+0

由于问题只是关于头部,我没有打扰设置命名空间,但你的解决方案当然是更清洁。 –