Skip to main content

xml2python is a simple helper class to transform XML input to more usable python dicts.

Project description

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.

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

xml2python-0.1.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

xml2python-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file xml2python-0.1.1.tar.gz.

File metadata

  • Download URL: xml2python-0.1.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for xml2python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3482b495322a0b26202c96c9983cfeaa9c549a4d6eda3ad849d16d6d8acc9da6
MD5 2bb0adb1cd9ff42b870e3280c2f28ea3
BLAKE2b-256 fba273948beec87ae8117634b12016387b462f280cfd187b639e6e2adc979be3

See more details on using hashes here.

File details

Details for the file xml2python-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xml2python-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for xml2python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c24c7263953916f36cae5968cab7b1834a92d4ecf4de4e32876b426145fd3f4a
MD5 f28dcb502ae52942db09b98d5b7c91eb
BLAKE2b-256 6682a4e67bfaa6a468f46d58d44afddc55602de5bbb8d9a5583b97f8753509da

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