Encode/decode XML payloads.
Codec ID: xml
Properties:
Name | Type | Default value | Description |
---|---|---|---|
encoding |
string | utf-8 |
encoding: use this to encode the payload. Decoding: assumes the payload follows this encoding if the xml prolog encoding attribute is missing. |
write_prolog |
boolean | True |
encoding: if True, write the <?xml version="1.0" encoding="..."?> prolog. |
prettyprint |
boolean | False |
encoding: if True, pretty print the XML (carriage returns, hard tabs) |
This codec enables to encode/decode XML strings from/to Testerman message structures.
Limitations:
Something like
<p>In this text, <i>some elements</i> are nested</p>
will be decoded to:
('p', { 'value': u"In this text, <i>some elements</i> are nested" })
I.e. the nested element won’t be constructed; whereas:
<p><i>nested element</i></p>
will be decoded to
('p', { 'children': [ ('i', 'value': u"nested element") ] })
If you don’t need to manage attributes and CDATA sections, you may consider CodecXerLite.
Input:
<element attr="attrval">
<subelement>subvalue</subelement>
<subelement2><![CDATA[cdata value]]></subelement2>
</element>
Decoded message:
('element', { 'attributes': { 'attr': 'attrval' }, 'children': [
('subelement', { 'value': 'subvalue', 'cdata': False }),
('subelement2', { 'value': 'cdata value', 'cdata': True }),
]}
)
cdata
is always provided during decoding.
Input:
('element', { 'attributes': { 'attr': 'attrval' }, 'children': [
('subelement', { 'value': 'subvalue' }),
('subelement2', { 'value': 'cdata value', 'cdata': True }),
]})
Encoded message:
<element attr="attrval">
<subelement>subvalue</subelement>
<subelement2><![CDATA[cdata value]]></subelement2>
</element>
If cdata
is not present during a value element encoding, it is assumed to be False
.
If both children
and value
are provided, children
has a greater precedence and value
will be ignored.
type record Element
{
Attributes attributes,
// if children is present, cdata and value are not present.
// if cdata and value are present, children it not present.
boolean cdata optional,
universal charstring value optional,
record of Element children optional
}
type record Attributes
{
// field name depends on the available attributes. All types are universal charstring
universal charstring name
}