Comprehensive XML generator for Python
Project description
XML Generator
Comprehensive XML generator for Python
Table of Contents
Installation
pip install xml-generator-seobaeksol
Usage
Import
from xml_generator.types import XmlNode
Comprehensive parsing
Parse Comprehensive format into an XmlNode object.
node = XmlNode.parse(
{
"name": "node",
"attributes": {"attr1": "value1", "attr2": "value2"},
"body": [
{"name": "child1", "attributes": {"attr1": "value1"}},
{"name": "child2", "attributes": {"attr2": "value2"}},
],
}
)
Create a XmlNode with a query
node = XmlNode.from_query('node@attr1="value1"@attr2="value2"')
Create a XmlNode with a extended query
extend_query = {
"root": [
"NoValueNode",
{
"SHORT-NAME": "node",
},
{
"ELEMENTS@type=string": [
"element@hint=id",
"element@unit=m",
{"element@unit=m@min=0@max=100@init=50": "100"},
],
},
]
}
Searching a specific node
Return the first XmlNode with the given query. Query can be a name with attributes.
parent = node.find("node")
child1 = node.find("child1@attr1")
child2 = node.find("child2")
Serialization
Using the to_xml()
function that return the XmlNode as an XML string.
with open('sample.xml', 'w', encoding='utf-8') as f:
f.write(node.to_xml())
Using the to_extend_query()
function that return the extended query object as an XML string.
extended_query_obj = root.to_extended_query()
Deserialization
Use XmlParser
class for reading a xml file that return XmlNode
by close()
method.
It is sub-class of xml.etree.ElementTree.XMLParser
built-in python class.
from xml_generator.types import XmlParser
parser = XmlParser()
with open(
file="xml_generator/tests/samples/complex.xml", mode="r", encoding="utf-8"
) as f:
original_xml = f.read()
parser.feed(original_xml)
root = parser.close()
xml_string = root.to_xml(
declaration=True, declaration_tag='<?xml version="1.0" encoding="UTF-8"?>\n'
)
Contributing
Coming soon.
Testing
python -m unittest discover -s xml_generator/tests -p "test*.py"
Building
python .\setup.py sdist bdist_wheel
Deployment
twine upload dist/*
License
License :: OSI Approved :: MIT License
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
Built Distribution
Hashes for xml-generator-seobaeksol-0.4.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10fd3532949ac1aae1b66ab114a60ce529f084deaac7b830a094681de63d9ca8 |
|
MD5 | 31213992abd6c63421947b8d7cd32c26 |
|
BLAKE2b-256 | f2a89b35e008bfbc4c6863cf4faf4db1a9034dd6424d8fd085dd2e37b2c075f2 |
Hashes for xml_generator_seobaeksol-0.4.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b8557843e2baf6e5ab6fdd3a6ce4bb450f4581c512a2713be5e2cc01c851dac |
|
MD5 | 105b8969e19c3a433e49975be156e0e5 |
|
BLAKE2b-256 | da04f214e09ad84945cfcd8d1544f538aebd8163f3efcadbe3e2ae845e3ce095 |