我用Python实现了
读取保存在源文件夹中的xml文件名和内容
将读取到的文件名和内容保存在数据库中,保存成功后将源文件夹中的文件移动到另外一个文件夹
- import os
- import xml.etree.ElementTree as ET
- import pyodbc
- folder_path = r"D:\XML"
- files = [f for f in os.listdir(folder_path) if f.endswith(".xml")]
- for file in files:
- tree = ET.parse(os.path.join(folder_path, file))
- root = tree.getroot()
- name = os.path.splitext(file)[0]
- content = ET.tostring(root, encoding="unicode")
- connection_string = "DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=base;UID=sa;PWD=password"
- with pyodbc.connect(connection_string) as connection:
- cursor = connection.cursor()
- query = f"INSERT INTO xmlfile (Name, content) VALUES ('{name}', '{content}')"
- cursor.execute(query)
- destination_folder = r"D:\yijiexi"
- os.rename(os.path.join(folder_path, file), os.path.join(destination_folder, file))
复制代码 |