You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(unparse): add selective force_cdata support (bool/tuple/callable)
- Add _should_force_cdata method similar to _should_force_list
- Support tuple of element names and callable functions for force_cdata
- Maintain full backwards compatibility with boolean values
- Add comprehensive tests for all force_cdata scenarios
- Update documentation with consistent examples for force_cdata and force_list
Fixes#375
Copy file name to clipboardExpand all lines: README.md
+46-2Lines changed: 46 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -210,13 +210,13 @@ Parse XML input into a Python dictionary.
210
210
-`xml_attribs=True`: Include attributes in output dict (with `attr_prefix`).
211
211
-`attr_prefix='@'`: Prefix for XML attributes in the dict.
212
212
-`cdata_key='#text'`: Key for text content in the dict.
213
-
-`force_cdata=False`: Force all text content to be wrapped as CDATA.
213
+
-`force_cdata=False`: Force text content to be wrapped as CDATA for specific elements. Can be a boolean (True/False), a tuple of element names to force CDATA for, or a callable function that receives (path, key, value) and returns True/False.
214
214
-`cdata_separator=''`: Separator string to join multiple text nodes. This joins adjacent text nodes. For example, set to a space to avoid concatenation.
215
215
-`postprocessor=None`: Function to modify parsed items.
216
216
-`dict_constructor=dict`: Constructor for dictionaries (e.g., dict or OrderedDict).
217
217
-`strip_whitespace=True`: Remove leading/trailing whitespace in text nodes. Default is True; this trims whitespace in text nodes. Set to False to preserve whitespace exactly.
218
218
-`namespaces=None`: Mapping of namespaces to prefixes, or None to keep full URIs.
219
-
-`force_list=None`: List of keys or callable to force list values. Useful for elements that may appear once or multiple times. Ensures consistent list output. Can also be a callable for fine-grained control.
219
+
-`force_list=None`: Force list values for specific elements. Can be a boolean (True/False), a tuple of element names to force lists for, or a callable function that receives (path, key, value) and returns True/False. Useful for elements that may appear once or multiple times to ensure consistent list output.
220
220
-`item_depth=0`: Depth at which to call `item_callback`.
221
221
-`item_callback=lambda *args: True`: Function called on items at `item_depth`.
222
222
-`comment_key='#comment'`: Key used for XML comments when `process_comments=True`. Only used when `process_comments=True`. Comments can be preserved but multiple top-level comments may not retain order.
@@ -239,6 +239,50 @@ Convert a Python dictionary back into XML.
239
239
240
240
Note: xmltodict aims to cover the common 90% of cases. It does not preserve every XML nuance (attribute order, mixed content ordering, multiple top-level comments). For exact fidelity, use a full XML library such as lxml.
241
241
242
+
## Examples
243
+
244
+
### Selective force_cdata
245
+
246
+
The `force_cdata` parameter can be used to selectively force CDATA wrapping for specific elements:
247
+
248
+
```python
249
+
>>> xml ='<a><b>data1</b><c>data2</c><d>data3</d></a>'
0 commit comments