Files
email_to_xml/csv_to_xml.py
2024-12-30 08:17:02 +13:00

66 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import csv
import lxml.etree as ET
from io import BytesIO
# Define the DTD
dtd_content = """
<!ELEMENT items (item+)>
<!ELEMENT item (Item_ID, Item_Name, Item_Description, Item_Price, Item_Quantity)>
<!ELEMENT Item_ID (#PCDATA)>
<!ELEMENT Item_Name (#PCDATA)>
<!ELEMENT Item_Description (#PCDATA)>
<!ELEMENT Item_Price (#PCDATA)>
<!ELEMENT Item_Quantity (#PCDATA)>
"""
# Save DTD to a file
dtd_file = "items.dtd"
with open(dtd_file, "w") as f:
f.write(dtd_content)
# Function to validate XML against the DTD
def validate_xml(xml_string, dtd_path):
dtd = ET.DTD(dtd_path)
# Convert the XML string to bytes to handle the encoding declaration
xml_doc = ET.parse(BytesIO(xml_string.encode('utf-8')))
if dtd.validate(xml_doc):
print("XML is valid against the DTD.")
else:
print("XML validation failed:", dtd.error_log.filter_from_errors())
# Read CSV and generate XML
def csv_to_xml(csv_file, xml_file):
root = ET.Element("items")
with open(csv_file, newline='', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
item = ET.SubElement(root, "item")
ET.SubElement(item, "Item_ID").text = row["Item_ID"]
ET.SubElement(item, "Item_Name").text = row["Item_Name"]
ET.SubElement(item, "Item_Description").text = row["Item_Description"]
ET.SubElement(item, "Item_Price").text = row["Item_Price"]
ET.SubElement(item, "Item_Quantity").text = row["Item_Quantity"]
# Convert to string and validate
xml_string = ET.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8").decode("utf-8")
validate_xml(xml_string, dtd_file)
# Save XML to file
with open(xml_file, "w", encoding="utf-8") as f:
f.write(xml_string)
print(f"XML file saved to {xml_file}")
# Input and output file paths
csv_file = "items.csv" # Input CSV file
xml_file = "items.xml" # Output XML file
# Example CSV content for reference:
# Item_ID,Item_Name,Item_Description,Item_Price,Item_Quantity
# 1,Widget A,A useful widget,19.99,100
# 2,Gadget B,A versatile gadget,29.49,200
# Generate XML
csv_to_xml(csv_file, xml_file)