Skip to main content

xml2pyton

Takes XML input and converts it to a dict. The output can be optimized by removing unnecessary nesting or ensuring lists in ambiguous cases, so you can easily handle it afterwards, eg in validataclass.

Usage

Without the optional arguments, this:

<Envelope>
    <Header>
        <Security>
            <UsernameToken>
                <Username>Ghost</Username>
                <Password>Boo</Password>
            </UsernameToken>
        </Security>
    </Header>
    <Body>
        <Content>
            <ContentText>
                <Text>some text</Text>
            </ContentText>
        </Content>
    </Body>
</Envelope>

... will be converted to this:

{
    'Envelope': {
        'Body': {
            'Content': {
                'ContentText': {
                    'Text': 'some text',
                }
            }
        },
        'Header': {
            'Security': {
                'UsernameToken': {
                    'Password': 'Boo',
                    'Username': 'Ghost',
                }
            }
        }
    }
}

Parameter ensure_array_keys

The ensure_array_keys list can be provided to specify for which keys in which tags, the values should always be interpreted as items of a list. In case there is only one child item, they would otherwise not be recognizable as a list in the XML.

With ensure_array_keys=[('Envelope', 'Header'), ('Content', 'ContentText')], the example above would be converted to this, interpreting the values of the given keys as lists:

{
    'Envelope': {
        'Body': {
            'Content': {
                'ContentText': [
                    {'Text': 'some text'},
                ]
            }
        },
        'Header': [
            {
                'Security': {
                    'UsernameToken': {
                        'Password': 'Boo',
                        'Username': 'Ghost'
                    }
                }
            },
        ]
    }
}

Parameter remote_type_tags

The remote_type_tags list can be provided to specify which tags should be interpreted as names of types, e.g. enums, which don't need to be preserved as keys in the output dict.

For example, this:

<status>
    <ChargePointStatusType>Operative</ChargePointStatusType>
</status>

would usually be converted to this:

{
    'status': {
        'ChargePointStatusType': 'Operative',
    }
}

But if ChargePointStatusType is given in the remote_type_tags list, it will be converted to this simpler version:

{
    'status': 'Operative'
}

Which could then be parsed into an enum value if necessary.

Another use case for remote_type_tags would be to skip a level in the nested data that has only one field (!) of which the name does also not need to be preserved as a key.

With remote_type_tags=['Envelope', 'Content', 'ContentText', 'Text', 'Security', 'UsernameToken'], the first example above can become as compact as this:

{
    'Body': 'some text',
    'Header': {
        'Password': 'Boo',
        'Username': 'Ghost',
    }
}

The Body, Header, Username and Password keys cannot be removed because there are multiple keys on their levels.

Parameter: conditional_remote_type_tags

The conditional_remote_type_tags list can be used to skip a tag depending on the name of its parent tag. This is most useful for tags where the field name and the type name are the same, but only one of them should be skipped, for example:

<resultCode>
    <resultCode>ok</resultCode>
</resultCode>

can be parsed into a non-nested field named 'resultCode' with the content 'ok' by giving conditional_remote_type_tags=[('resultCode', 'resultCode')].

The 'ignore_attributes' list can be given to avoid parsing attributes with a certain name. This is useful if a tag could either have a simple string value as a child, or be self-closing - and the self-closing version should be interpreted as if the tag's value is null (because there might be attributes that say this, but in a weirdly complicated way).

For example, a tag like this is 'null' in terms of practical use:

<resultDescription xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

But it would normally be parsed as a dictionary here:

'resultDescription': {'{http://www.w3.org/2001/XMLSchema-instance}nil': 'true'}

And if we were to just skip the attribute by adding it to remote_type_tags, we would get this instead:

'resultDescription': 'true'

Which would be very misleading. So to parse this tag into a useful value for an 'Optional[str]' type field, the ignore_attributes list can be used. With ignore_attributes=['{http://www.w3.org/2001/XMLSchema-instance}nil'], we get a result that fits the type:

'resultDescription': None

Licence

This library is under MIT licence. Please look at LICENCE.txt for details.

Metadata

Release files for xml2python 0.1.1

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

Source distribution (sdist)

Source distribution for xml2python 0.1.1
File Size Uploaded
xml2python-0.1.1.tar.gz 8.2 kB Details

Built distribution (wheel)

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

Total release size: 14.2 kB

Release files / xml2python-0.1.1.tar.gz

Download URL xml2python-0.1.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3482b495322a0b26202c96c9983cfeaa9c549a4d6eda3ad849d16d6d8acc9da6
BLAKE2b-256 checksum
How to use checksums
fba273948beec87ae8117634b12016387b462f280cfd187b639e6e2adc979be3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / xml2python-0.1.1-py3-none-any.whl

Download URL xml2python-0.1.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c24c7263953916f36cae5968cab7b1834a92d4ecf4de4e32876b426145fd3f4a
BLAKE2b-256 checksum
How to use checksums
6682a4e67bfaa6a468f46d58d44afddc55602de5bbb8d9a5583b97f8753509da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.1

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