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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
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.
"""
...
"""
High-performance XML parser. Converts XML string, bytes, or file to a Python dictionary.
:param xml_input:
XML content as a string, bytes, or a file-like object (must implement .read()).
:param attr_prefix:
Prefix to use for attributes in the resulting dict (default: "@").
:param cdata_key:
Key to use for text content (default: "#text").
:param force_cdata:
If True, always puts text content into `cdata_key`, even for simple elements.
E.g., <a>b</a> becomes {'a': {'#text': 'b'}} instead of {'a': 'b'}.
:param process_namespaces:
If True, expands namespaces into the keys (e.g., 'http://example.com:tag').
If False (default), keeps the prefix as-is (e.g., 'ns:tag').
:param namespace_separator:
Separator to use when expanding namespaces (default: ":").
:param strip_whitespace:
If True (default), trims leading/trailing whitespace from text values.
:param force_list:
A list of tag names that should ALWAYS be parsed as a list,
even if they only appear once in the XML.
:param process_comments:
If True, comments are included in the dictionary using the key '#comment'.
:return:
A Python dictionary representing the XML structure.
"""
...