Jul-24-2025, 10:02 AM
(This post was last modified: Jul-24-2025, 10:08 AM by Gribouillis.)
I have a below program. The problem is if i pull the text value from the tkinter entrybox "dbnameentry.get()" and pass it the create the root element of the xml document " top = ET.Element(dbnameentry.get())" then I get the XML parse error while parsing the created file.
Error-> xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1
However if I pass the manual value like "top = ET.Element('Students')" then the while parsing i do not get error. I commented this line in the below program.
I need help if there is any known issue or any issue in program itself.
Program Sample:
Error-> xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1
However if I pass the manual value like "top = ET.Element('Students')" then the while parsing i do not get error. I commented this line in the below program.
I need help if there is any known issue or any issue in program itself.
Program Sample:
#################################################
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk, filedialog, messagebox
import xml.etree.ElementTree as ET
def create_db_window():
directory_path = filedialog.askdirectory()
def create_db():
if not dbnameentry.get():
messagebox.showerror("Error", "Please enter filename")
elif not dbpwdentry.get():
messagebox.showerror("Error", "please set password")
else:
top = ET.Element(dbnameentry.get())
# top = ET.Element('Students')
app = ET.SubElement(top, "app")
studentname = ET.SubElement(app, "studentname")
studentname.text = "Superman"
studentid = ET.SubElement(app, "ID")
studentid.text = "87"
studentclass = ET.SubElement(app, "class")
studentclass.text = "programming"
try:
tree = ET.ElementTree(top)
tree.write(f'{directory_path}/{dbpwdentry.get()}.xml')
messagebox.showinfo("Success", "Database created")
except Exception as e:
messagebox.showerror("Error", f"Failed to save file:\n{e}")
createdbwindow = Toplevel()
createdbwindow.geometry("350x160")
dbnamelabel = Label(createdbwindow, text="Enter Db Name:")
dbnamelabel.pack()
dbnameentry = Entry(createdbwindow)
dbnameentry.pack()
dbpwdlabel = Label(createdbwindow, text="Set DB Password:")
dbpwdlabel.pack()
dbpwdentry = Entry(createdbwindow, show="*")
dbpwdentry.pack()
dbcreatebutton = Button(createdbwindow, text="Create", command=create_db)
dbcreatebutton.pack()
mainwindow = Tk()
mainwindow.geometry('200x200')
createwindowbutton = Button(mainwindow, text="Create new window", command=create_db_window)
createwindowbutton.pack()
mainwindow.mainloop()
#############################################################
Gribouillis write Jul-24-2025, 10:08 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
