• This repository has been archived on 02/May/2019
  • Stars
    star
    464
  • Rank 94,450 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 14 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python script converts XML to JSON or the other way around

XML2JSON

This module is deprecated and will not be updated anymore (May 2019)

  • To convert between text-based data formats (including XML and JSON) use my library dataknead.
  • To work with XML in Python i recommend the excellent xmltodict.

Deprecated documentation

Python script converts XML to JSON or the other way around

Usage

Make this executable

$ chmod +x xml2json

Then invoke it from the command line like this

$ xml2json -t xml2json -o file.json file.xml

Or the other way around

$ xml2json -t json2xml -o file.xml file.json

Without the -o parameter xml2json writes to stdout

$ xml2json -t json2xml file.json

Additional the options: Strip text (#text and #tail) in the json

$ xml2json -t xml2json -o file.json file.xml --strip_text

Strip namespace in the json

$ xml2json -t xml2json -o file.json file.xml --strip_namespace

In code

from xml2json import json2xml
d = {'r': {'@p': 'p1', '#text': 't1', 'c': 't2'}}
print(json2xml(d))
> b'<r p="p1">t1<c>t2</c></r>'

Installation

Either clone this repo or use pip like this:

pip install https://github.com/hay/xml2json/zipball/master

License

xml2json is released under the terms of the MIT license.

Contributors

This script was originally written by R.White, Rewritten to a command line utility by Hay Kranen with contributions from George Hamilton (gmh04) and Dan Brown (jdanbrown)

Links

How it works

xml2json relies on ElementTree for the XML parsing. This is based on pesterfish.py but uses a different XML->JSON mapping. The XML -> JSON mapping is described here

XML                              JSON
<e/>                             "e": null
<e>text</e>                      "e": "text"
<e name="value" />               "e": { "@name": "value" }
<e name="value">text</e>         "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e>        "e": { "#text": "text", "a": "text" }

This is very similar to the mapping used for Yahoo Web Services

This is a mess in that it is so unpredictable -- it requires lots of testing (e.g. to see if values are lists or strings or dictionaries). For use in Python this could be vastly cleaner. Think about whether the internal form can be more self-consistent while maintaining good external characteristics for the JSON.

Look at the Yahoo version closely to see how it works. Maybe can adopt that completely if it makes more sense...

R. White, 2006 November 6

More Repositories

1

stapes

the Javascript MVC microframework that does just enough
JavaScript
443
star
2

markov

PHP Markov chain text generator
PHP
121
star
3

dataknead

Effortless conversion between data formats like JSON, XML and CSV
Python
119
star
4

wiki-tools

Code for my Wikimedia Labs Tools account
Shell
90
star
5

facetool

Command line utility to manipulate faces in videos and images
Python
51
star
6

hu-dsf

Introduction course to data science using the Python programming language in the form of Jupyter Notebooks
Jupyter Notebook
25
star
7

sum

The Sum of All Knowledge - A project to visualize the knowledge getting added to Wikidata, Wikipedia and Wikimedia Commons.
HTML
12
star
8

json2sql

PHP script that converts a JSON file to a series of SQL INSERT staments
PHP
11
star
9

audio2text

Python command line utility wrappers for Whispercpp and other speech-to-text utilities
Python
10
star
10

wiki-text-nlp

Extract 'Did you know?' facts from Wikipedia articles
Jupyter Notebook
9
star
11

wikilope

Discover interesting connections starting from a Wikipedia article
JavaScript
8
star
12

fronteers

HTML5 presentation of the 2010 fronteers conference
JavaScript
8
star
13

httprequest

Simple PHP HttpRequest class that mimics jQuery's $.ajax function
PHP
8
star
14

wdreconcile

Map strings to Wikidata QID's using various methods
Python
7
star
15

valenski

A lightweight set of SASS mixins, classes, resets and values that simplify your workflow
SCSS
7
star
16

csv2json

PHP script that converts a CSV file to a JSON object. It expects all values to * be enclosed in double quotes (") and to be seperated by commas (,).
7
star
17

nagfree

A Chrome extension that makes a few sites a little better
JavaScript
6
star
18

radio

Very simple online radio player
JavaScript
5
star
19

backuptweets

Back up your Twitter tweets with Node.js without oAuth
JavaScript
5
star
20

mandelbrot

Mandelbrot set using HTML5 tech
JavaScript
4
star
21

rare-tijden

Dutch Chrome and Firefox Extension. Plakt de zin 'Het zijn rare tijden.' voor artikelen op de grootste Nederlandse en Vlaamse nieuwssites.
JavaScript
4
star
22

photocommons

WordPress plugin to include photos from Wikimedia Commons on your blog
JavaScript
4
star
23

csv2sql

Convert CSV to SQL
PHP
4
star
24

chantek

An unaspiring read-only HTTP API server written in Python.
Python
4
star
25

hay

Hay's tools and tests
JavaScript
4
star
26

msn-messenger-xml-importer

Imports and parses MSN Messenger chat log XML files
Vue
4
star
27

rijksmuseumapi

Set of PHP classes for the Rijksmuseum API
PHP
4
star
28

wmnl-theme

Customizable Wikimedia theme
PHP
3
star
29

templates

Templates for projects
JavaScript
3
star
30

catalytics

Chrome extension that replaces people on the Google Analytics homepage with surly looking cats.
JavaScript
3
star
31

twitter-cli

Command-line tool to search and save tweets, using the Streaming API
Python
3
star
32

wikistuff

Some unrelated Wiki stuff
Jupyter Notebook
3
star
33

jsonviewer

View JSON files in a browser, either direct by URL or by copy-pasting the source
PHP
2
star
34

trackings

Tracking snippets for Google Analytics and Piwik / Matomo
JavaScript
2
star
35

dexify

Converts text-based files to pretty HTML and creates a searchable index.
JavaScript
2
star
36

html5video

Some HTML5 video element samples
2
star
37

pywikibot-hay

Some pywikibot scripts i wrote
Python
2
star
38

wherewereyou

Enter your Twitter username to view all the places you've been on a map
PHP
2
star
39

haybase

Makes developing WordPress plugins and themes infinitely more easy
PHP
2
star
40

fieldbook-download

Downloads all sheets in a Fieldbook and all images
JavaScript
2
star
41

searchtweets

Make a backup JSON of all tweets from a search query
JavaScript
1
star
42

googleeyes

1
star
43

piwikanalytics

Fork of Piwik Analytics with support for the async code
PHP
1
star
44

strikeout

A Dropbox-powered todo list and task manager
JavaScript
1
star
45

inspirator

Inspirator for designers!
PHP
1
star
46

datasets

Datasets i've made or gathered and are not published somewhere else or easily accessible
1
star
47

gifvideo

Play videos inline on iOS Safari, just like GIF files.
JavaScript
1
star
48

wdtool

Tool to match strings to Wikidata items
Python
1
star
49

amsterdammuseum

Datasets and tools for the Amsterdam Museum API
1
star
50

erfgoedbot

Facebook Messenger bot with Dutch erfgoed data, linked to Wikidata
JavaScript
1
star
51

youtube-angular

A simple example application that uses Angular.js. Search and play YouTube videos.
JavaScript
1
star
52

jsdynaload

A dynamic Javascript file loader with support for arrays and callbacks
JavaScript
1
star
53

setup-processing

Examples for the "Apps om in te Lijsten" course for SETUP Utrecht
Processing
1
star
54

bbgen

Python
1
star