I want to get json data from given url. And what json data do I need to convert to xml form

I want to get JSON data from a given URL

http://www.deanclatworthy.com/imdb/?=The+Green+Mile 

and convert JSON data to XML. I used urllib and json to convert JSON objects to a python dictionary.

Here is my code:

 import json json_string = '{"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340}' new_python_object = json.loads(json_string) print(json_string) print() print (new_python_object) 

And the result:

 {"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340} {'ukscreens': 340, 'rating': '8.4', 'genres': 'Crime,Drama,Fantasy,Mystery', 'title': 'The Green Mile', 'series': 0, 'imdbid': 'tt0120689', 'year': '1999', 'votes': '281023', 'languages': 'English ,French', 'stv': 0, 'country': 'USA', 'usascreens': 2875, 'imdburl': 'http://www.imdb.com/title/tt0120689/'} 
+4
source share
3 answers

Using requests and dict2xml libraries:

 >>> import requests >>> r = requests.get("http://www.deanclatworthy.com/imdb/?q=The+Green+Mile") >>> import dict2xml >>> xml = dict2xml.dict2xml(r.json) >>> print xml <country>USA</country> <genres>Crime,Drama,Fantasy,Mystery</genres> <imdbid>tt0120689</imdbid> <imdburl>http://www.imdb.com/title/tt0120689/</imdburl> <languages>English,French</languages> <rating>8.5</rating> <runtime>189min</runtime> <series>0</series> <stv>0</stv> <title>The Green Mile</title> <ukscreens>340</ukscreens> <usascreens>2875</usascreens> <votes>344054</votes> <year>1999</year> 
+1
source

Now that you have a useful python dictionary, you must write it in xml.

What does your XML format look like? Python provides some tools in the standard library for writing / working with xml. http://docs.python.org/library/xml.dom.minidom.html

Writing will take some effort because python does not know the structure of your XML document. A little Google research can go a long way.

0
source

On the ActiveState recipes page, the entry (Cory Fabre) is very important for converting a python dict to and from XML. I have been using it a bit lately and it seems to work very well. Here's a link:

http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/

This can be useful if you are lucky that your JSON structure displays the XML structure pretty well.

0
source

Source: https://habr.com/ru/post/1436674/


All Articles