SOAP Send and Read Complex Types Using XPath
SOAP services often require structured XML data (complex types) instead of simple parameters. The SOAP component supports building full XML payloads and reading complex responses using XPath navigation.
To send complex types, you must prevent automatic XML escaping so the component can build the full XML structure correctly. This is done by setting ValueFormat to vfFullXML, allowing raw XML fragments to be included in the request body. For instance:
Sending Complex Types
// C#
soap.Method = "test";
soap.MethodURI = "http://tempuri.org";
soap.ValueFormat = SoapValueFormats.vfFullXML;
soap.AddParam("parent", "test");
soap.BuildPacket();
// Java
soap.setMethod("test");
soap.setMethodURI("http://tempuri.org");
soap.setValueFormat(soap.vfFullXML);
soap.addParam("parent", "test");
soap.buildPacket();
This produces a SOAP packet with the following structure:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:="">
<SOAP-ENV:Body>
<m:test xmlns:m="http://tempuri.org">
<parent>
<child1>
<grandchild1>test</grandchild1>
</child1>
</parent>
</m:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If the server returns a complex type in response to your request you can use the XPath and related properties to traverse the response. For instance if the server returned the value:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:="">
<SOAP-ENV:Body>
<m:TestResults xmlns:m="http://tempuri.org">
<parent>
<child1>
<grandchild1>value1</grandchild1>
<grandchild2>value2</grandchild2>
</child1>
</parent>
</m:TestResults>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Reading Complex Types Using XPath
You could use the code below to output the value for the grandchild2 element. Please consult the help files for a complete list of XPath related properties that may be useful in your application.
C#
soap.XPath = "/Envelope/Body/TestResults/parent/child1/grandchild2";
Console.WriteLine(soap.XText);
Java
soap.setXPath("/Envelope/Body/TestResults/parent/child1/grandchild2");
System.out.println(soap.getXText());
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.