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
Build a XML by nesting XmlElements
from XmlElement import XmlElement as X
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:
from XmlElement import XmlElement as X
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
Converting XmlElement to POJO-like object
from XmlElement import XmlElement as X
xml = X('RootElement', s=[ # root element without attributes
X('Child1', {'testattr': 'true'}, [ # sub element with a boolean attribute
X('Child2', t='1234'), # sub-sub element with int-like value
X('Child3', t='-1234.56') # sub-sub element with float-like value
])
])
print(xml) # <RootElement><Child1 testattr="true"><Child2>1234</Child2><Child3>-1234.56</Child3></Child1></RootElement>
print(xml.to_dict()) # {'Child1': {'@testattr': True, 'Child2': 1234, 'Child3': -1234.56}}
print(xml.to_dict( # {'Child1': {'@testattr': 'true', 'Child2': '1234', 'Child3': '-1234.56'}}
recognize_numbers=False,
recognize_bool=False)
)
Subnodes of equal names under a node will result in a list. However, this cannot recognize if a single element should be list in another context.
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.2.tar.gz
(4.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file XmlElement-0.2.2.tar.gz.
File metadata
- Download URL: XmlElement-0.2.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a97be01ef6b40d1d4e18fcfc1871b05873ccbda7bde77c95ec6a78fb8339f948
|
|
| MD5 |
1489209f875df9f7d51a64499b50230b
|
|
| BLAKE2b-256 |
2a0bd9280bc24c3828c4c299f1076e0ca060ab9c67a81634dd521ab55a8c056a
|
File details
Details for the file XmlElement-0.2.2-py3-none-any.whl.
File metadata
- Download URL: XmlElement-0.2.2-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
913b6af5fec7fd7b46bb69685fdb51a41e1e2956b6dbd6d1b9282757461cbe7f
|
|
| MD5 |
071c5df564626c5b0da6459f89ffbc5d
|
|
| BLAKE2b-256 |
e7a46467d6f707b871aecad5ca516e6af36fe5d414ff37a71aa846a2feb924a4
|