xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
Project description
xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
About
XML can be converted to a data structure (such as JSON) and back. For example:
<employees>
<person>
<name value="Alice"/>
</person>
<person>
<name value="Bob"/>
</person>
</employees>
can be converted into this data structure (which also a valid JSON object):
{ "employees": [
{ "person": {
"name": {"@value": "Alice"}
} },
{ "person": {
"name": {"@value": "Alice"}
} }
] }
This uses the BadgerFish convention that prefixes attributes with @. Some popular conventions supported by this library are:
BadgerFish: Use "$" for text content, @ to prefix attributes,
GData: Use "$" for text content, ignore attributes
Parker: Ignore attributes and text content
Usage
To convert from a data structure to XML using the BadgerFish convention:
>>> from xmljson import badgerfish as bf
>>> bf.etree({'p': {'@id': 'main', '$': 'Hello', 'b': {'$': 'bold'}}})
This returns an array of etree.Element structures. In this case, the result is identical to:
>>> from xml.etree.ElementTree import fromstring
>>> [fromstring('<p id="main">Hello<b>bold</b></p>')]
The result can be inserted into any existing root etree.Element:
>>> from xml.etree.ElementTree import Element, tostring
>>> root = Element('root')
>>> result = bf.etree({'p': {'@id': 'main'}}, root=root)
>>> tostring(result)
<root><p id="main"/></root>
To convert from XML to a data structure using the BadgerFish convention:
>>> bf.data(fromstring('<p id="main">Hello<b>bold</b></p>'))
{"p": {"$": "Hello", "@id": "main", "b": {"$": "bold"}}}
To convert this to JSON, use:
>>> from json import dumps
>>> dumps(bf.data(fromstring('<p id="main">Hello<b>bold</b></p>')))
'{"p": {"b": {"$": "bold"}, "@id": "main", "$": "Hello"}}'
To preserve the order of attributes and children, specify the dict_type as OrderedDict (or any other dictionary-like type) in the constructor:
>>> from collections import OrderedDict >>> from xmljson import BadgerFish # import the class >>> bf = BadgerFish(dict_type=OrderedDict) # pick dict class
To use a different conversion method, replace BadgerFish with one of the other classes. Currently, these are supported:
>>> from xmljson import badgerfish # == xmljson.BadgerFish() >>> from xmljson import gdata # == xmljson.GData() >>> from xmljson import parker # == xmljson.Parker()
Installation
This is a pure-Python package built for Python 2.6+ and Python 3.0+. To set up:
pip install xmljson
Roadmap
Test cases for most HTML and XML scenarious across conventions
Test cases for Unicode
Support for namespaces and namespace prefixes
History
0.1.0 (2015-09-15)
Two-way conversions via BadgerFish, GData and Parker conventions.
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xmljson-0.1.0.zip.
File metadata
- Download URL: xmljson-0.1.0.zip
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35e4aa061154545f03b6c790959f617b7f579a360f9fec61876783c74708b45b
|
|
| MD5 |
ab16b65e01ef0c83072f1b252e688a52
|
|
| BLAKE2b-256 |
d81b53ae61f550102900ced1e27d0f7a91de6626a305efbd5b7fe79a3fb2b0af
|
File details
Details for the file xmljson-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: xmljson-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77ee8f0284c5c130a882865f115903681511788d4927c3aa0d71827a058f91d1
|
|
| MD5 |
9b60ec811f988217ca92e5b2eaf4fdae
|
|
| BLAKE2b-256 |
2b528c71fc22b4586613c8831ad079802ad731c86627cd374010f2f9c1e795d3
|