Having a problem with attributes when importing XML data into SQL I have tried this 4 or 5 different ways but must be looking at the problem incorrectly. I am new to python but have experience with SQL and some other languages Have tried this a few different ways using xpath and it still doesn't work
<?xml version="1.0" encoding="UTF-8"?>
<PARTS>
<Header>
<Version>6.5</Version>
</Header>
<Items>
<Item MaintenanceType="A">
<HazardousMaterialCode>N</HazardousMaterialCode>
<ItemLevelGTIN GTINQualifier="UP">00651860733074</ItemLevelGTIN>
<PartNumber>14-230615</PartNumber>
<BrandAAIAID>BBGL</BrandAAIAID>
<BrandLabel>Bilstein</BrandLabel>
<ACESApplications>Y</ACESApplications>
<ItemQuantitySize UOM="EA">1.0</ItemQuantitySize>
<ContainerType>BX</ContainerType>
<QuantityPerApplication Qualifier="MAX" UOM="EA">1</QuantityPerApplication>
</Item>
</Items>
</PARTS>
from xml.etree import ElementTree
import mysql.connector
file_name = 'bsn.xml'
dom = ElementTree.parse(file_name)
mydb = mysql.connector.connect(user='frank', password='xxxxxx', host='127.0.0.1', database='scoresre', auth_plugin='mysql_native_password')
mycursor = mydb.cursor()
item = dom.findall('Items/Item')
for x in item:
PartNumber = x.find('PartNumber').text
BrandAAIAID = x.find('BrandAAIAID').text
BrandLabel = x.find('BrandLabel').text
ItemLevelGTIN = x.find('ItemLevelGTIN').text
GTINQualifier = x.find('.//GTINQualifier[@attr="UP"]')
print(PartNumber, BrandAAIAID, BrandLabel, ItemLevelGTIN, GTINQualifier)
val = (PartNumber, BrandAAIAID, BrandLabel, ItemLevelGTIN, GTINQualifier)
sql = "INSERT INTO scoreitem (B15_PartNumber, B20_BrandAAIAID, B25_BrandLabel, B10_ItemLevelGTIN, " \
"B11_GTINQualifier) VALUES (%s, %s, %s, %s, %s)"
mycursor.execute(sql, val)
mydb.commit()
The code is not importing the attributes under GTINQualifier="UP" -- UP is coming in as null ItemQuantitySize UOM="EA" -- EA comes in null when i use the same above syntax and Qualifier="MAX" UOM="EA" MAX and EA also come in as NULL. Thank you ahead of time