Skip to main content

A simpler XML writer and xml-to-dict converter

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


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.3.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

XmlElement-0.3.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file XmlElement-0.3.1.tar.gz.

File metadata

  • Download URL: XmlElement-0.3.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for XmlElement-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5bd0224108440a8c3d10e47a72de0a85782455fa5dc07b009267dab16516674a
MD5 52224520c5f2b66cc3958a700f3eed0d
BLAKE2b-256 566aed1627140ad99e5eb724bb1c26ee4bc445c09d99538df7585ec23bf33fde

See more details on using hashes here.

File details

Details for the file XmlElement-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: XmlElement-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for XmlElement-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 74d5f20eac46a00b81b9b10033e35d5a51b9f8e7cde80f55bf00be8d6b5fa068
MD5 f752f21133db1c1eb9c225a7143990dd
BLAKE2b-256 5139b8d2546a8bd8373c5c932be78174a587c6647cc294b3f72b73a07352a794

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