showing results for - "xml to json python"
Cassandre
11 Jun 2018
1from xml.dom import minidom
2
3# parse an xml file by name
4mydoc = minidom.parse('items.xml')
5
6items = mydoc.getElementsByTagName('item')
7
8# one specific item attribute
9print('Item #2 attribute:')
10print(items[1].attributes['name'].value)
11
12# all item attributes
13print('\nAll attributes:')
14for elem in items:
15    print(elem.attributes['name'].value)
16
17# one specific item's data
18print('\nItem #2 data:')
19print(items[1].firstChild.data)
20print(items[1].childNodes[0].data)
21
22# all items data
23print('\nAll item data:')
24for elem in items:
25    print(elem.firstChild.data)
Nabil
09 Jul 2017
1Soviut's advice for lxml objectify is good. With a specially subclassed simplejson, you can turn an lxml objectify result into json.
2
3import simplejson as json
4import lxml
5
6class objectJSONEncoder(json.JSONEncoder):
7  """A specialized JSON encoder that can handle simple lxml objectify types
8      >>> from lxml import objectify
9      >>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")       
10      >>> objectJSONEncoder().encode(obj)
11      '{"price": 1.5, "author": "W. Shakespeare"}'       
12 """
13
14
15    def default(self,o):
16        if isinstance(o, lxml.objectify.IntElement):
17            return int(o)
18        if isinstance(o, lxml.objectify.NumberElement) or isinstance(o, lxml.objectify.FloatElement):
19            return float(o)
20        if isinstance(o, lxml.objectify.ObjectifiedDataElement):
21            return str(o)
22        if hasattr(o, '__dict__'):
23            #For objects with a __dict__, return the encoding of the __dict__
24            return o.__dict__
25        return json.JSONEncoder.default(self, o)
26See the docstring for example of usage, essentially you pass the result of lxml objectify to the encode method of an instance of objectJSONEncoder
27
28Note that Koen's point is very valid here, the solution above only works for simply nested xml and doesn't include the name of root elements. This could be fixed.
29
30I've included this class in a gist here: http://gist.github.com/345559
queries leading to this page
python xml to json converterxml to python objecthow to convert xml to json in pythonget xml tag value in pythonpython function convert xml to jsonpython code to read an xml file and search a fieldopen xml file python 3xml in json field pythonimport xml data to pythonconvert xml to json using pythonhow to access xml files through pythonpython read xmlxml to json using pythonread xml file in python 3 exampleparse xml to json pythonhow to get data from a local xml file using pythonhow to view xml file in pythonxml to json pythonpython read xml from urlaccessing xml with pythonpython library to read xml filepython return xmlpython xml realpythonhow read xml in pythonload xml file in pythonpython read xml file as string 2021xml reader pythonpython convert xml to json examplehow to open a xml file 22python 22read xmlns pythonread xml as json pythonconvert xml to json using python4read xml file encoding pythonpython with xml filesxml to json pytonparse xml file in pythonread xml files in pythonread xml file python to analyzexml data read pythonextracting data from xml to json pythonhow to read xml file using pythonpython xml libraryhow to convert json to xml using pythonworking with xml file in pythonxml to a parse json file in pythonhow to import xml pythonhow to open xml file in pythonxml file read using pythonpython xml vs jsonxml with python webread xml in ipythonpython read xml config filepython extract xml from fileconvert xml to json for whole folder pythonload xml file pythonconverting xml to json using pythonpython request xml to jsonread from mrss xml file in pythonpython extract from xmlxml python readxml reading pythonxml reading in pythonhow to import xml file in pythonpython import xmlpython get xml file from urlpython request xml dataextract data from xml file using pythonpython load xmlconverting xml to json pythonparse xml file python xml to json pythonprocess an xml file pythonxml api to json pythonhow to read xml file with pythonopen xml in pythonread xml file python as objectpython xml reader examplegenerate xml with pythonopen xml file in pythonpython convert xml string to jsonpython xml readpython using xmlconvert http xml to json pythonpython convert xml to jsonuse xml pythonhow to read xml file as a string pythonpython work with xml filesread from xml file in pythonload file xml pythonpython string find xmlread xml in a string pythonread xml in pythonhow to access xml files through python 5cxml read pythonpython read xml stringpackage for converting xml to json in pythonpython import xml fileload xml in pythonread xml file into string pythonimport xml in pythonpython return a xml filereading xml file in python pandasxml to json in pythonxml string reader pythonread data from xml pythonpython requests xml to jsonxml to json pyrhonhow to read xml file in pythonload xml from link pythoncopy xml to json pythonimport xml file pythonhow to read xml file pythonreading a xml file in pythonpython xml findconvert xml string to json object pythonresponse xml to json pythonpython xmlhow to read a xml file in pythonxml to json python bs4read xml document pythonxml python libraryhow to read an xml file in pythonread xml file in python and check for stringreading xml in pythonread xml file using file read 28 29 in pythontransfer xml to json pythonpythn read from xml filehow to read xml in pythonpython xml to json conversionhow to read an xml file using pythopython xml converting to jsonextract xml information pythonxml request in pythonpython 3 read xml fileworking with xml in pythonpython parse xml to jsonimport xml to json pythonpython xml to jsonread xml in python textxml to json converter pythonpython 2b read xml filehow to read the attributes of xml in pythonread xml file with python exampleread xml data in pythonpython xml filesread in xml data pythonopen text formatted xml file in pythonread an xml file in pythonread xml as string pythonhow to load xml in pythonpython read xml file from urlread xml python pandasconvert xml to json pythonload xml from file pythonread and right xml files pythonload xml pythonread data from xml file in pythonpython3 read xmlxml and json output from pythonpython convert xml into jsonxml to json python without librarypython xml extractxml file from pythonopen xml file pythoinopen xml files pythonread xml file python with 22 26 22how to convert xml to json pythonconvert json to xml pythonsimple xml to json pythonhow to change output xml to json in pythonpython read string from xml filepython xml readerpython xml loadxml to json python pypiread the xml file in pythonpython xml from stringhow to read xml file url in pythonprint xml file pythonpython requests read xml responseread xml file in pythonhow to import xml file pythonpython simple read xmlfrom xml json pythonpython xml handlingpython xml real pythonpython with xmlopen xml files in pythonread xml tag value in pythonpython convert xml to json 3fwork with xml files in pythonpython load xml fileconvert xml to json python codepython read xml datahow to read xml tags in pythonread in xml file pythonpython open xml fileread xml file in pythonhow to use xml file in pythonxml json pythonpython read data xml fileget xml from url pythoncan python read xmlhow to read xml data in pythonhow to open xml file through pythonread xml files pythonpython3 xml findrequest python3 get xmlread xml as text file pythonxml response to json pythonpython read from xmlhow to read xml in python3xml file read in pythonget xml data pythonhow to handle a xml file on pythonpython extract xml datapythonn get xmlread xml file in python bs4open xml file with pythonpython read xml file and parseworking with xml files in pythonread a xml in pythonopen a xml file in pythonread xml data pythonxml to jsonread xml file into pythonxml pythonpython reading xmlxml to josnpython convert xml to json no modulepython xml file use xml in pythonpython how to write read xmlpython read xml responseconvert xml string to json pythonread xml pythonxml data importer python applicationhow to get data from xml file in pythonpython xml file functionbest way to read an xml file in pythonread data from xml in pythonconvert xml response to json in pythonread xml file and convert to json in pythonopen xml et pythonread xml file as text file pythonpython view xml fileget xml from url python 3xml read node pythonread xml from string pythonread xml using pythonpython xml document readerpython library to convert json to xmlfiles xml in pythonaccessing xml file in pythonxml to json in python3read xml file python pandaspython 2c using xml xml structure in pythonpython read from xml filehow to use xml files in pythonread xml as database pythonxml to json python xmltodictpython get xml attributespython read 3fxml version 3d 221 1 22 3fxml file operations in pythonpython how to read xml filejson to xml with attributes pythonformat xml to json pytohnpython xml displayxml reading attributes pythonpython get data from xmlpython program to convert xml file to json filepython open xmlxml to a parsed json file in pythonhow to convert xml into json in pythonpython extract information xmlturn xml into json pythonpython read xml file to stringget xml attribute value pythonread xml file python how to read xml files using pythonreading xml with pytonpython reading xml fileread xml file in python pandasparsing xml file in pythonpython generate xmlpython to generate xmlimport xml file in pythonhow to handle xml file in pythonextract information from xml pythonpython json to xmlopen xml file pythonread xml file with pythonpython read xml fileconvert xml to json 22without libraries 22 pythonget xml data from url pythonconverting xml to json in pythonpython read xml file as stringxml in pythonpython read xml filesxml files in pythonwhat is xml file in pythonreading xml file in pythonread and print xml using pythonpython read xml anserrpython read xml contentconvert xml file to json pythonread xml with pythonopen xml pythonpython xml readingload xml file python extract attributeparse xml files in pythonparse xml to json python no libraryread xml files with elementree pythonfunction to read xml in pythonread xml file pythonpython to read xml fileconvert xml into json pythonread xml file content using file read 28 29 in pythonread a xml file in pythonconvert xml to json in pythonpython load xml from fileconvert xml response to json pythonconvert xml to json without library pythonhow to read xml pythonxml to json python