Bar

How to set attributes for SOAP elements in Delphi?

In the SOAP client request, the header needs this line:

<NS2:Header Name="Foo">Bar<NS2:Header>

So, I wrote a class that has two string properties:

  • Content property - for element value ("Bar")
  • Name property - for attribute value ("Foo")

The AS_ATTRIBUTE flag of the Name property should indicate that it is an XML attribute.

   Header = class(TSoapHeader)
   private
     FContent: string;
     FName: string;
   published
     property Content: string read FContent write FContent;
     property Name: string read FName write FName stored AS_ATTRIBUTE;
   end;

and register using

   RemClassRegistry.RegisterXSClass(Header, MY_URI);
   RemClassRegistry.RegisterSerializeOptions(Header, [xoLiteralParam, 
xoSimpleTypeWrapper]);

The xoLiteralTypWrapper parameter specifies that the class should only "wrap" the value of the Content property in the element and not add a nested element to it.

For Name: = "Foo" and Content: = "Bar", this will be the result of the XML in the SOAP request:

<NS2:Header Name="Foo">
   <Content xsi:type="xsd:string">Bar</Content>
</NS2:Header>

, , . Name , Content , :

<NS2:Header>Bar</NS2:Header>

, - , xoSimpleTypeWrapper.

+3
1

, . ObjectToSOAP SOAPToObject.

ObjectToSOAP TSOAPHeader SOAP .

, , :

function Header.ObjectToSOAP(RootNode, ParentNode: IXMLNode; 
  const ObjConverter: IObjConverter; const Name, URI: InvString; 
  ObjConvOpts: TObjectConvertOptions; out RefID: InvString): IXMLNode;  
begin 
  ObjConvOpts := ObjConvOpts + [ocoDontSerializeProps]; 
  Result := inherited ObjectToSOAP(RootNode, ParentNode, ObjConverter, Name, URI, ObjConvOpts, RefID); 
  Result.Text := FContent; 
  Result.Attributes['Name'] := FName;
end; 
+3

Source: https://habr.com/ru/post/1708105/


All Articles