A simpler XML writer
Project description
XmlElement
A simpler XML writer.
Installation
pip install XmlElement
Test
>>> from XmlElement import XmlElement as X
>>> xml = X.from_string('<test><x/></test>')
>>> xml
XmlElement(test)
Usage
Creating XmlElements
from XmlElement import XmlElement as X
# Build a XML by nesting XmlElements:
xml = X('RootElement', s=[ # root element without attributes
X('Child1', {'testattr': 'Example attribute'}, [ # sub element with an attribute
X('Child2', t='Example text value') # sub-sub element with text value
])
])
# Alternative: Build a XML from a POJO-like object:
xml2 = X.from_object(
node_name='RootElement', # root element without attributes
data={ # no @-elements: root has -> no attributes
'Child1': { # sub element with an attribute @testattr
'@testattr': 'Example attribute',
'Child2': 'Example text value' # sub-sub element with text value
}
}
)
print(str(xml) == str(xml2)) # True
Accessing values by dot operator
print(xml)
print(xml.Child1[0].attributes['testattr']) # Example attribute
print(xml.Child1[0].Child2[0].text) # Example text value
Accessing values dict-like (to avoid static type checker warnings)
print(xml)
print(xml['Child1'][0].attributes['testattr']) # Example attribute
print(xml['Child1'][0]['Child2'][0].text) # Example text value
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
XmlElement-0.2.0.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for XmlElement-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a24220479a66689c11f94960ace0bd88f63ed8b95d7c2ac7d9dc75baa8542694 |
|
MD5 | 48d241a4ca188b7984819ecc07600bb1 |
|
BLAKE2b-256 | 92ead00553bcf5163f834e2ae3af14596e76e31c9b273b420fcc470facde68f8 |