Skip to content

Commit 8136d7d

Browse files
committed
Added a short example and summary to the readme
1 parent 5dd203b commit 8136d7d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,51 @@ in ambiguous cases, so you can easily handle it afterwards, e.g. in [validatacla
55

66
## Usage
77

8+
### Summary
9+
10+
`xml_string_to_dict()` takes a string with xml content and converts it to a python dictionary.
11+
12+
Optional arguments can be provided to control specific behaviour:
13+
- `ensure_array_keys` to make sure specific tags are always parsed as a list, even if it has only one element.
14+
- `remote_type_tags` and `conditional_remote_type_tags` to make sure specific tags will (always or in specific conditions) be interpreted as type names and therefore omitted in the output.
15+
- `ignore_attributes` to specify which attributes should be ignored for a cleaner output.
16+
17+
If you prefer to do the conversion from `str` to `etree.Element` yourself,
18+
you can use `xml_to_dict()` instead, which takes an `etree.Element` as input,
19+
and otherwise works the same as `xml_string_to_dict()`
20+
(which is a wrapper around `string_to_xml_etree()` and `xml_to_dict()`).
21+
22+
23+
### Short usage example
24+
25+
```python
26+
from xml2python import Xml2Python
27+
28+
example_xml_string: str = """
29+
<Envelope>
30+
<Body>
31+
<Content>
32+
<Text>some text</Text>
33+
</Content>
34+
</Body>
35+
<resultDescription xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
36+
</Envelope>
37+
"""
38+
39+
parsed_dict: dict = Xml2Python.xml_string_to_dict(
40+
example_xml_string,
41+
ensure_array_keys=[('Content', 'Text')],
42+
remote_type_tags=['Text'],
43+
ignore_attributes=['{http://www.w3.org/2001/XMLSchema-instance}nil'],
44+
)
45+
46+
# parsed_dict will look like this:
47+
assert parsed_dict == {'Envelope': {'Body': {'Content': ['some text']}, 'resultDescription': None}}
48+
```
49+
50+
51+
### Usage without parameters
52+
853
Without the optional arguments, this:
954

1055
```xml

0 commit comments

Comments
 (0)