Skip to main content

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

Command line

After installing XmlElement, two command line tools will be provided:

xml_to_json example.xml         # print json from xml file to stdout
json_to_xml example.json        # print xml from json file to stdout
cat example.xml | xml_to_json   # print json from xml stdin to stdout
cat example.json | json_to_xml  # print xml from json stdin to stdout

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.

Metadata

Release files for XmlElement 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for XmlElement 0.3.2
File Size Uploaded
XmlElement-0.3.2.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for XmlElement 0.3.2
File Interpreter ABI Platform
XmlElement-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / XmlElement-0.3.2.tar.gz

Download URL XmlElement-0.3.2.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cb5cab4d27104ce5367c9ae607d6fc36817aa2374612fcb95c678be7b01a6156
BLAKE2b-256 checksum
How to use checksums
f2e86084dc7432fa0c528a4a7b8302ecbdfeabe124e84cab15aa43fef9264eca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release files / XmlElement-0.3.2-py3-none-any.whl

Download URL XmlElement-0.3.2-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af40d0eb5355eead5572473928aa1d2c210144df259c84fa0e8ad801edcb644f
BLAKE2b-256 checksum
How to use checksums
033033847bfef589ddf7ba6c6883a9ec565356d7adc0427d987c87fdc406f992
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page