https://github.com/nicwolff/xml_from_seq
Generate XML from a Python data structure
https://github.com/nicwolff/xml_from_seq
python xml
Last synced: 6 months ago
JSON representation
Generate XML from a Python data structure
- Host: GitHub
- URL: https://github.com/nicwolff/xml_from_seq
- Owner: nicwolff
- Created: 2020-02-23T19:38:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-07T14:49:32.000Z (about 2 years ago)
- Last Synced: 2024-05-12T09:42:47.069Z (about 2 years ago)
- Topics: python, xml
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# xml_from_seq
Generate XML from a Python data structure.
## Examples
The `XML()` function renders a Python list or tuple to an XML element. The first item is the
element name, the second item – if it's a dict – is the attributes, and the rest of the items
are either text or nested elements.
```py
from xml_from_seq import XML
item = ['item', {'attr1': 123, 'attr2': 'other value'}, 'This is the content of the item.']
assert XML(item) == 'This is the content of the item.'
item = [
'item',
'This is some content of the item.'
['sub', 'This is the content of a subelement.']
]
print(XML(item))
```
```xml
This is some content of the item.
This is the content of a subelement.
```
If an element is False or None it will be omitted. If a element's name is `None` its contents will
appear in its place. If an attribute's value is `None` it will be omitted.
If an element's name is a list or tuple, it will be inserted into the XML as-is – so you can
include already-rendered XML by double-bracketing it:
```py
print(XML([['123']]))
```
```xml
123
```
If an element's tag name is `xml_from_seq.CDATA`, that element's content will be rendered unescaped
in a `` section.
### Indentation and line breaks
If the first item in an element (not counting an attribute dict) is `xml_from_seq.INLINE`, that
element's contents won't be indented on separate lines from the element's start and end tags.
```py
from xml_from_seq import INLINE, XML
item = [
'item',
'This is some content of the item.'
['sub', INLINE, 'This is the content of a subelement.']
]
print(XML(item))
```
```xml
This is some content of the item.
This is the content of a subelement.
```
You can pass an integer `indent` parameter to the `XML()` function to indent the output XML by
that many tabs.