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 str1 = "<should be escaped>"
local str2 = '"should also be &escaped"'
local escaped1 = str1:gsub("<", "<"):gsub(">", ">")
local escaped2 = str2:gsub("&", "&"):gsub('"', """)
local escaped_element = {
tag = "StringEscapingTest",
{ tag = "string", escaped1, },
{ tag = "string", escaped2, },
} --> <StringEscapingTest><string><should be escaped></string><string>"should also be &escaped"</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.
require "soap.client"
local ns, meth, ent = soap.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