Skip to main content

Comprehensive XML generator for Python

Project description

XML Generator

Comprehensive XML generator for Python

Table of Contents

Installation

pip install xml-generator

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"}},
        ],
    }
)

Parse Comprehensive format with quries

node = XmlNode.from_query('node@attr1="value1"@attr2="value2"')
nodes = XmlNode.from_queries(
    [
        "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")

Generate a xml file

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())

Read a xml file

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xml-generator-seobaeksol-0.3.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xml_generator_seobaeksol-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file xml-generator-seobaeksol-0.3.0.tar.gz.

File metadata

File hashes

Hashes for xml-generator-seobaeksol-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a78d4e52daf3e67d6becb130a194118aec7160d9fea7d96b005f7bac62583c46
MD5 3a935669de4181730ba768609fc0e932
BLAKE2b-256 0d3cdbc4caae11fd8cf97451dc6c82a5855c89ccbf9783994643998b0606f95d

See more details on using hashes here.

File details

Details for the file xml_generator_seobaeksol-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for xml_generator_seobaeksol-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0194d15d2861459edcc549fadc605d43b7ccd77673086e20fea410f627c85caf
MD5 2d9f9778675a060a109be87238106278
BLAKE2b-256 60dfcb791f791b7cec034a4d8ea55f34d87a5c8d6b0f0ff8d8af2b5f8e280d2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page