Skip to content

Commit

Permalink
Merge pull request #26 from vinitkumar/fix/bug-with-multiple-records
Browse files Browse the repository at this point in the history
Fix/bug with multiple records
  • Loading branch information
Vinit Kumar authored Nov 13, 2017
2 parents bec3d87 + 1a7cf2e commit 707bb2b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
12 changes: 5 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Beautifulsoup4==4.4.1
dict2xml==1.3
json2xml==1.0.1
requests==2.9.1
simplejson==3.6.5
six==1.9.0
wheel==0.24.0
dict2xml==1.5
six==1.11.0
lxml==4.1.1
requests==2.18.4
xmltodict==0.11.0
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '1.3.0'
version = '2.0.0'

setup(
name='json2xml',
Expand All @@ -12,11 +12,11 @@
packages=find_packages(),
zip_safe=False,
include_package_data=True,
install_requires=['BeautifulSoup4==4.5.3',
install_requires=[
'dict2xml==1.5',
'simplejson==3.10.0',
'six==1.10.0',
'lxml==3.7.3',
'requests==2.13.0',
'six==1.11.0',
'lxml==4.1.1',
'requests==2.18.4',
'xmltodict==0.11.0'
],
)
24 changes: 11 additions & 13 deletions src/json2xml.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#! /usr/bin/env python
import argparse
import json
import sys
import requests
import simplejson

import dict2xml
import json
from bs4 import BeautifulSoup
import requests


class Json2xml(object):
Expand All @@ -32,7 +31,7 @@ def __init__(self, data: str) -> None:
def fromjsonfile(cls, filename: str):
try:
json_data = open(filename)
data = simplejson.load(json_data)
data = json.load(json_data)
json_data.close()
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
Expand All @@ -46,8 +45,8 @@ def fromjsonfile(cls, filename: str):
#
# ---------------------------------
@classmethod
def fromurl(cls, url: str):
response = requests.get(url)
def fromurl(cls, url: str, params=None):
response = requests.get(url, params=params)
if response.status_code == 200:
return cls(response.json())
else:
Expand Down Expand Up @@ -75,9 +74,8 @@ def fromstring(cls, data: str):
# ---------------------------------
def json2xml(self):
if self.data:
xmldata = dict2xml.dict2xml(self.data)
xml = BeautifulSoup(xmldata, "html.parser")
return xml
xmldata = dict2xml.dict2xml(self.data, wrap="all", indent=" ")
return xmldata


def main(argv=None):
Expand All @@ -90,17 +88,17 @@ def main(argv=None):
if args.url:
url = args.url
data = Json2xml.fromurl(url)
print(Json2xml.json2xml(data))
return Json2xml.json2xml(data)

if args.file:
file = args.file
data = Json2xml.fromjsonfile(file)
print(Json2xml.json2xml(data))
return Json2xml.json2xml(data)

if args.data:
str_data = args.data
data = Json2xml.fromstring(str_data)
print(Json2xml.json2xml(data))
return Json2xml.json2xml(data)

if __name__ == "__main__":
main(sys.argv)
10 changes: 8 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@


import unittest
from collections import OrderedDict

import xmltodict

from src.json2xml import Json2xml


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


if __name__ == '__main__':
Expand Down

0 comments on commit 707bb2b

Please sign in to comment.