Skip to main content

xmljson

https://img.shields.io/travis/sanand0/xmljson.svg https://img.shields.io/pypi/v/xmljson.svg

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 @. Other conventions supported by this library are:

  • BadgerFish: Use "$" for text content, @ to prefix attributes,

  • GData: Use "$t" for text content, ignore attributes

  • Parker: Ignore attributes and text content

Convert data to XML

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>'

This includes lxml.html as well:

>>> from lxml.html import Element, tostring
>>> root = Element('html')
>>> result = bf.etree({'p': {'@id': 'main'}}, root=root)
>>> tostring(result, doctype='<!DOCTYPE html>')
'<!DOCTYPE html>\n<html><p id="main"></p></html>'

Convert XML to data

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

Conventions

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 Unicode

  • Support for namespaces and namespace prefixes

History

0.1.2 (2015-09-18)

  • Always use the dict_type class to create dictionaries (which defaults to OrderedDict to preserve order of keys)

  • Update documentation, test cases

  • Remove support for Python 2.6 (since we need collections.Counter)

  • Make the Travis CI build pass

0.1.1 (2015-09-18)

  • Convert true, false and numeric values from strings to Python types

  • xmljson.parker.data() is compliant with Parker convention (bugs resolved)

0.1.0 (2015-09-15)

  • Two-way conversions via BadgerFish, GData and Parker conventions.

  • First release on PyPI.

Metadata

Release files for xmljson 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmljson 0.1.2
File Size Uploaded
xmljson-0.1.2.zip 24.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xmljson 0.1.2
File Interpreter ABI Platform
xmljson-0.1.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 32.1 kB

Release files / xmljson-0.1.2.zip

Download URL xmljson-0.1.2.zip
Size 24.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ffbde2b134d43c365083ed1bf643b4c4664c0a87c9c481358a836bab0d1d9338
BLAKE2b-256 checksum
How to use checksums
b329eecebc07d89acb8f1606e3d85f65e57094ac9e02a60d321b7348d8a1d1b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / xmljson-0.1.2-py2.py3-none-any.whl

Download URL xmljson-0.1.2-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e7f9b3ba33ed454bcf115ac5a9ea87040e94eeda43224ef3b1745631467ed82b
BLAKE2b-256 checksum
How to use checksums
b8bcec01549982c3011778822863cfec1eb098d5ecf9c5eaebb1ff5e9c3245cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page