Complex Types using the Node and Nodes objects

Any non-trivial web service will require you to handle complex types. There are two basic approachs in pocketSOAP for working with complex types, you can use serializers that map between SOAP complex types and objects within your system, or you can work with the node objects and collections directly. This section contains details on working with the Node and Nodes objects, the advanced topics section details writing custom serializers.

Sample complex type

The Apache SOAP project includes a sample called the addressbook sample, in this it defines a complex type for a phone number and a complex type for an address, which includes a phone number. Here's an example of an instance of the address type.
<address>
	<number>100</number>
	<street>Spear Street</street>
	<city>San Francisco</city>
	<state>CA</state>
	<zip>94107</zip>
	<phone>
		<area>415</area>
		<exchange>343</exchange>
		<number>3000</number>
	</phone>
</address>

Deserialization

In the absense of any deserializer registered to handle a particular complex type, pocketSOAP will deserialize the complex type using a generic complex type deserializer. The generates a nested collection of Node objects, one per element. Once parsing is finished, the pocketSOAP application can use the Nodes & Value properties to access the tree of nodes. for example, we make a SOAP call, and this is the response
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body s:EncodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<m:getAddressResponse xmlns:m="http://address.examples.org/schema/">
<address>
	<number>100</number>
	<street>Spear Street</street>
	<city>San Francisco</city>
	<state>CA</state>
	<zip>94107</zip>
	<phone>
		<area>415</area>
		<exchange>343</exchange>
		<number>3000</number>
	</phone>
</address>
</m:getAddressResponse>
</s:Body>
</s:Envelope>
The following code will access the fields of the complex type returned.
' Assuming env is a pocketSOAP Envelope object, and http is a transport object, and that we've sucessfully built and sent the request.

' parse the response
env.parse http

' address is the first child in the response parameters, so lets grab that first
dim adr
set adr = env.Parameters.Item(0).Value

' now we simply use the ItemByName function on the Nodes collection class to access each contained field
wscript.echo adr.Nodes.ItemByName("number").Value & " " & adr.Nodes.ItemByName("street").value
wscript.echo adr.Nodes.ItemByName("city").Value & " " & adr.Nodes.ItemByName("state").value
wscript.echo adr.Nodes.ItemByName("zip").Value

' as phone is a nested complex type, we grab the phone node, then look at its children
dim ph
set ph = adr.Nopdes.ItemByName("phone")
wscript.echo ph.Nodes.ItemByName("area").value & " " & ph.Nodes.ItemByName("exchange").value & " " & ph.Nodes.ItemByName("number").value

Serialization

Sending complex types works in the reverse manner, the application builds a nested set of node objects, which get serialized into the complex type when serialize is called.
dim env, adr, ph
set env = CreateObject("pocketSOAP.Envelope.2")
env.SetMethod "addAddress", "http://address.example.org/"
set adr = env.Parameters.Create ("address", empty)
adr.Nodes.Create "number", "100"
adr.Nodes.Create "street", "Spear Street"
adr.Nodes.Create "city",   "San Francisco"
adr.Nodes.Create "state",  "CA"
adr.Nodes.Create "zip",    "94107"
set ph = adr.Nodes.Create ("phone", empty)
ph.Nodes.Create "area", "415"
ph.Nodes.Create "exchange", "343"
ph.Nodes.Create "number", "3000"

wscript.echo env.serialize



Copyright

Copyright © Simon Fell, 2000-2004. All rights reserved.