代码拉取完成,页面将自动刷新
import sys
import os
import uuid
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtXml import *
class Demo(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.xml=os.path.join(self.parent.getAppDir(),'test.xml')
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignmentFlag.AlignTop)
toolBar=QToolBar()
btn=QPushButton('Create')
btn.clicked.connect(self.Create)
toolBar.addWidget(btn)
btn=QPushButton('Add Root')
btn.clicked.connect(self.AddRoot)
toolBar.addWidget(btn)
btn=QPushButton('Add Child')
btn.clicked.connect(self.AddChild)
toolBar.addWidget(btn)
btn=QPushButton('Add Child with attribute')
btn.clicked.connect(self.AddChildAttribute)
toolBar.addWidget(btn)
btn=QPushButton('Add Child with TextNode')
btn.clicked.connect(self.AddChildTextNode)
toolBar.addWidget(btn)
btn=QPushButton('SaveFile')
btn.clicked.connect(self.SaveFile)
toolBar.addWidget(btn)
btn=QPushButton('LoadFile')
btn.clicked.connect(self.LoadFile)
toolBar.addWidget(btn)
layout.addWidget(toolBar)
self.text=QTextBrowser()#QTextEdit的只读控件
layout.addWidget(self.text)
self.setLayout(layout)
def Create(self):
self.doc= QDomDocument()
self.doc.appendChild(self.doc.createProcessingInstruction('xml',"version=\"1.0\" encoding=\"UTF-8\""))
self.text.setPlainText(self.doc.toString())
def AddRoot(self):
if self.root is None:
self.root=self.doc.createElement('Books')
self.doc.appendChild(self.root)
self.text.setPlainText(self.doc.toString())
def AddChild(self):
child=self.doc.createElement('Book')
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def AddChildAttribute(self):
child=self.doc.createElement('Book')
child.setAttribute('ID',uuid.uuid1().hex)
child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
child.setAttribute('Price',f'{random.randint(1,100)}')
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def AddChildTextNode(self):
child=self.doc.createElement('Book')
child.setAttribute('ID',uuid.uuid1().hex)
child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
child.setAttribute('Price',f'{random.randint(1,100)}')
des=self.doc.createElement('Description')
des.appendChild(self.doc.createTextNode('Book Description'))
child.appendChild(des)
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def SaveFile(self):
file=QFile(self.xml)
file.open(QIODeviceBase.OpenModeFlag.ReadWrite|QIODeviceBase.OpenModeFlag.Truncate)
self.doc.save(QTextStream(file),4)
file.close()
def LoadFile(self):
file=QFile(self.xml)
file.open(QIODeviceBase.OpenModeFlag.ReadOnly)
content=file.readAll().toStdString()
self.text.setPlainText(content)
self.doc= QDomDocument()
#self.doc.setContent(file)#此方式无效,暂时不清楚原因
self.doc.setContent(content)
file.close()
self.root=self.doc.documentElement()
def runDemo(parent):
wigdet = Demo(parent)
return wigdet
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。