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))
Release files for py-xmlparser 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py-xmlparser-1.0.1.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_xmlparser-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.3 kB
Release files / py-xmlparser-1.0.1.tar.gz
| Download URL | py-xmlparser-1.0.1.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da726309977753b34a7d68283b9743f61c84af40f4e0a6e0956ee9eda0e95b59
|
|
BLAKE2b-256 checksum How to use checksums |
5adf607e9d89e45bdec6db698148e5b05fb2868e57b9dea1dbfd3259318d6505
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / py_xmlparser-1.0.1-py3-none-any.whl
| Download URL | py_xmlparser-1.0.1-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2451b4f3df69e55eb90c72a0cdc9272696b8387636cbcdda590fabac97e9cc13
|
|
BLAKE2b-256 checksum How to use checksums |
bc52a8a86fb9cf1a7d794f7d4f1f0ee863512cca8b2d867df53df4ca95205828
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|