0

I wanted programmatically store an XML file into a database, then I'll tranform, analyse and visualize data. Since I'm more proficient with python than any other language, I wanted to put the XML into MongoDB. So I'm trying to first convert XML to JSON using xmltodict. I'm using colabs, but it keeps throwing the following ExpatError.

!pip install -q xlrd
!git clone https://github.com/martinblech/xmltodict.git
!pip install xmltodict

import xmltodict
import json
import pprint

 from google.colab import files
 files.upload() #here I upload customer.xml

with open('customer.xml') as fd:
doc = xmltodict.parse(fd.read())

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(json.dumps(doc))

ExpatError                                Traceback (most recent call last)

<ipython-input-14-b14a90e48155> in <module>()
      1 with open('customer.xml') as fd:
----> 2     doc = xmltodict.parse(fd.read())
      3 
      4 pp = pprint.PrettyPrinter(indent=4)
      5 pp.pprint(json.dumps(doc))

/usr/local/lib/python3.6/dist-packages/xmltodict.py in parse(xml_input, encoding, expat, process_namespaces, namespace_separator, disable_entities, **kwargs)
    325         parser.ParseFile(xml_input)
    326     else:
--> 327         parser.Parse(xml_input, True)
    328     return handler.item
    329 

ExpatError: no element found: line 1, column 0
Husun
  • 29
  • 1
  • 7
  • Without seeing the contents of that input xml file it's difficult for me to say. However, replacing that one line in your code with doc = xmltodict.parse(''); ie, asking the parser to process an empty input yields the same diagnostic message. – Bill Bell Dec 04 '19 at 15:52
  • The file is definitely not empty. How can I send the file to you? – Husun Dec 04 '19 at 16:02
  • Its xml version ="1.0", can that cause problems? – Husun Dec 04 '19 at 16:03
  • 1
    Here on SO, when you want to make a file available you post it on a site such as https://pastebin.com/. – Bill Bell Dec 04 '19 at 16:12

0 Answers0