#!/usr/bin/python3 import sys import xml.etree.ElementTree as ET from qtpy import QtGui, QtWidgets, QtCore from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel def _make_item(element) -> QTreeWidgetItem: r = QTreeWidgetItem([element.tag, None, element.text]) r.addChildren([QTreeWidgetItem([None, name, value]) for name, value in element.attrib.items()]) r.addChildren([_make_item(child) for child in element]) return r def main(): app = QApplication(sys.argv) # TODO: Check for suitable arguments first. # Parameter may be a Qt option. if len(sys.argv) == 1: print("Usage:", sys.argv[0], "[file …]", file=sys.stderr) return items = [] for arg in sys.argv[1:]: with open(arg) as xmlfile: xmltree = ET.parse(xmlfile) fileItem = QTreeWidgetItem([arg]) items.append(fileItem) fileItem.addChildren([_make_item(xmltree.getroot())]) tree = QTreeWidget() tree.setColumnCount(2) tree.setHeaderLabels(["Element", "attribute", "text"]) tree.insertTopLevelItems(0, items) tree.show() sys.exit(app.exec_()) # Main loop. if __name__ == '__main__': main()