LuaSOAP
SOAP interface for the Lua programming language

Examples

SOAP elements

Follows a small example of common XML constructions written in LuaSOAP.
local simple = { tag = "symbol", "DEF", } --> <symbol>DEF</symbol>

local simple_attr = {
	tag = "symbol",
	anAttribute = "aValue",
	"DEF",
} --> <symbol anAttribute="aValue">DEF</symbol>

local structured = {
	tag = "PriceAndVolume",
	{ tag = "LastTradePrice", 34.5, },
	{ tag = "DayVolume", 10000, },
} --> <PriceAndVolume><LastTradePrice>34.5</LastTradePrice><DayVolume>10000</DayVolume></PriceAndVolume>

local structured_attr = {
	{ tag = "stooges",
		{
			tag = "curly",
			attr = { "xsi:type", ["xsi:type"] = "xsd:int", },
			-21,
		},
		{
			tag = "larry",
			attr = { "xsi:type", ["xsi:type"] = "xsd:int", },
			59,
		},
		{
			tag = "moe",
			attr = { "xsi:type", ["xsi:type"] = "xsd:int", },
			11,
		},
	},
} --> <stooges><curly xsi:type="xsd:int">-21</curly><larry xsi:type="xsd:int">59</larry><moe xsi:type="xsd:int">11</moe></stooges>

Escaping example

Below is an example of a simple way of escaping strings that contain special characters. Careful must be taken to non-ASCII characters since they have to be converted according to the locale/encoding.
local escaped_element = {
	tag = "StringEscapingTest",
	{ tag = "string", "<this was automatically escaped>", },
	{ tag = "string", 'do not re-escape my &', },
} --> <StringEscapingTest><string>&lt;this was automatically escaped&gt;</string><string>do not re-escape my &amp;</string></StringEscapingTest>

Client example

Below is a small sample code displaying the use of the library in a client application. Note that the external tag might not be provided in the entries table, since the method field will be used for that purpose.
local client = require "soap.client"
local ns, meth, ent = client.call {
	url = "http://soap.4s4c.com/ssss4c/soap.asp", 
	soapaction = "doubler",
	method = "http://simon.fell.com/calc",
	entries = { -- `tag' will be filled with `method' field
		{
			tag = "nums",
			attr = {
				["xmlns:SOAP-ENC"] = "http://schemas.xmlsoap.org/soap/encoding/",
				["SOAP-ENC:arrayType"] = "xsd:int[5]",
			},
			{ tag = "number", 10 },
			{ tag = "number", 20 },
			{ tag = "number", 30 },
			{ tag = "number", 50 },
			{ tag = "number", 100 },
		},
	}
}
print("namespace = ", ns, "element name = ", meth)
for i, elem in ipairs (ent[1]) do
	print (elem[1])
end

Valid XHTML 1.0!

$Id: examples.html,v 1.2 2009/07/22 19:02:46 tomas Exp $