I inserted a XML file into the QTreeWidget view and I want to get the id of the selected directory in the tree and insert a new XML element (directory) into it.
What the XML-file basically looks like:
For example, I want to click on directory 1 and create a new directory or document in it. I want to get the ID of the selected directory and create a new directory in that element via the ID.
<dir name="Work space" id="directory_0">
<dir name="Directory 1" id="directory_1">
<document name="Document 1" id="document_1_1"></document>
<document name="Document 2" id="document_1_2"></document>
<document name="Document 3" id="document_1_3"></document>
<document name="Document 4" id="document_1_4"></document>
<document name="Document 5" id="document_1_5"></document>
<document name="Document 6" id="document_1_6"></document>
</dir>
<dir name="Directory 2" id="directory_2">
<document name="Document 1" id="document_2_1"></document>
<document name="Document 2" id="document_2_2"></document>
<document name="Document 3" id="document_2_3"></document>
</dir>
</dir>
I managed to get the element ID, but I don't know how to insert a new element.
self.treeWidget.itemClicked.connect(self.onItemClicked)
def onItemClicked(self):
item = self.treeWidget.currentItem()
print(self.getParentPath(item))
def getParentPath(self, item):
def getParent(item, outstring):
if item.parent() is None:
return outstring
outstring = item.parent().text(0) "/" outstring
return getParent(item.parent(), outstring)
output = getParent(item, item.text(0))
return output
For example, if I want to add directory 3 and directory 4 to directory 2, it would look like this.
<dir name="Work space" id="directory_0">
<dir name="Directory 1" id="directory_1">
<document name="Document 1" id="document_1_1"></document>
<document name="Document 2" id="document_1_2"></document>
<document name="Document 3" id="document_1_3"></document>
<document name="Document 4" id="document_1_4"></document>
<document name="Document 5" id="document_1_5"></document>
<document name="Document 6" id="document_1_6"></document>
</dir>
<dir name="Directory 2" id="directory_2">
<document name="Document 1" id="document_2_1"></document>
<document name="Document 2" id="document_2_2"></document>
<document name="Document 3" id="document_2_3"></document>
<dir name="Directory 3" id="directory_3"></dir>
<dir name="Directory 4" id="directory_4"></dir>
</dir>
</dir>
So I need to list all the directories and the one I choose to get his id and insert a new directory
CodePudding user response:
See below
import xml.etree.ElementTree as ET
xml = '''<dir name="Work space" id="directory_0">
<dir name="Directory 1" id="directory_1">
<document name="Document 1" id="document_1_1"></document>
<document name="Document 2" id="document_1_2"></document>
<document name="Document 3" id="document_1_3"></document>
<document name="Document 4" id="document_1_4"></document>
<document name="Document 5" id="document_1_5"></document>
<document name="Document 6" id="document_1_6"></document>
</dir>
<dir name="Directory 2" id="directory_2">
<document name="Document 1" id="document_2_1"></document>
<document name="Document 2" id="document_2_2"></document>
<document name="Document 3" id="document_2_3"></document>
</dir>
</dir>'''
root = ET.fromstring(xml)
folder = root.find('.//dir[@name="Directory 2"]')
for x in [3,4]:
ET.SubElement(folder, 'dir',{'name':f'Directory {x}','id':f'directory_{x}'})
ET.dump(root)
output
<?xml version="1.0" encoding="UTF-8"?>
<dir name="Work space" id="directory_0">
<dir name="Directory 1" id="directory_1">
<document name="Document 1" id="document_1_1" />
<document name="Document 2" id="document_1_2" />
<document name="Document 3" id="document_1_3" />
<document name="Document 4" id="document_1_4" />
<document name="Document 5" id="document_1_5" />
<document name="Document 6" id="document_1_6" />
</dir>
<dir name="Directory 2" id="directory_2">
<document name="Document 1" id="document_2_1" />
<document name="Document 2" id="document_2_2" />
<document name="Document 3" id="document_2_3" />
<dir name="Directory 3" id="directory_3" />
<dir name="Directory 4" id="directory_4" />
</dir>
</dir>