Я создал скрипт python и должен иметь некоторые параметры в XML. Итак, вот XML:
<ROOT>
<FOLDERTYPES>
<FOLDERTYPE_ID>
<NAME>FOURNISSEUR</NAME>
<ID>2</ID>
<SUBFOLDER>
<NAME>Administratif</NAME>
<ID>4</ID>
</SUBFOLDER>
<SUBFOLDER>
<NAME>Commandes</NAME>
<ID>5</ID>
</SUBFOLDER>
<SUBFOLDER>
<NAME>Factures</NAME>
<ID>6</ID>
</SUBFOLDER>
</FOLDERTYPE_ID>
<FOLDERTYPE_ID>
<NAME>CLIENT</NAME>
<ID>3</ID>
<SUBFOLDER>
<NAME>Administratif</NAME>
<ID>4</ID>
</SUBFOLDER>
<SUBFOLDER>
<NAME>Commandes</NAME>
<ID>5</ID>
</SUBFOLDER>
<SUBFOLDER>
<NAME>Factures</NAME>
<ID>6</ID>
</SUBFOLDER>
<SUBFOLDER>
<NAME>Logistique</NAME>
<ID>7</ID>
</SUBFOLDER>
</FOLDERTYPE_ID>
</FOLDERTYPES>
На данный момент я просто могу получить "имя" и "идентификатор" следующим образом:
{'FOURNISSEUR': {'id': '2'}, 'CLIENT': {'id': '3'}}
Но мне нужно иметь все подпапки, в таком тике:
{'FOURNISSEUR': {'id': '2', 'subfolders' : {'Administratif':'4','Commandes':'5','Factures':'6'}}, 'CLIENT': {'id': '3', 'subfolders' : {'Administratif':'4','Commandes':'5','Factures':'6','Logistique':'7'}}
Вот фрагмент кода, который у меня есть сейчас:
def getFolderTypeArray(fileName):
result = {}
with open(fileName, 'rb') as config_file:
content = config_file.read()
config = BeautifulSoup(content, "lxml")
folderTypesId = config.find_all('foldertype_id')
for folderType in folderTypesId:
label = folderType.find('name').string
folderTypeId = folderType.find('id').string
result[label] = {'id' : folderTypeId}
Добавить в другой цикл
folderTypesId = config.find_all('foldertype_id')
for folderType in folderTypesId:
label = folderType.find('name').string
folderTypeId = folderType.find('id').string
subfolders = dict()
for s in folderType.find_all('subfolder'):
subfolders[s.find('name').string] = s.find('id').string
result[label] = {'id' : folderTypeId, 'subfolders': subfolders}