module xml.tf
The tweakstreet/xml
module helps working with XML documents.
library xml
The xml library provides functions that help working with XML data.
stringify
(dict data, boolean pretty_print=true) -> string
Returns nil
if data
is nil
.
Otherwise, returns an XML representation of given data. Encodes all content with XML entities - such as <
instead of <
as required.
The data
argument describes the root node of the XML.
If pretty_print
is true, indentation whitespace is included in the output.
All data nodes are processed recursively to generate the entire XML tree. A data node is interpreted based on its type:
Type | Handling |
---|---|
dict | an XML tag is created, the following keys have special meaning:
|
list | each entry is converted to a node |
numeric | text node |
string | text node |
boolean | text node of ‘true’ or ‘false’ |
datetime | text node encoded using the ISO format pattern uuuu-MM-dd'T'HH:mm:ss.SSSZZZZZ |
binary | text node using base64 encoding of binary value |
function | an error is thrown, functions cannot be encoded as XML |
void | nil values do not generate XML nodes, they are omitted |
> xml.stringify({"#name" "root", '@attr' 1, :foo "bar"})
"<root attr=\"1\">
<foo>bar</foo>
</root>
"
> \e
xml.stringify({
'#name' 'soap12:Envelope',
'@xmlns:soap12' "http://www.w3.org/2003/05/soap-envelope",
'soap12:Body' {
:ListOfContinentsByName {
'@xmlns' 'http://www.oorsprong.org/websamples.countryinfo'
}
}
})
> \e
"<soap12:Envelope xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">
<soap12:Body>
<ListOfContinentsByName xmlns=\"http://www.oorsprong.org/websamples.countryinfo\"/>
</soap12:Body>
</soap12:Envelope>
"