Skip to content

Commit

Permalink
Merge pull request #4 from vinitkumar/feature/add-support-to-read-fro…
Browse files Browse the repository at this point in the history
…m-file

Feature/add support to read from file
  • Loading branch information
Vinit Kumar committed Feb 13, 2015
2 parents 751091d + 6f0ce18 commit 3df6495
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 128 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
json2xml.egg-info\
json2xml.egg-info
build
*.swp
dist/*
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: python
# command to install dependencies
install:
- pip install -r requirements.txt
- python setup.py install
# command to run tests
script: python tests/test.py
Binary file removed dist/json2xml-0.6-py2.7.egg
Binary file not shown.
32 changes: 32 additions & 0 deletions examples/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false,
"name": "monalisa octocat",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "[email protected]",
"hireable": false,
"bio": "There once was...",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z"
}
35 changes: 0 additions & 35 deletions examples/example.py

This file was deleted.

19 changes: 0 additions & 19 deletions examples/example2.py

This file was deleted.

56 changes: 51 additions & 5 deletions json2xml/json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,61 @@
from BeautifulSoup import BeautifulStoneSoup

class Json2xml(object):

# -------------------------------
##
# @Synopsis This class could read a json file
# from the filesystem or get a file from across
# the Internet, and convert that json object to
# xml
#
# @Param data : Data to be fed into the system.
#
# @Returns Null
# ---------------------------------
def __init__(self, data):
self.data = data

def __init__(self, url):
self.url = url
# -------------------------------
##
# @Synopsis Read JSON from a file in
# the system
# ---------------------------------
@classmethod
def fromjsonfile(cls, filename):
try:
json_data = open(filename)
data = simplejson.load(json_data)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
data = []
return cls(data)

# -------------------------------
##
# @Synopsis Fetches the JSON
# data from an URL Source.
#
# ---------------------------------
@classmethod
def fromurl(cls, url):
data = simplejson.load(urllib.urlopen(url))
return cls(data)

# -------------------------------
##
# @Synopsis This method actually
# converts the json data that is converted
# into dict into XML
#
# @Returns XML
# ---------------------------------
def json2xml(self):
data = simplejson.load(urllib.urlopen(self.url))
if data:
xmldata = dict2xml.dict2xml(data)
if self.data:
xmldata = dict2xml.dict2xml(self.data)
xml = BeautifulStoneSoup(xmldata)
return xml




82 changes: 16 additions & 66 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##About

This is a simple python module to convert json data to xml data.

A Simple python utility to convert JSON to xml. It support conversation
from a json file or from an URL.

### How to install

Expand All @@ -11,76 +11,26 @@ pip install json2xml

###How to use

Check out the examples given in examples directory.

Run:
```bash
#### From JSON File

$ python example.py
```python
from src.json2xml import Json2xml
data = Json2xml.fromjsonfile('examples/example.json').data
data_object = Json2xml(data)
data_object.json2xml()
```

Gives:
#### From WEB Url

```xml
<results>
<address_components>
<long_name>Pune</long_name>
<short_name>Pune</short_name>
<types>locality</types>
<types>political</types>
</address_components>
<address_components>
<long_name>Pune</long_name>
<short_name>Pune</short_name>
<types>administrative_area_level_2</types>
<types>political</types>
</address_components>
<address_components>
<long_name>Maharashtra</long_name>
<short_name>MH</short_name>
<types>administrative_area_level_1</types>
<types>political</types>
</address_components>
<address_components>
<long_name>India</long_name>
<short_name>IN</short_name>
<types>country</types>
<types>political</types>
</address_components>
<formatted_address>Pune, Maharashtra, India</formatted_address>
<geometry>
<bounds>
<northeast>
<lat>18.6346965</lat>
<lng>73.9894867</lng>
</northeast>
<southwest>
<lat>18.4136739</lat>
<lng>73.7398911</lng>
</southwest>
</bounds>
<location>
<lat>18.5204303</lat>
<lng>73.8567437</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<northeast>
<lat>18.6346965</lat>
<lng>73.9894867</lng>
</northeast>
<southwest>
<lat>18.4136739</lat>
<lng>73.7398911</lng>
</southwest>
</viewport>
</geometry>
<types>locality</types>
<types>political</types>
</results>
<status>OK</status>
```python
from src.json2xml import Json2xml
data = data = Json2xml.fromurl('https://coderwall.com/vinitcool76.json').data
data_object = Json2xml(data)
data_object.json2xml()
```

These are two simple ways you can use this utility.

### Bugs, features

Create an issue in the repository and if you have a new feature that you want to add, please send a pull request.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
BeautifulSoup==3.2.1
argparse==1.2.1
dict2xml==1.3
six==1.9.0
wsgiref==0.1.2
simplejson==3.6.5
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from setuptools import setup, find_packages
import os

version = '0.6'
version = '1.0.0'

setup(
name='json2xml',
version=version,
description='To covert json data to xml data',
author='Vinit Kumar',
author_email='[email protected]',
url='http://github.com:vinitcool76/json2xml.git',
url='http://github.com:vinitkumar/json2xml.git',
packages=find_packages(),
zip_safe=False,
include_package_data=True,
Expand Down
23 changes: 23 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
##
# @file test.py
# @Synopsis Unit test for json2xml
# @author Vinit Kumar
# @version
# @date 2015-02-13


import unittest
from json2xml.json2xml import Json2xml

class Json2xmlTestCase(unittest.TestCase):

def test_is_json_from_file_works(self):
data = Json2xml.fromjsonfile('examples/example.json').data
data_object = Json2xml(data)
xml_output = data_object.json2xml()
is_xml = xml_output.XML_ENTITIES
self.assertEqual(is_xml, 'xml')


if __name__ == '__main__':
unittest.main()

0 comments on commit 3df6495

Please sign in to comment.