Skip to content

Instantly share code, notes, and snippets.

@LegoStormtroopr
Created September 1, 2013 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LegoStormtroopr/6402059 to your computer and use it in GitHub Desktop.
Save LegoStormtroopr/6402059 to your computer and use it in GitHub Desktop.
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