xmlpydict 0.0.10__cp310-cp310-manylinux_2_24_x86_64.manylinux_2_34_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyxmlhandler.cpython-310-x86_64-linux-gnu.so +0 -0
- xmlpydict/__init__.py +23 -0
- xmlpydict-0.0.10.dist-info/METADATA +112 -0
- xmlpydict-0.0.10.dist-info/RECORD +7 -0
- xmlpydict-0.0.10.dist-info/WHEEL +6 -0
- xmlpydict-0.0.10.dist-info/licenses/LICENSE +21 -0
- xmlpydict-0.0.10.dist-info/top_level.txt +2 -0
|
Binary file
|
xmlpydict/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from pyxmlhandler import _PyDictHandler
|
|
2
|
+
from xml.parsers import expat
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def parse(xml_content, **kwargs) -> dict:
|
|
6
|
+
handler = _PyDictHandler(**kwargs)
|
|
7
|
+
parser = expat.ParserCreate()
|
|
8
|
+
parser.CharacterDataHandler = handler.characters
|
|
9
|
+
parser.StartElementHandler = handler.startElement
|
|
10
|
+
parser.EndElementHandler = handler.endElement
|
|
11
|
+
parser.Parse(xml_content, True)
|
|
12
|
+
return handler.item
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def parse_file(file_path, **kwargs) -> dict:
|
|
16
|
+
handler = _PyDictHandler(**kwargs)
|
|
17
|
+
parser = expat.ParserCreate()
|
|
18
|
+
parser.CharacterDataHandler = handler.characters
|
|
19
|
+
parser.StartElementHandler = handler.startElement
|
|
20
|
+
parser.EndElementHandler = handler.endElement
|
|
21
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
22
|
+
parser.ParseFile(f)
|
|
23
|
+
return handler.item
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xmlpydict
|
|
3
|
+
Version: 0.0.10
|
|
4
|
+
Summary: xml to dictionary tool for python
|
|
5
|
+
Author-email: Matthew Taylor <matthew.taylor.andre@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/MatthewAndreTaylor/xml-to-pydict
|
|
7
|
+
Keywords: xml,dictionary
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Text Processing :: Markup :: XML
|
|
21
|
+
Requires-Python: >=3.7
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: tests
|
|
25
|
+
Requires-Dist: pytest; extra == "tests"
|
|
26
|
+
Requires-Dist: requests; extra == "tests"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# xmlpydict 📑
|
|
30
|
+
|
|
31
|
+
[](https://github.com/MatthewAndreTaylor/xml-to-pydict/actions/workflows/tests.yml)
|
|
32
|
+
[](https://github.com/MatthewAndreTaylor/xml-to-pydict)
|
|
33
|
+
[](https://pypi.org/project/xmlpydict/)
|
|
34
|
+
|
|
35
|
+
## Requirements
|
|
36
|
+
|
|
37
|
+
- `python 3.8+`
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
To install xmlpydict, using pip:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install xmlpydict
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quickstart
|
|
48
|
+
|
|
49
|
+
```py
|
|
50
|
+
>>> from xmlpydict import parse
|
|
51
|
+
>>> parse("<package><xmlpydict language='python'/></package>")
|
|
52
|
+
{'package': {'xmlpydict': {'@language': 'python'}}}
|
|
53
|
+
>>> parse("<person name='Matthew'>Hello!</person>")
|
|
54
|
+
{'person': {'@name': 'Matthew', '#text': 'Hello!'}}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Goals
|
|
58
|
+
|
|
59
|
+
Create a consistent parsing strategy between XML and Python dictionaries using the specification found [here](https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html). `xmlpydict` focuses on speed; see the benchmarks below.
|
|
60
|
+
|
|
61
|
+
<img width="256" alt="small_xml_document" src="https://github.com/user-attachments/assets/0248a408-6bb6-4790-bd0f-f90537e2f21a" />
|
|
62
|
+
<img width="256" alt="large_xml_document" src="https://github.com/user-attachments/assets/539a2a69-f475-46a5-bffc-1e8805a5a5e7" />
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
### xmlpydict supports the following
|
|
66
|
+
|
|
67
|
+
[CDataSection](https://www.w3.org/TR/xml/#sec-cdata-sect): CDATA Sections are stored as {'#text': CData}.
|
|
68
|
+
|
|
69
|
+
[Comments](https://www.w3.org/TR/xml/#sec-comments): Comments are tokenized for corectness, but have no effect in what is returned.
|
|
70
|
+
|
|
71
|
+
[Element Tags](https://www.w3.org/TR/xml/#sec-starttags): Allows for duplicate attributes, however only the latest defined will be taken.
|
|
72
|
+
|
|
73
|
+
[Characters](https://www.w3.org/TR/xml/#charsets): Similar to CDATA text is stored as {'#text': Char} , however this text is stripped.
|
|
74
|
+
|
|
75
|
+
```py
|
|
76
|
+
# Empty tags are containers
|
|
77
|
+
>>> from xmlpydict import parse
|
|
78
|
+
>>> parse("<a></a>")
|
|
79
|
+
{'a': None}
|
|
80
|
+
>>> parse("<a/>")
|
|
81
|
+
{'a': None}
|
|
82
|
+
>>> parse("<a/>").get('href')
|
|
83
|
+
None
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Attribute prefixing
|
|
87
|
+
|
|
88
|
+
```py
|
|
89
|
+
# Change prefix from default "@" with keyword argument attr_prefix
|
|
90
|
+
>>> from xmlpydict import parse
|
|
91
|
+
>>> parse('<p width="10" height="5"></p>', attr_prefix="$")
|
|
92
|
+
{"p": {"$width": "10", "$height": "5"}}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
### Exceptions
|
|
97
|
+
|
|
98
|
+
```py
|
|
99
|
+
# Grammar and structure of the xml_content is checked while parsing
|
|
100
|
+
>>> from xmlpydict import parse
|
|
101
|
+
>>> parse("<a></ a>")
|
|
102
|
+
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 5
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
### Unsupported
|
|
107
|
+
|
|
108
|
+
Prolog / Enforcing Document Type Definition and Element Type Declarations
|
|
109
|
+
|
|
110
|
+
Entity Referencing
|
|
111
|
+
|
|
112
|
+
Namespaces
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
pyxmlhandler.cpython-310-x86_64-linux-gnu.so,sha256=JuhYybTuMYp9WDK4l2NGJF_LAwXkWWSf3bPM8EkkKLw,84336
|
|
2
|
+
xmlpydict/__init__.py,sha256=imQYx-ABH-sMyQ9aTj5kIy9Ue-3DwX2H4a7BZ90uZic,787
|
|
3
|
+
xmlpydict-0.0.10.dist-info/METADATA,sha256=tfd9JN29qZi7whvb-roeJOzqzX4G5H2T_ASVvrQdouI,3815
|
|
4
|
+
xmlpydict-0.0.10.dist-info/WHEEL,sha256=uuWJa98bPU_AXb4UVlgAI4CIVTk5kJLmtKpdE0CpVLg,152
|
|
5
|
+
xmlpydict-0.0.10.dist-info/top_level.txt,sha256=YEIhCVuLdrZ3UaJku2ROkLGCjBw4g4oMXWgDnMtbw5o,23
|
|
6
|
+
xmlpydict-0.0.10.dist-info/RECORD,,
|
|
7
|
+
xmlpydict-0.0.10.dist-info/licenses/LICENSE,sha256=x08oLniev4jR-mE0Qcw9TWflIKdMnam8nmF15BUoM-4,1076
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Matthew Andre Taylor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|