xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
Project description
===============================
xmljson
===============================
.. image:: https://img.shields.io/travis/sanand0/xmljson.svg
:target: https://travis-ci.org/sanand0/xmljson
.. image:: https://img.shields.io/pypi/v/xmljson.svg
:target: https://pypi.python.org/pypi/xmljson
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 ``@``.
The conventions supported by this library are:
* `BadgerFish`_: Use ``"$"`` for text content, ``@`` to prefix attributes,
* `GData`_: Use ``"$t"`` for text content, ignore attributes
* `Parker`_: Use tail nodes for text content, ignore attributes
.. _BadgerFish: http://www.sklar.com/badgerfish/
.. _GData: http://wiki.open311.org/JSON_and_XML_Conversion/#the-gdata-convention
.. _Parker: https://developer.mozilla.org/en-US/docs/JXON#The_Parker_Convention
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>')]
.. _etree.Element: http://effbot.org/zone/element-index.htm
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 <http://lxml.de/lxmlhtml.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>'
For ease of use, strings are treated as node text. For example, both the
following are the same::
>>> bf.etree({'p': {'$': 'paragraph text'}})
>>> bf.etree({'p': 'paragraph text'})
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.3 (2015-09-20)
~~~~~~~~~~~~~~~~~
- Simplify ``{'p': {'$': 'text'}}`` to ``{'p': 'text'}`` in BadgerFish and GData
conventions.
- Add test cases for ``.etree()`` -- mainly from the `MDN JXON article`_.
- ``dict_type``/``list_type`` do not need to inherit from ``dict``/``list``
.. _MDN JXON article: https://developer.mozilla.org/en-US/docs/JXON#In_summary
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
.. _Travis CI build: https://travis-ci.org/sanand0/xmljson
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.
xmljson
===============================
.. image:: https://img.shields.io/travis/sanand0/xmljson.svg
:target: https://travis-ci.org/sanand0/xmljson
.. image:: https://img.shields.io/pypi/v/xmljson.svg
:target: https://pypi.python.org/pypi/xmljson
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 ``@``.
The conventions supported by this library are:
* `BadgerFish`_: Use ``"$"`` for text content, ``@`` to prefix attributes,
* `GData`_: Use ``"$t"`` for text content, ignore attributes
* `Parker`_: Use tail nodes for text content, ignore attributes
.. _BadgerFish: http://www.sklar.com/badgerfish/
.. _GData: http://wiki.open311.org/JSON_and_XML_Conversion/#the-gdata-convention
.. _Parker: https://developer.mozilla.org/en-US/docs/JXON#The_Parker_Convention
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>')]
.. _etree.Element: http://effbot.org/zone/element-index.htm
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 <http://lxml.de/lxmlhtml.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>'
For ease of use, strings are treated as node text. For example, both the
following are the same::
>>> bf.etree({'p': {'$': 'paragraph text'}})
>>> bf.etree({'p': 'paragraph text'})
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.3 (2015-09-20)
~~~~~~~~~~~~~~~~~
- Simplify ``{'p': {'$': 'text'}}`` to ``{'p': 'text'}`` in BadgerFish and GData
conventions.
- Add test cases for ``.etree()`` -- mainly from the `MDN JXON article`_.
- ``dict_type``/``list_type`` do not need to inherit from ``dict``/``list``
.. _MDN JXON article: https://developer.mozilla.org/en-US/docs/JXON#In_summary
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
.. _Travis CI build: https://travis-ci.org/sanand0/xmljson
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.
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
xmljson-0.1.3.zip
(25.7 kB
view hashes)
Built Distribution
Close
Hashes for xmljson-0.1.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f77dc47f8195bafb60eb88b434111d29c3c5db69b73f971a99f7d39920ee7625 |
|
MD5 | 85a375b143e494a9201f3d9670ad88ff |
|
BLAKE2b-256 | 6c3610e98c8c634f54515db9daa9f403652c2c0237873a885e84e88964c5b713 |