Professional Documents
Culture Documents
Release 0.1.9
S Anand
1 About 3
4 Conventions 9
5 Options 11
6 Installation 13
7 Roadmap 15
8 More information 17
8.1 Contributing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
8.2 Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
8.3 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8.4 Indices and tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
i
ii
xmljson Documentation, Release 0.1.9
xmljson converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
Contents 1
xmljson Documentation, Release 0.1.9
2 Contents
CHAPTER 1
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": "Bob"
}
}
}]
}
This uses the BadgerFish convention that prefixes attributes with @. The conventions supported by this library are:
• Abdera: Use "attributes" for attributes, "children" for nodes
• BadgerFish: Use "$" for text content, @ to prefix attributes
3
xmljson Documentation, Release 0.1.9
• Cobra: Use "attributes" for sorted attributes (even when empty), "children" for nodes, values are
strings
• GData: Use "$t" for text content, attributes added as-is
• Parker: Use tail nodes for text content, ignore attributes
• Yahoo Use "content" for text content, attributes added as-is
4 Chapter 1. About
CHAPTER 2
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>')]
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'})
By default, non-string values are converted to strings using Python’s str, except for booleans – which are converted
into true and false (lower case). Override this behaviour using xml_fromstring:
>>> tostring(bf.etree({'x': 1.23, 'y': True}, root=Element('root')))
'<root><y>true</y><x>1.23</x></root>'
>>> from xmljson import BadgerFish # import the class
5
xmljson Documentation, Release 0.1.9
To preserve the order of attributes and children, specify the dict_type as OrderedDict (or any other dictionary-
like type) in the constructor:
By default, values are parsed into boolean, int or float where possible (except in the Yahoo method). Override this
behaviour using xml_fromstring:
>>> dumps(bf.data(fromstring('<x>1</x>')))
'{"x": {"$": 1}}'
>>> bf_str = BadgerFish(xml_fromstring=False) # Keep XML values as strings
>>> dumps(bf_str.data(fromstring('<x>1</x>')))
'{"x": {"$": "1"}}'
>>> bf_str = BadgerFish(xml_fromstring=repr) # Custom string parser
'{"x": {"$": "\'1\'"}}'
xml_fromstring can be any custom function that takes a string and returns a value. In the example below, only
the integer 1 is converted to an integer. Everything else is retained as a float:
7
xmljson Documentation, Release 0.1.9
Conventions
To use a different conversion method, replace BadgerFish with one of the other classes. Currently, these are
supported:
9
xmljson Documentation, Release 0.1.9
10 Chapter 4. Conventions
CHAPTER 5
Options
11
xmljson Documentation, Release 0.1.9
12 Chapter 5. Options
CHAPTER 6
Installation
This is a pure-Python package built for Python 2.6+ and Python 3.0+. To set up:
13
xmljson Documentation, Release 0.1.9
14 Chapter 6. Installation
CHAPTER 7
Roadmap
15
xmljson Documentation, Release 0.1.9
16 Chapter 7. Roadmap
CHAPTER 8
More information
Contributing
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions
Report Bugs
Fix Bugs
Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.
Implement Features
Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement
it.
17
xmljson Documentation, Release 0.1.9
Write Documentation
xmljson could always use more documentation, whether as part of the official xmljson docs, in docstrings, or even on
the web in blog posts, articles, and such.
Submit Feedback
Get Started!
xmljson runs on Python 2.6+ and Python 3+ in any OS. To set up the development environment:
1. Fork the xmljson repo
2. Clone your fork locally:
3. Install your local copy into a virtualenv. If you have virtualenvwrapper installed, this is how you set up your
fork for local development:
$ mkvirtualenv xmljson
$ cd xmljson/
$ python setup.py develop
make release-test
Note: This uses the python.exe in your PATH. To change the Python used, run:
6. Commit your changes and push your branch to GitHub. Then send a pull request:
$ git add .
$ git commit -m "Your detailed description of your changes."
$ git push --set-upstream origin <branch-name>
Before you submit a pull request, check that it meets these guidelines:
1. The pull request should include tests.
2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function
with a docstring, and add the feature to the list in README.rst.
3. The pull request should work for Python 2.7 and 3.4.
Release
make release-test
git commit .
git tag -a vx.x.x
git push --follow-tags
make clean
python setup.py sdist bdist_wheel --universal
twine upload dist/*
Credits
Development Lead
• S Anand <root.node@gmail.com>
Contributors
8.2. Credits 19
xmljson Documentation, Release 0.1.9
History
• Bugfix and test cases for multiple nested children in Abdera convention
Thanks to @mukultaneja
• Fix GData.etree() conversion of attributes. (They were ignored. They should be added as-is.)
• 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
• 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
• Convert true, false and numeric values from strings to Python types
• xmljson.parker.data() is compliant with Parker convention (bugs resolved)
• genindex
• modindex
• search