Skip to content

Commit 3df6495

Browse files
author
Vinit Kumar
committed
Merge pull request #4 from vinitkumar/feature/add-support-to-read-from-file
Feature/add support to read from file
2 parents 751091d + 6f0ce18 commit 3df6495

File tree

11 files changed

+137
-128
lines changed

11 files changed

+137
-128
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.pyc
2-
json2xml.egg-info\
2+
json2xml.egg-info
33
build
4+
*.swp
5+
dist/*

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: python
2+
# command to install dependencies
3+
install:
4+
- pip install -r requirements.txt
5+
- python setup.py install
6+
# command to run tests
7+
script: python tests/test.py

dist/json2xml-0.6-py2.7.egg

-2.33 KB
Binary file not shown.

examples/example.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"login": "octocat",
3+
"id": 1,
4+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
5+
"gravatar_id": "",
6+
"url": "https://api.github.com/users/octocat",
7+
"html_url": "https://github.com/octocat",
8+
"followers_url": "https://api.github.com/users/octocat/followers",
9+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
10+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
11+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
12+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
13+
"organizations_url": "https://api.github.com/users/octocat/orgs",
14+
"repos_url": "https://api.github.com/users/octocat/repos",
15+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
16+
"received_events_url": "https://api.github.com/users/octocat/received_events",
17+
"type": "User",
18+
"site_admin": false,
19+
"name": "monalisa octocat",
20+
"company": "GitHub",
21+
"blog": "https://github.com/blog",
22+
"location": "San Francisco",
23+
"email": "[email protected]",
24+
"hireable": false,
25+
"bio": "There once was...",
26+
"public_repos": 2,
27+
"public_gists": 1,
28+
"followers": 20,
29+
"following": 0,
30+
"created_at": "2008-01-14T04:33:35Z",
31+
"updated_at": "2008-01-14T04:33:35Z"
32+
}

examples/example.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/example2.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

json2xml/json2xml.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,61 @@
44
from BeautifulSoup import BeautifulStoneSoup
55

66
class Json2xml(object):
7+
8+
# -------------------------------
9+
##
10+
# @Synopsis This class could read a json file
11+
# from the filesystem or get a file from across
12+
# the Internet, and convert that json object to
13+
# xml
14+
#
15+
# @Param data : Data to be fed into the system.
16+
#
17+
# @Returns Null
18+
# ---------------------------------
19+
def __init__(self, data):
20+
self.data = data
721

8-
def __init__(self, url):
9-
self.url = url
22+
# -------------------------------
23+
##
24+
# @Synopsis Read JSON from a file in
25+
# the system
26+
# ---------------------------------
27+
@classmethod
28+
def fromjsonfile(cls, filename):
29+
try:
30+
json_data = open(filename)
31+
data = simplejson.load(json_data)
32+
except IOError as e:
33+
print "I/O error({0}): {1}".format(e.errno, e.strerror)
34+
data = []
35+
return cls(data)
1036

37+
# -------------------------------
38+
##
39+
# @Synopsis Fetches the JSON
40+
# data from an URL Source.
41+
#
42+
# ---------------------------------
43+
@classmethod
44+
def fromurl(cls, url):
45+
data = simplejson.load(urllib.urlopen(url))
46+
return cls(data)
47+
48+
# -------------------------------
49+
##
50+
# @Synopsis This method actually
51+
# converts the json data that is converted
52+
# into dict into XML
53+
#
54+
# @Returns XML
55+
# ---------------------------------
1156
def json2xml(self):
12-
data = simplejson.load(urllib.urlopen(self.url))
13-
if data:
14-
xmldata = dict2xml.dict2xml(data)
57+
if self.data:
58+
xmldata = dict2xml.dict2xml(self.data)
1559
xml = BeautifulStoneSoup(xmldata)
1660
return xml
1761

62+
63+
1864

readme.md

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##About
22

3-
This is a simple python module to convert json data to xml data.
4-
3+
A Simple python utility to convert JSON to xml. It support conversation
4+
from a json file or from an URL.
55

66
### How to install
77

@@ -11,76 +11,26 @@ pip install json2xml
1111

1212
###How to use
1313

14-
Check out the examples given in examples directory.
15-
16-
Run:
17-
```bash
14+
#### From JSON File
1815

19-
$ python example.py
16+
```python
17+
from src.json2xml import Json2xml
18+
data = Json2xml.fromjsonfile('examples/example.json').data
19+
data_object = Json2xml(data)
20+
data_object.json2xml()
2021
```
2122

22-
Gives:
23+
#### From WEB Url
2324

24-
```xml
25-
<results>
26-
<address_components>
27-
<long_name>Pune</long_name>
28-
<short_name>Pune</short_name>
29-
<types>locality</types>
30-
<types>political</types>
31-
</address_components>
32-
<address_components>
33-
<long_name>Pune</long_name>
34-
<short_name>Pune</short_name>
35-
<types>administrative_area_level_2</types>
36-
<types>political</types>
37-
</address_components>
38-
<address_components>
39-
<long_name>Maharashtra</long_name>
40-
<short_name>MH</short_name>
41-
<types>administrative_area_level_1</types>
42-
<types>political</types>
43-
</address_components>
44-
<address_components>
45-
<long_name>India</long_name>
46-
<short_name>IN</short_name>
47-
<types>country</types>
48-
<types>political</types>
49-
</address_components>
50-
<formatted_address>Pune, Maharashtra, India</formatted_address>
51-
<geometry>
52-
<bounds>
53-
<northeast>
54-
<lat>18.6346965</lat>
55-
<lng>73.9894867</lng>
56-
</northeast>
57-
<southwest>
58-
<lat>18.4136739</lat>
59-
<lng>73.7398911</lng>
60-
</southwest>
61-
</bounds>
62-
<location>
63-
<lat>18.5204303</lat>
64-
<lng>73.8567437</lng>
65-
</location>
66-
<location_type>APPROXIMATE</location_type>
67-
<viewport>
68-
<northeast>
69-
<lat>18.6346965</lat>
70-
<lng>73.9894867</lng>
71-
</northeast>
72-
<southwest>
73-
<lat>18.4136739</lat>
74-
<lng>73.7398911</lng>
75-
</southwest>
76-
</viewport>
77-
</geometry>
78-
<types>locality</types>
79-
<types>political</types>
80-
</results>
81-
<status>OK</status>
25+
```python
26+
from src.json2xml import Json2xml
27+
data = data = Json2xml.fromurl('https://coderwall.com/vinitcool76.json').data
28+
data_object = Json2xml(data)
29+
data_object.json2xml()
8230
```
8331

32+
These are two simple ways you can use this utility.
33+
8434
### Bugs, features
8535

8636
Create an issue in the repository and if you have a new feature that you want to add, please send a pull request.

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
BeautifulSoup==3.2.1
22
argparse==1.2.1
3+
dict2xml==1.3
4+
six==1.9.0
35
wsgiref==0.1.2
6+
simplejson==3.6.5

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from setuptools import setup, find_packages
22
import os
33

4-
version = '0.6'
4+
version = '1.0.0'
55

66
setup(
77
name='json2xml',
88
version=version,
99
description='To covert json data to xml data',
1010
author='Vinit Kumar',
1111
author_email='[email protected]',
12-
url='http://github.com:vinitcool76/json2xml.git',
12+
url='http://github.com:vinitkumar/json2xml.git',
1313
packages=find_packages(),
1414
zip_safe=False,
1515
include_package_data=True,

0 commit comments

Comments
 (0)