Skip to content

Commit 707bb2b

Browse files
author
Vinit Kumar
authored
Merge pull request #26 from vinitkumar/fix/bug-with-multiple-records
Fix/bug with multiple records
2 parents bec3d87 + 1a7cf2e commit 707bb2b

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

requirements.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
Beautifulsoup4==4.4.1
2-
dict2xml==1.3
3-
json2xml==1.0.1
4-
requests==2.9.1
5-
simplejson==3.6.5
6-
six==1.9.0
7-
wheel==0.24.0
1+
dict2xml==1.5
2+
six==1.11.0
3+
lxml==4.1.1
4+
requests==2.18.4
5+
xmltodict==0.11.0

setup.py

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

3-
version = '1.3.0'
3+
version = '2.0.0'
44

55
setup(
66
name='json2xml',
@@ -12,11 +12,11 @@
1212
packages=find_packages(),
1313
zip_safe=False,
1414
include_package_data=True,
15-
install_requires=['BeautifulSoup4==4.5.3',
15+
install_requires=[
1616
'dict2xml==1.5',
17-
'simplejson==3.10.0',
18-
'six==1.10.0',
19-
'lxml==3.7.3',
20-
'requests==2.13.0',
17+
'six==1.11.0',
18+
'lxml==4.1.1',
19+
'requests==2.18.4',
20+
'xmltodict==0.11.0'
2121
],
2222
)

src/json2xml.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#! /usr/bin/env python
22
import argparse
3+
import json
34
import sys
4-
import requests
5-
import simplejson
5+
66
import dict2xml
7-
import json
8-
from bs4 import BeautifulSoup
7+
import requests
98

109

1110
class Json2xml(object):
@@ -32,7 +31,7 @@ def __init__(self, data: str) -> None:
3231
def fromjsonfile(cls, filename: str):
3332
try:
3433
json_data = open(filename)
35-
data = simplejson.load(json_data)
34+
data = json.load(json_data)
3635
json_data.close()
3736
except IOError as e:
3837
print("I/O error({0}): {1}".format(e.errno, e.strerror))
@@ -46,8 +45,8 @@ def fromjsonfile(cls, filename: str):
4645
#
4746
# ---------------------------------
4847
@classmethod
49-
def fromurl(cls, url: str):
50-
response = requests.get(url)
48+
def fromurl(cls, url: str, params=None):
49+
response = requests.get(url, params=params)
5150
if response.status_code == 200:
5251
return cls(response.json())
5352
else:
@@ -75,9 +74,8 @@ def fromstring(cls, data: str):
7574
# ---------------------------------
7675
def json2xml(self):
7776
if self.data:
78-
xmldata = dict2xml.dict2xml(self.data)
79-
xml = BeautifulSoup(xmldata, "html.parser")
80-
return xml
77+
xmldata = dict2xml.dict2xml(self.data, wrap="all", indent=" ")
78+
return xmldata
8179

8280

8381
def main(argv=None):
@@ -90,17 +88,17 @@ def main(argv=None):
9088
if args.url:
9189
url = args.url
9290
data = Json2xml.fromurl(url)
93-
print(Json2xml.json2xml(data))
91+
return Json2xml.json2xml(data)
9492

9593
if args.file:
9694
file = args.file
9795
data = Json2xml.fromjsonfile(file)
98-
print(Json2xml.json2xml(data))
96+
return Json2xml.json2xml(data)
9997

10098
if args.data:
10199
str_data = args.data
102100
data = Json2xml.fromstring(str_data)
103-
print(Json2xml.json2xml(data))
101+
return Json2xml.json2xml(data)
104102

105103
if __name__ == "__main__":
106104
main(sys.argv)

tests/test.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
import unittest
10+
from collections import OrderedDict
11+
12+
import xmltodict
13+
1014
from src.json2xml import Json2xml
1115

1216

@@ -15,8 +19,10 @@ def test_is_json_from_file_works(self):
1519
data = Json2xml.fromjsonfile('examples/example.json').data
1620
data_object = Json2xml(data)
1721
xml_output = data_object.json2xml()
18-
htmlkeys = xml_output.XML_FORMATTERS.keys()
19-
self.assertTrue('html' in htmlkeys)
22+
dict_from_xml = xmltodict.parse(xml_output)
23+
# since it's a valid XML, xml to dict is able to load it and return
24+
# elements from under the all tag of xml
25+
self.assertTrue(type(dict_from_xml['all']) == OrderedDict)
2026

2127

2228
if __name__ == '__main__':

0 commit comments

Comments
 (0)