summaryrefslogtreecommitdiff
path: root/xmlbrowser
diff options
context:
space:
mode:
Diffstat (limited to 'xmlbrowser')
-rwxr-xr-xxmlbrowser39
1 files changed, 39 insertions, 0 deletions
diff --git a/xmlbrowser b/xmlbrowser
new file mode 100755
index 0000000..731b466
--- /dev/null
+++ b/xmlbrowser
@@ -0,0 +1,39 @@
+#!/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()