Skip to content

Commit

Permalink
fix(src): refactor main code for generating xml
Browse files Browse the repository at this point in the history
- remove the dependency on BeautifulSoup and just use the dict2xml code
- fixes #21, issue with wrong outout on multiple records
- remove unused packages

- Github Issue:
- Trello Ticket:

Signed-off-by: Vinit Kumar <[email protected]>
  • Loading branch information
Vinit Kumar committed Nov 13, 2017
1 parent bec3d87 commit d87431f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion 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 = '1.4.0'

setup(
name='json2xml',
Expand Down
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)

0 comments on commit d87431f

Please sign in to comment.