1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
High-performance, feature-rich dictionary to XML converter.
:param input:
The input data to convert. Can be:
- A dictionary representing the XML structure.
- An iterable/generator (for streaming lists).
- A primitive value.
:param output:
Destination to write the XML to. Can be:
- None (returns the XML as a string).
- A string (file path to write to).
- A file-like object (must have a .write() method, like open() or io.BytesIO).
:param encoding:
The encoding to declare in the XML header (default: "utf-8").
:param full_document:
If True, adds the `<?xml ...?>` declaration and ensures a single root element.
If False, generates an XML fragment (useful for streaming chunks).
:param attr_prefix:
The prefix used to identify attributes in the dictionary keys (default: "@").
:param cdata_key:
The key used to identify text content (default: "#text").
:param pretty:
If True, outputs formatted XML with indentation.
:param indent:
The string used for indentation if pretty is True (default: " ").
:param compat:
Compatibility mode.
- "native" (default): Produces clean XML (e.g., self-closing tags `<tag/>` for None).
- "legacy": Emulates legacy behavior (e.g., `<tag></tag>` for None).
:param streaming:
If True, writes to `output` incrementally as the input is traversed.
Useful for huge datasets that do not fit in memory.
:param default:
A callback function `f(obj) -> str` to handle types that are not natively supported
(like datetime objects). Should raise TypeError or ValueError if conversion fails.
:param item_name:
The default tag name used for items in a list/generator if the tag cannot be
inferred from a dictionary key (default: "item").
:param sort_attributes:
If True, sorts attributes alphabetically by name.
Useful for deterministic output and testing.
:param namespaces:
A dictionary mapping prefixes to URIs (e.g., `{'soap': 'http://...'}`).
These will be declared at the root element of the document.
:return:
The generated XML string if `output` is None, otherwise an empty string.
"""
...