Skip to main content

xmlparser

A python package for parsing xml files into data structures.

Installation

Using pip:

pip install py-xmlparser

Usage

Create concrete classes with variable names matching the attributes of the element. They will be filled automatically when parsing. Those classes must inherit from xp.XmlCreatedObject.

Override process_children(self, children) to handle created instances from the inner xml.

Provide a factory that inherits from xp.XmlCreatedObjectFactory to create the instances with.

Then simply call xp.parse(path_to_xml, factory) or xp.parse_string(xml_text, factory).

XML

<First an_attribute="my-attribute">
    <Second also_an_attribute="my-second-attribute">
        <Third num="1"/>
        <Third num="2"/>
        <Third num="3"/>
        <Third num="4"/>
    </Second>
    <Second also_an_attribute="my-second-attribute">
        <Third num="5"/>
        <Third num="6"/>
    </Second>
    <Forth num1="1" num2="2"/>
</First>

Python

import xmlparser as xp
import pprint as pp


class ThirdClass(xp.XmlCreatedObject):
    def __init__(self):
        self.num = None

    def process_children(self, children):
        pass


class SecondClass(xp.XmlCreatedObject):
    def __init__(self):
        self.thirds = []
        self.also_an_attribute = None

    def process_children(self, children):
        for child in children:
            if isinstance(child, ThirdClass):
                self.thirds.append(child)


class ForthClass(xp.XmlCreatedObject):
    def __init__(self):
        self.num1 = None
        self.num2 = None

    def process_children(self, children):
        pass


class FirstClass(xp.XmlCreatedObject):
    def __init__(self):
        self.an_attribute = None
        self.seconds = []
        self.forths = []

    def process_children(self, children):
        for child in children:
            if isinstance(child, SecondClass):
                self.seconds.append(child)
            elif isinstance(child, ForthClass):
                self.forths.append(child)


class Factory(xp.XmlCreatedObjectFactory):
    def __init__(self):
        self.mapping = {
            'First': lambda: FirstClass(),
            'Second': lambda: SecondClass(),
            'Third': lambda: ThirdClass(),
            'Forth': lambda: ForthClass()
        }

    def keys(self):
        return self.mapping.keys()

    def create(self, xml_tag):
        return self.mapping[xml_tag]()


if __name__ == '__main__':
    instance = xp.parse('Test.xml', Factory())
    pp.pprint(vars(instance))

Download files

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

Source Distribution

py-xmlparser-1.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

py_xmlparser-1.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file py-xmlparser-1.0.1.tar.gz.

File metadata

  • Download URL: py-xmlparser-1.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for py-xmlparser-1.0.1.tar.gz
Algorithm Hash digest
SHA256 da726309977753b34a7d68283b9743f61c84af40f4e0a6e0956ee9eda0e95b59
MD5 61238f5cf3fa67694418a35fa03a15f7
BLAKE2b-256 5adf607e9d89e45bdec6db698148e5b05fb2868e57b9dea1dbfd3259318d6505

See more details on using hashes here.

File details

Details for the file py_xmlparser-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: py_xmlparser-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for py_xmlparser-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2451b4f3df69e55eb90c72a0cdc9272696b8387636cbcdda590fabac97e9cc13
MD5 791141a8f4173cae7dcd57de042c115a
BLAKE2b-256 bc52a8a86fb9cf1a7d794f7d4f1f0ee863512cca8b2d867df53df4ca95205828

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 files

1.0.0

2 files

Supported by

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