How to parse all nodes of given XML to an array in python -
how parse nodes of following xml array in python
<?xml version="1.0"?> <carlist> <car> <model>benz</model> <specifications> <engine>turbo</engine> <fueltype>petrol</fueltype> </specifications> </car> <car> <model>fiat</model> <specifications> <engine>diesel</engine> <fueltype>petrol</fueltype> </specifications> </car> </carlist>
xmltodict python module makes working xml feel working json
example:
s = """<?xml ... </carlist>""" parsed = xmltodict.parse(s) car in parsed['carlist']['car']: print(dict(car)) {u'specifications': ordereddict([(u'engine', u'turbo'), (u'fueltype', u'petrol')]), u'model': u'benz'} {u'specifications': ordereddict([(u'engine', u'diesel'), (u'fueltype', u'petrol')]), u'model': u'fiat'}
Comments
Post a Comment