Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a basic XML editor that shows of how to do very simple XML validation inside of a PyQT QTextEdit field. All this does is check for document well-formed-ness, but using the LXML library it shouldn't be difficult to extend this to perform Schema or DOCTYPE based validation.
import sys
from lxml import etree
from PyQt4 import QtCore, QtGui
class editor(QtGui.QMainWindow):
def __init__(self):
super(editor, self).__init__()
self.text = QtGui.QTextEdit()
self.setCentralWidget(self.text)
self.text.textChanged.connect(self.validate)
self.statusBar()
self.setWindowTitle('Simple XML editor')
self.show()
def validate(self):
try:
root = etree.fromstring(str(self.text.toPlainText()))
self.statusBar().showMessage("Valid XML")
except etree.XMLSyntaxError, e:
msg = e.error_log.last_error.message
line=e.error_log.last_error.line
col=e.error_log.last_error.column
self.statusBar().showMessage("Invalid XML: Line %s, Col %s: %s"%(line,col,msg))
except:
self.statusBar().showMessage("Invalid XML: Unknown error")
def main():
app = QtGui.QApplication(sys.argv)
w = editor()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.