1
votes
import MySQLdb
from xml.dom import minidom
import xml.dom.minidom

xmldoc = xml.dom.minidom.parse("Country.xml")

country= xmldoc.getElementsByTagName("country")[0]

sensors = country.getElementsByTagName("sensor")

for sensor in sensors:
    Translation = sensor.getElementsByTagName("Translation")[0].firstChild.data
    Name = sensor.getElementsByTagName("Name")[0].firstChild.data
    Prefix = sensor.getElementsByTagName("Prefix")[0].firstChild.data
    Percentage = sensor.getElementsByTagName("Percentage")[0].firstChild.data
    Position = sensor.getElementsByTagName("Position")[0].firstChild.data

    db = MySQLdb.connect("localhost","root","akshay","country")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS country_table")

# Create table as per requirement
sql = """CREATE TABLE country_table (_
         Translation  VARCHAR(20) NOT NULL,
         Name  VARCHAR(20) NOT NULL,
         Prefix INT(5) NOT NULL,  
         Percentage INT(3) NOT NULL,
         Position INT(3) NOT NULL
          )"""

sql1= ("INSERT INTO country_table(Translation, Name, Prefix, Percentage,Position) VALUES(%s, %s, %s, %s,%s)"
   %(Translation, Name, Prefix, Percentage,Position))

try:
   # Execute the SQL command

     cursor.execute(sql1)

except:
   # Rollback in case there is any error
   db.rollback()

# disconnect from server
db.close()

I am getting the following error:

Traceback (most recent call last): File "test.py", line 5, in xmldoc = xml.dom.minidom.parse("Country.xml") File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1918, in parse return expatbuilder.parse(file) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 924, in parse result = builder.parseFile(fp) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 211, in parseFile parser.Parse("", True) xml.parsers.expat.ExpatError: no element found: line 43, column 0

Please help.

1
Could you post the XML file ? - kaiser

1 Answers

0
votes

You haven't closed Origination and Transaction tags , just close them and it should work just fine.