1.python处理xml代码如下
import datetime
import xml.dom.minidom
import random
import re
import sys
import tkinter as tk
#设置主窗口
window=tk.Tk()
#设置主窗口标题栏
window.title('自动添加报文序列')
window.geometry('800x500')
l01=tk.Label(window,text='清输入原报文:')
l01.place(x=30,y=10,anchor='nw')
t01=tk.Text(window,height=12)
t01.place(x=30,y=30,anchor='nw')
def increase_copy_res():
content=t01.get('1.0','end-1c')#获取第二个窗格的内容
newcontent=[line for line in content.splitlines() if line.strip()] #去掉空行
datasource='\n'.join(newcontent)
tree=xml.dom.minidom.parseString(datasource) #minidom模块只支持utf-8格式文档
num=str(random.randint(10000000,99999999))
#获取某个标签
ele01=tree.getElementsByTagName('id')[0]
newid='202402213000'+num
ele01.firstChild.data=newid
root=tree.documentElement
t02.delete('1.0','end-1c')#清除窗格内容后再赋值
t02.insert('insert','<?xml version="1.0" encoding="GBK"?>\n')
t02.insert('insert',root.toxml())#给第二个窗格赋值
content01=t02.get('1.0','end-1c')#获取第二个窗格的内容
window.clipboard_clear()#清除剪贴板内容
window.clipboard_append(content01)#给剪贴板赋值
window.update()#更新剪贴板
b=tk.Button(window,text='增加序列并复制到剪贴板',width=20,height=3,command=increase_copy_res)
b.place(x=620,y=180,anchor='nw')
l02=tk.Label(window,text='结果预览:')
l02.place(x=30,y=210,anchor='nw')
t02=tk.Text(window,height=16)
t02.place(x=30,y=230,anchor='nw')
window.mainloop()
2.安装pyinstaller
3.打包成带一个参数的exe,打包好的文件在dist文件夹下
4.执行exe
直接双节dealwithxml.exe即可打开窗口
输入原始xml,点击按钮生成新的xml并自动复制到剪贴板
注意:要处理的xml文件不能带注释。
xml范例如下:
<?xml version="1.0" encoding="GBK"?>
<bookstore>
<book category="cooking">
<id>2024021312345678</id>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<id>2024021312345679</id>
<title lang="en">Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<id>2024021312345676</id>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>