1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
from xml.etree import cElementTree as etree
from cStringIO import StringIO
from PySide.QtGui import QTreeView, QStandardItemModel, QStandardItem, QApplication
from PySide.QtCore import Qt
XML = '''
<root>
<lv1 name="first" custom="test attribute 1">
<lv2 name="sub first"/>
<lv2 name="sub second"/>
</lv1>
<lv1 name="second" custom="test attribute 2">
<lv2 name="sub third"/>
<lv2 name="sub fourth"/>
</lv1>
</root>
'''
def parseXML(parent, node):
for element in node:
item = QStandardItem()
item.setData(element.attrib.get('name', 'undefined'), Qt.DisplayRole)
item.setData(element.attrib.get('custom'), Qt.UserRole)
parseXML(item, element)
parent.appendRow(item)
if __name__ == '__main__':
xml = etree.parse(StringIO(XML)).getroot()
app = QApplication([])
win = QTreeView()
mdl = QStandardItemModel()
win.setModel(mdl)
parseXML(mdl, xml)
win.show()
app.exec_() |