0
# xmljson
1
2
A Python library that converts XML into JSON/Python dictionaries and vice-versa, supporting 6 different XML-to-JSON conventions including BadgerFish, Abdera, Cobra, GData, Parker, and Yahoo. It handles bidirectional conversion between XML structures and Python data structures with customizable parsing behaviors.
3
4
## Package Information
5
6
- **Package Name**: xmljson
7
- **Language**: Python
8
- **Installation**: `pip install xmljson`
9
10
## Core Imports
11
12
```python
13
import xmljson
14
```
15
16
Import specific classes:
17
18
```python
19
from xmljson import BadgerFish, Parker, GData, Yahoo, Abdera, Cobra
20
```
21
22
Import pre-configured instances:
23
24
```python
25
from xmljson import badgerfish, parker, gdata, yahoo, abdera, cobra
26
```
27
28
## Basic Usage
29
30
```python
31
from xmljson import badgerfish as bf
32
from xml.etree.ElementTree import fromstring, tostring
33
import json
34
35
# XML to data conversion
36
xml_string = '<p id="main">Hello<b>bold</b></p>'
37
xml_element = fromstring(xml_string)
38
data = bf.data(xml_element)
39
print(json.dumps(data))
40
# Output: {"p": {"@id": "main", "$": "Hello", "b": {"$": "bold"}}}
41
42
# Data to XML conversion
43
data = {'p': {'@id': 'main', '$': 'Hello', 'b': 'bold'}}
44
xml_elements = bf.etree(data)
45
print(tostring(xml_elements[0]).decode())
46
# Output: <p id="main">Hello<b>bold</b></p>
47
```
48
49
## Architecture
50
51
xmljson uses a base `XMLData` class that defines the core conversion framework, with specialized subclasses implementing different XML-to-JSON conventions:
52
53
- **XMLData**: Base class providing `etree()` and `data()` methods with configurable options
54
- **Convention Classes**: Specialized implementations (BadgerFish, Parker, etc.) with preset configurations
55
- **Pre-configured Instances**: Module-level instances for convenience
56
57
The design allows for:
58
- Customizable string parsing and type conversion
59
- Flexible dictionary and list constructors (OrderedDict, custom types)
60
- Configurable handling of XML attributes, text content, and invalid tags
61
- Support for both xml.etree and lxml backends
62
63
## Capabilities
64
65
### BadgerFish Convention
66
67
XML conversion using BadgerFish convention with @ prefix for attributes and $ for text content. This convention is widely supported and provides a clean JSON representation of XML structures.
68
69
```python { .api }
70
class BadgerFish(XMLData):
71
def __init__(self, **kwargs): ...
72
73
def etree(self, data, root=None): ...
74
def data(self, root): ...
75
```
76
77
[BadgerFish Convention](./badgerfish.md)
78
79
### Parker Convention
80
81
XML conversion using Parker convention with tail nodes for text content and ignoring attributes. Produces the most compact JSON representation by omitting attribute information.
82
83
```python { .api }
84
class Parker(XMLData):
85
def __init__(self, **kwargs): ...
86
87
def etree(self, data, root=None): ...
88
def data(self, root, preserve_root=False): ...
89
```
90
91
[Parker Convention](./parker.md)
92
93
### GData Convention
94
95
XML conversion using GData convention with $t for text content and attributes added as-is. This convention is used by Google's GData APIs.
96
97
```python { .api }
98
class GData(XMLData):
99
def __init__(self, **kwargs): ...
100
101
def etree(self, data, root=None): ...
102
def data(self, root): ...
103
```
104
105
[GData Convention](./gdata.md)
106
107
### Yahoo Convention
108
109
XML conversion using Yahoo convention with 'content' for text and no automatic string parsing. Values remain as strings unless explicitly converted.
110
111
```python { .api }
112
class Yahoo(XMLData):
113
def __init__(self, **kwargs): ...
114
115
def etree(self, data, root=None): ...
116
def data(self, root): ...
117
```
118
119
[Yahoo Convention](./yahoo.md)
120
121
### Abdera Convention
122
123
XML conversion using Abdera convention with 'attributes' and 'children' keys for structured XML representation.
124
125
```python { .api }
126
class Abdera(XMLData):
127
def __init__(self, **kwargs): ...
128
129
def etree(self, data, root=None): ...
130
def data(self, root): ...
131
```
132
133
[Abdera Convention](./abdera.md)
134
135
### Cobra Convention
136
137
XML conversion using Cobra convention with sorted attributes and string-only values. This convention ensures consistent attribute ordering and string type preservation.
138
139
```python { .api }
140
class Cobra(XMLData):
141
def __init__(self, **kwargs): ...
142
143
def etree(self, data, root=None): ...
144
def data(self, root): ...
145
```
146
147
[Cobra Convention](./cobra.md)
148
149
### Command Line Interface
150
151
XML to JSON conversion via command line tool supporting all conventions with customizable input/output files.
152
153
```python { .api }
154
def main(*test_args): ...
155
def parse_args(args=None, in_file=sys.stdin, out_file=sys.stdout): ...
156
```
157
158
[Command Line Interface](./cli.md)
159
160
## Pre-configured Instances
161
162
```python { .api }
163
abdera: Abdera
164
badgerfish: BadgerFish
165
cobra: Cobra
166
gdata: GData
167
parker: Parker
168
yahoo: Yahoo
169
```
170
171
These module-level instances provide immediate access to conversion functionality without needing to instantiate classes.
172
173
## Module Constants
174
175
```python { .api }
176
__author__: str
177
__email__: str
178
__version__: str
179
```
180
181
Module metadata constants providing package information:
182
- `__author__`: Package author name ('S Anand')
183
- `__email__`: Author email address ('root.node@gmail.com')
184
- `__version__`: Current package version ('0.2.0')
185
186
## Types
187
188
```python { .api }
189
class XMLData:
190
def __init__(
191
self,
192
xml_fromstring=True,
193
xml_tostring=True,
194
element=None,
195
dict_type=None,
196
list_type=None,
197
attr_prefix=None,
198
text_content=None,
199
simple_text=False,
200
invalid_tags=None
201
): ...
202
203
def etree(self, data, root=None): ...
204
def data(self, root): ...
205
```